SSM框架整合【第四部分-Spring整合Mybatis框架】
第四部分-Spring整合Mybatis框架
1)搭建和测试Mybatis环境
A)在web项目中编写Mybatis-config.xml配置文件并配置
1 <configuration>
2 <environments default="mysql">
3 <environment id="mysql">
4 <transactionManager type="JDBC"/>
5 <dataSource type="POOLED">
6 <property name="driver" value="com.mysql.jdbc.Driver"/>
7 <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
8 <property name="username" value="root"/>
9 <property name="password" value="root"/>
10 </dataSource>
11 </environment>
12 </environments>
13 <!-- 使用的是注解 -->
14 <mappers>
15 <!-- <mapper class="com.dream.dao.IAccountDao"/> -->
16 <!-- 该包下所有的dao接口都可以使用 -->
17 <package name="com.dream.dao"/>
18 </mappers>
19 </configuration>
B)在IAccountDao接口方法上添加注解,编写SQL语句
1 /**
2 * @author ZhangJun
3 * @date 2020-04-04
4 * @description 账户dao接口
5 */
6 @Repository("accountDao")
7 public interface IAccountDao {
8 /**
9 * 查询所有账户信息
10 * @return
11 */
12 @Select("select * from account")
13 public List<Account> findAll();
14
15 /**
16 * 保存账户信息
17 * @param account
18 */
19 @Insert("insert into account(name,money) values(#{name},#{money})")
20 public void saveAccount(Account account);
21 }
C)编写测试类,测试Mybatis框架
1 /**
2 * @author ZhangJun
3 * @date 2020-04-04
4 * @description
5 */
6 public class TestMybatis {
7 @Test
8 public void testFindAll() throws IOException {
9 //1.加载Mybatis的配置文件
10 InputStream resourceAsStream = Resources.getResourceAsStream("Mybatis-Config.xml");
11 //2.创建SqlSessionFactory工厂
12 SqlSessionFactoryBuilder builder= new SqlSessionFactoryBuilder();
13 SqlSessionFactory factory = builder.build(resourceAsStream);
14 //3.创建SQLSession
15 SqlSession session = factory.openSession();
16 //4.创建Dao接口中的代理方法
17 IAccountDao mapper = session.getMapper(IAccountDao.class);
18 //5.执行dao的方法
19 List<Account> accounts = mapper.findAll();
20 for(Account account: accounts){
21 System.out.println(account);
22 }
23 //6.释放资源
24 session.close();
25 resourceAsStream.close();
26 }
27
28 @Test
29 public void testSaveAccount() throws IOException {
30 //1.加载Mybatis的配置文件
31 InputStream resourceAsStream = Resources.getResourceAsStream("Mybatis-Config.xml");
32 //2.创建SqlSessionFactory工厂
33 SqlSessionFactoryBuilder builder= new SqlSessionFactoryBuilder();
34 SqlSessionFactory factory = builder.build(resourceAsStream);
35 //3.创建SQLSession
36 SqlSession session = factory.openSession();
37 //4.创建Dao接口中的代理方法
38 IAccountDao mapper = session.getMapper(IAccountDao.class);
39 //5.执行方法
40 Account account = new Account();
41 account.setName("张三");
42 account.setMoney(15869d);
43 mapper.saveAccount(account);
44 //5.1 提交事务【当插入和修改时,需要我们自己提交事务】
45 session.commit();
46 //6.释放资源
47 session.close();
48 resourceAsStream.close();
49 }
50 }
2)Spring整合Mybatis框架
A)要求:把Mybatis-config.xml配置文件的内容配置到applicationContext.xml配置文件中去
1 <!--=========================Spring整合MyBatis框架==========================-->
2 <!--配置连接池-->
3 <!--引入外部配置文件-->
4 <context:property-placeholder location="classpath:jdbcConfig.properties"/>
5 <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
6 <property name="driverClass" value="${jdbc.driver}"></property>
7 <property name="jdbcUrl" value="${jdbc.url}"></property>
8 <property name="user" value="${jdbc.username}"></property>
9 <property name="password" value="${jdbc.password}"></property>
10 </bean>
11 <!--配置SqlSessionFactory工厂-->
12 <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
13 <!--添加数据源-->
14 <property name="dataSource" ref="pooledDataSource"></property>
15 </bean>
16 <!--配置AccountDao接口所在的包-->
17 <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
18 <property name="basePackage" value="com.dream.dao"></property>
19 </bean>
B)把持久层接口的创建工作交给spring框架来管理

C)在业务层中注入持久层对象

D)测试
3)配置Spring的声明式事务管理
1 <!--===================配置Spring框架声明式事务管理===============================-->
2 <!--配置事务管理器-->
3 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
4 <property name="dataSource" ref="pooledDataSource"></property>
5 </bean>
6 <!--配置事务通知-->
7 <tx:advice id="txAdvice" transaction-manager="transactionManager">
8 <tx:attributes>
9 <!--所有方法都是事务方法-->
10 <tx:method name="*" propagation="REQUIRED" read-only="false"/>
11 <!--以find开始的所有方法-->
12 <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
13 </tx:attributes>
14 </tx:advice>
15 <!--配置AOP增强-->
16 <aop:config>
17 <!--配置切入点表达式-->
18 <aop:pointcut id="pt1" expression="execution(* com.dream.service..*(..))"/>
19 <!--建立切入点表达式和事务通知的对应关系-->
20 <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
21 </aop:config>
22 <!--=============================================================================-->

浙公网安备 33010602011771号