9.spring整合Mybatis

一.原始框架搭建

流程:

  1. 导入包和解决资源访问问题

  2. 编写实体类

  3. 编写mapper接口和mapper.xml

  4. 核心配置文件mybatis-config.xml中添加mapper

  5. 编写测试类

1.导入包和解决资源访问问题

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>Spring-Study</artifactId>
 7         <groupId>ustc.wzh</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>spring-10-mybatis</artifactId>
13 
14     <dependencies>
15         <!--junit-->
16         <dependency>
17             <groupId>junit</groupId>
18             <artifactId>junit</artifactId>
19             <version>4.12</version>
20         </dependency>
21         <!--mybatis-->
22         <dependency>
23             <groupId>org.mybatis</groupId>
24             <artifactId>mybatis</artifactId>
25             <version>3.5.2</version>
26         </dependency>
27         <!--mysql-->
28         <dependency>
29             <groupId>mysql</groupId>
30             <artifactId>mysql-connector-java</artifactId>
31             <version>5.1.47</version>
32         </dependency>
33         <!--spring 相关-->
34         <dependency>
35             <groupId>org.springframework</groupId>
36             <artifactId>spring-webmvc</artifactId>
37             <version>5.1.10.RELEASE</version>
38         </dependency>
39         <dependency>
40             <groupId>org.springframework</groupId>
41             <artifactId>spring-jdbc</artifactId>
42             <version>5.1.10.RELEASE</version>
43         </dependency>
44         <!--AOP切面编程-->
45         <dependency>
46             <groupId>org.aspectj</groupId>
47             <artifactId>aspectjweaver</artifactId>
48             <version>1.9.4</version>
49         </dependency>
50         <!--mybatis-spring整合包-->
51         <dependency>
52             <groupId>org.mybatis</groupId>
53             <artifactId>mybatis-spring</artifactId>
54             <version>2.0.2</version>
55         </dependency>
56         <!--lombok-->
57         <dependency>
58             <groupId>org.projectlombok</groupId>
59             <artifactId>lombok</artifactId>
60             <version>1.18.10</version>
61         </dependency>
62     </dependencies>
63 
64     <build>
65         <resources>
66             <resource>
67                 <directory>src/main/java</directory>
68                 <includes>
69                     <include>**/*.properties</include>
70                     <include>**/*.xml</include>
71                 </includes>
72                 <filtering>true</filtering>
73             </resource>
74         </resources>
75     </build>
76 
77 </project>

2.编写实体类

1 import lombok.Data;
2 
3 @Data
4 public class User {
5     private int id;  //id
6     private String name;   //姓名
7     private String pwd;   //密码
8 }

3.编写mapper接口和mapper.xml

UserMapper接口

1 public interface UserMapper {
2 
3     List<User> selectUser();
4 }

UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <!--namespace用于绑定一个对应的Dao/Mapper接口-->
 7 <mapper namespace="ustc.wzh.mapper.UserMapper">
 8 
 9     <!--select查询语句 id绑定函数名 resultType绑定返回类型-->
10     <select id="selectUser" resultType="user">
11       select * from mybatis.user
12     </select>
13 </mapper>

4.核心配置文件mybatis-config.xml中添加mapper

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7     <typeAliases>
 8         <package name="ustc.wzh.pojo"/>
 9     </typeAliases>
10 
11     <environments default="development">
12         <environment id="development">
13             <transactionManager type="JDBC"/>
14             <dataSource type="POOLED">
15                 <property name="driver" value="com.mysql.jdbc.Driver"/>
16                 <property name="url"
17                           value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
18                 <property name="username" value="root"/>
19                 <property name="password" value="123456"/>
20             </dataSource>
21         </environment>
22     </environments>
23 
24     <mappers>
25         <mapper resource="ustc/wzh/mapper/UserMapper.xml"></mapper>
26     </mappers>
27 </configuration>

5.编写测试类

 1 public class MyTest {
 2 
 3     @Test
 4     public void test() throws IOException {
 5         String resources = "mybatis-config.xml";
 6 
 7         InputStream in = Resources.getResourceAsStream(resources);
 8         SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
 9 
10         SqlSession sqlSession = sessionFactory.openSession(true);
11 
12         UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
13         List<User> userList = userMapper.selectUser();
14 
15         for (User user : userList) {
16 
17             System.out.println(user);
18         }
19     }
20 }

二.Spring整合Mybatis方式一

