Spring整合MyBatis
即,Spring框架中加入MyBatis的功能,让Spring的配置文件applicationContext.xml完成一部分sqlMapConfig.xml的配置
-
将sqlMapConfig.xml命名为sqlMapConfig-spring.xml,里面内容只保留
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 定义别名--> <typeAliases> <!-- <typeAlias type="com.domain.Account" alias="account"/>--> <package name="com.domain"/> </typeAliases> </configuration>
-
在applicationContext.xml中配置上面删除的内容
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 组件扫描 service和mapper--> <context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 加载properteis文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置sessionFactory:sessionFactory是为了让spring自动创建下面的mapper对象--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 加载mybatis核心文件--> <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"/> </bean> <!-- 扫描mapper所在的包: 创建mapper对象, 通过注解注入,快速使用--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mapper"/> </bean> <!-- 声明式事务控制--> <!-- 平台事务管理器:让每一个service成为一个事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置事务增强--> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 事务的织入--> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.impl.*.*(..))"/> </aop:config> </beans>
-
AccountServiceImpl.java中通过注解注入mapper,直接使用
@Service("accountService") public class AccountServiceImpl implements AccountService { @Autowired private AccountMapper accountMapper; @Override public void save(Account account) { accountMapper.save(account); } @Override public List<Account> findAll() { return accountMapper.findAll(); } }
-
测试