- Mybatis-config.xml
// 1. typeAlias 别名配置
// 2. mapper 配置
-
Spring-mybaits.xml
// 1. DataSource配置 // 2. SqlSessionAFactory配置 // 3. MapperScannerConfigurer配置 // 4. Manger 事务 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置文件引入 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 数据源定义 --> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- sqlSession 注入 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="datasource" /> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 接口实现类注入 不需要声明 由MapperScannerConfigurer自动声称代理实现类并且赋值SqlSession 【配合MapperScan使用】 --> <bean id="configurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.qqhru.mapper"/> </bean> </beans> -
UserMapper.class
接口中 要使用MapperScan注解 进行Bean在Spring中的托管 /** * @MapperScan就是上面所讲的Mapper扫描器中所需要的配置,会自动生成代理对象。 * 注意,接口中的方法名称要和对应的MyBatis映射文件中的语句的id值一样,因为生成的 * 动态代理,会根据这个匹配相应的Sql语句执行。另外就是方法的参数和返回值也需要注 * 意。接口中的方法如何定义,对应的MyBatis映射文件就应该进行相应的定义。 * 最后,标注中的userDao是用来作为Spring的Bean的id(或name)进行使用的,方便我 * 们在Service层进行注入使用。 */ 1. 扫描指定接口 2. 注册这些接口的bean定义到spring容器 -
service层:
配置文件中:spring-service.xml
//1. 事务配置 //2. 开启注解扫描 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> // 开启注解扫描 配合@Service注解 和AutoWried 为Mapper属性赋值并且注册bean到spring中 <context:component-scan base-package="com.qqhru.service"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"/> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config > <aop:pointcut id="txPointcut" expression="execution(* com.qqhru.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans> -
serviceImpl.class
@Service("mangerService") public class MangerServiceImpl implements MangerService { @Autowired private MangerMapper mangerMapper; public Manger login(Manger manger) { return mangerMapper.login(manger); } } -
Service.class
public interface MangerService { Manger login(@Param("manger") Manger manger); } -
MVC层:
配置文件
Spring-MVC.xml
//1. 注解扫描 //2. 字符集过滤器 //3. 视图解析器 //4. 静态资源过滤 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.qqhru.controller"/> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8"/> <property name="writeAcceptCharset" value="false"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:default-servlet-handler/> </beans>
本文详细介绍了Spring与MyBatis的整合配置过程,包括Mybatis-config.xml中的typeAlias和mapper配置,Spring-mybatis.xml中的DataSource、SqlSessionFactory和MapperScannerConfigurer的设置,以及Service层的事务管理和注解扫描。在ServiceImpl类中,通过@Autowired注解实现Mapper接口的自动装配,并展示了Service接口定义。Spring-MVC.xml配置了注解扫描、字符集过滤器、视图解析器和静态资源处理,确保了MVC层的正常运行。
5253

被折叠的 条评论
为什么被折叠?



