Spring集成MyBatis

整合的想法:使用spring的ioc核心技术, 把mybatis中使用的对象交给spring统一创建和管理。spring是容器,存放项目中使用的各种对象,例如Service对象,Dao对象,工具类对象等等。

项目结构:

1、在pom.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
36
37
38
39
40
41
42
43
44
45
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- Spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- MyBatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

2、交给spring的mybatis对象

1.数据源

2.SqlSessionFactory对象

3.Dao对象

4.Service对象
创建spring配置文件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
36
37
38
39
40
41
<?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: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/context
https://www.springframework.org/schema/context/spring-context.xsd">

<!-- 将数据库配置文件写入单独的文件中 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--声明数据源 用于连接数据库-->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
</bean>

<!--声明mybatis中的SqlSessionFactoryBean 用于创建SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- set注入,把数据库连接池赋值给dateSource属性-->
<property name="dataSource" ref="myDataSource"/>
<!-- mybatis主配置文件的位置-->
<property name="configLocation" value="classpath:mybatisConfig.xml"/>
</bean>

<!-- 声明Dao类
MapperScannerConfigurer:在内部调用getMapper()生成每一个接口的dao代理对象
创建好的dao对象放在spring容器中,默认名称为 接口名首字母小写-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.zyz.dao"/>
</bean>

<!-- 声明Service类 -->
<bean id="employeeService" class="com.zyz.service.impl.EmployeeServiceImpl">
<property name="employeeDao" ref="employeeDao"/>
</bean>
</beans>

jdbc.propeties

1
2
3
4
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=2824199842
jdbc.maxActive=20

3、mybatis配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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>
<!-- 打印日志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!-- 注册映射文件 -->
<mappers>
<package name="com.zyz.dao"/>
</mappers>

</configuration>

4、在dao包下添加sql映射文件

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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.zyz.dao.EmployeeDao">
<insert id="insertEmp" useGeneratedKeys="true" keyProperty="id">
insert into employee(name,gender,email,dept_id) values(#{name},#{gender},#{email},#{deptId})
</insert>

<select id="selectAllEmps" resultType="com.zyz.bean.Employee">
select id, name, gender, email, dept_id deptId from employee
</select>
</mapper>

5、创建service包添加相应的Service接口并实现

1
2
3
4
5
public interface EmployeeService {
public int addEmp(Employee employee);

public List<Employee> queryAllEmps();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class EmployeeServiceImpl implements EmployeeService {

private EmployeeDao employeeDao;

// 使用set注入赋值
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}

@Override
public int addEmp(Employee employee) {

return employeeDao.insertEmp(employee);
}

@Override
public List<Employee> queryAllEmps() {
return employeeDao.selectAllEmps();
}
}

6、创建测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class TestDao {
@Test
public void test1() {
String config = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
EmployeeDao employeeDao = (EmployeeDao) applicationContext.getBean("employeeDao");
Employee employee = new Employee(null,"zyz001",'1',"2824199842@qq.com",1);
employeeDao.insertEmp(employee);

}

@Test
public void test2() {
String config = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
EmployeeDao employeeDao = (EmployeeDao) applicationContext.getBean("employeeDao");
List<Employee> employees = employeeDao.selectAllEmps();
for (Employee e:employees) {
System.out.println(e);
}

}
}
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
public class TestService {
@Test
public void test1(){
String config = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
EmployeeService employeeService = (EmployeeService) applicationContext.getBean("employeeService");
Employee employee = new Employee(null,"zhangsan",'1',"zhangsan@126.com",1);
// 事务自动提交
employeeService.addEmp(employee);
}

@Test
public void test2(){
String config = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config);
EmployeeService employeeService = (EmployeeService) applicationContext.getBean("employeeService");
Employee employee1 = new Employee(null,"zhangsan",'1',"zhangsan@126.com",1);
// 事务自动提交
List<Employee> employees = employeeService.queryAllEmps();
for(Employee employee:employees){
System.out.println(employee);
}
}

}