整合思路: 将mybatis的核心类对象(sqlsessionfactory)交给spring工厂管理
整合步骤:
1. 引入依赖
2. 建数据库和表
3. 编写对应的实体类
4. 编写DAO接口
5. 编写mapper配置文件
6. 编写spring的配置文件
7. 工厂测试
配置文件spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--数据源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db1?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- sqlsessionfactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- mapper映射-->
<property name="mapperLocations" value="classpath:com/codegzy/mapper/*Mapper.xml"/>
<!-- 实体类包中的类默认起别名-->
<property name="typeAliasesPackage" value="com.codegzy.entity"/>
</bean>
<!-- 配置dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="scannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.codegzy.dao"/>
</bean>
<!-- 事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务 环绕通知-->
<tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面-->
<aop:config>
<aop:pointcut id="pc" expression="within(com.codegzy.service.*ServiceImpl)"/>
<aop:advisor advice-ref="transactionInterceptor" pointcut-ref="pc"/>
</aop:config>
<!-- service-->
<bean class="com.codegzy.service.AccountServiceImpl" id="accountService">
<property name="accountDAO" ref="accountDAO"/>
</bean>
</beans>