mybatis 之四:mybatis整合spring

前言

整合思路:
1、数据源信息交给spring管理
2、sqlsessionfactory交给spring进行单例管理
3、由spring来管理mapper代理的代理类

下面以根据用户id查询用户信息案例来搭建工程
步骤一:建立java工程

步骤二:添加整合包(这里需要的jar包过多)
Mysql的驱动包
Mybatis的核心包和依赖包
Mybatis和spring的整合包
Spring的包
dbcp数据库连接池包
等等

步骤三:在src目录下建立sqlmapconfig.xml配置文件,里面暂时只有一个dtd文件声明

1
2
3
4
5
6
7
<?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>
</configuration>

步骤四:在src目录下建立applicationcontext.xml配置文件
里面目前只配置一些重要连接数据库的信息,后面步骤将加入重要信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 创建数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:9806/sshe" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定mybatis的全局配置文件的路径 -->
<property name="configLocation" value="SqlMapConfig.xml"></property>
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

步骤五:创建pojo类

1
2
3
4
5
6
public class User {
private int age;
private int id;
private String name;
此处省略set与get方法。。
}

步骤六:创建mapper代理
首先是UserMapper.xml

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zwl.mapper.UserMapper">
<!-- 根据用户ID查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="com.zwl.pojo.User">
SELECT
* FROM user WHERE id =#{id}
</select>
</mapper>

接下来是mapper接口

1
2
3
public interface UserMapper {
public User findUserById(int id) throws Exception;
}

然后在sqlmapconfig.xml文件中声明代理文件

1
2
3
4
5
6
<configuration>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="com/zwl/mapper/UserMapper.xml" />
</mappers>
</configuration>

步骤七:spring中注入

1
2
3
4
5
6
<bean id="UserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 设置代理类的接口 -->
<property name="mapperInterface" value="com.zwl.mapper.UserMapper"></property>
<!-- 依赖注入SqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

步骤八:测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class test {
private ApplicationContext ctx;
//在测试之前自动加载
@Before
public void setUp() throws Exception {
ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception {
UserMapper dao = (UserMapper) ctx.getBean("userMapper");
User user = dao.findUserById(1);
System.out.println(user.getName());
}
}

测试结果:取出数据库中的helloworld
至此mybatis整合spring完成

热评文章