步骤:(在上一步的基础上)

  1. 创建spring-dao.xml和applicationContext.xml:

    1. spring-dao.xml:是用于连接数据源,创建sqlSessionFactory,创建sqlSessionTemplate(也就是创建sqlSession)

    2. applicationContext.xml:用于导入其他配置信息,创建bean对象,是Spring的主配置文件

  2. 新建UserMapperImpl类为UserMapper接口的实现类,被Spring用于注入sqlSession创建对象

  3. 编写测试类

创建spring-dao.xml和applicationContext.xml:

spring-dao.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/aop
 7         http://www.springframework.org/schema/aop/spring-aop.xsd">
 8 
 9 
10     <!--spring-dao.xml用于绑定数据源,获得sqlSessionFactory,再获得sqlSession-->
11 
12     <!--1.配置数据源,需要spring-jdbc包
13         使用Spring来管理数据源就不需要mybatis-config.xml来管理数据源了
14     -->
15     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
16         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
17         <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
18         <property name="username" value="root"/>
19         <property name="password" value="123456"/>
20     </bean>
21 
22     <!--2.配置sqlSessionFactory-->
23     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
24         <property name="dataSource" ref="dataSource"/>
25         <!--绑定Mybatis配置文件
26             configLocation:绑定mybatis-config.xml的地址
27             mapperLocation:绑定mapper.xml的地址
28         -->
29         <property name="configLocation" value="classpath:mybatis-config.xml"/>
30         <property name="mapperLocations" value="classpath:ustc/wzh/mapper/*.xml"/>
31     </bean>
32 
33     <!--3.配置sqlSession
34         SqlSessionTemplate:就是我们所说的sqlSession
35         注册SqlSessionTemplate , 关联sqlSessionFactory
36     -->
37     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
38         <!--只能使用构造器注入,没有set方法-->
39         <constructor-arg index="0" ref="sqlSessionFactory"/>
40     </bean>
41 
42 
43 </beans>

applicationContext.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:aop="http://www.springframework.org/schema/aop"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/aop
 7         http://www.springframework.org/schema/aop/spring-aop.xsd">
 8 
 9 
10     <import resource="spring-dao.xml"></import>
11 
12     <!--UserMapperImpl实体类装配,并注入sqlSession参数-->
13     <bean id="userMapper" class="ustc.wzh.mapper.UserMapperImpl">
14         <property name="sqlSession" ref="sqlSession"/>
15     </bean>
16 
17 
18 </beans>

新建UserMapperImpl类

 1 public class UserMapperImpl implement UserMapper{
 2 
 3     //sqlSession不用我们自己创建了,Spring来管理
 4     private SqlSessionTemplate sqlSession;
 5 
 6     //用于注入sqlSession
 7     public void setSqlSession(SqlSessionTemplate sqlSession) {
 8         this.sqlSession = sqlSession;
 9     }
10 
11     public List<User> selectUser() {
12         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
13         return mapper.selectUser();
14     }
15 }

编写测试类

 1 public class MyTest {
 2     
 3     @Test
 4     public void test(){
 5         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
 6 
 7         UserMapper userMapper = (UserMapper) context.getBean("userMapper");
 8         for (User user : userMapper.selectUser()) {
 9             System.out.println(user);
10         }
11 
12     }
13 }

三.Spring整合Mybatis方式二

步骤:(在上一步的基础上)

  1. 新建UserMapperImpl2类为UserMapper接口的实现类并且继承SqlSessionDaoSupport类,被Spring用于注入sqlSession创建对象

    1. 继承SqlSessionDaoSupport类就可以在spring-dao.xml中去掉配置sqlSession了,

  2. 修改applicationContext.xml添加一个bean对象,该对象中需要填入属性为sqlSessionFactory,由于UserMapperImpl2继承了SqlSessionDaoSupport类则通过这个属性会帮助我们在内部创建sqlSession对象了不需要手动创建了

  3. 编写测试类

新建UserMapperImpl2类

1 public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
2 
3     public List<User> selectUser() {
4         UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
5         return mapper.selectUser();
6     }
7 }

修改applicationContext.xml添加一个bean对象

1 <bean id="userMapper2" class="ustc.wzh.mapper.UserMapperImpl2">
2     <property name="sqlSessionFactory" ref="sqlSessionFactory" />
3 </bean>

编写测试类

@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    UserMapper userMapper2 = (UserMapper) context.getBean("userMapper2");
    for (User user : userMapper2.selectUser()) {
        System.out.println(user);
    }

}

总结 :

  • 整合到spring中以后可以完全不要mybatis的配置文件,除了这些方式可以实现整合之外,我们还可以使用注解来实现,这个等我们后面学习SpringBoot的时候还会测试整合!

 

posted @ 2020-02-18 22:33  All_just_for_fun  阅读(212)  评论(0编辑  收藏  举报