一、基本整合方式
思路梳理
-
以上流程可以全部移交给Spring来处理
-
读取配置文件、组件的创建、组件之间的依赖关系以及整个框架的生命周期都由Spring容器统一管理
Spring和MyBatis的整合步骤
-
整合所需的依赖及配置
-
通过Spring配置文件配置数据源
-
通过Spring配置文件创建SqlSessionFactory
-
通过SqlSessionTemplate操作数据库
二、整合步骤
① 导入我们需要用到的依赖
Spring核心
MyBatis核心
MySQL JDBC驱动
log4j日志
JUnit单元测试
Lombok插件
Druid数据库连接池
Spring和的MyBatis整合包
Spring和ORM框架的整合包—此依赖项会自动导入spring-orm,spring-jdbc,spring-tx三个jar包
Spring和JUnit的整合包……
这里用的的版本可以根据你们需要的版本进行更改
<!--spring用到的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.10</version>
</dependency>
<!--mybatis用到的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--junit,mysql,log4j的依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
② 配置MyBatis文件 myBatis-config.xml
<?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="autoMappingBehavior" value="FULL"/>
<setting name="cacheEnabled" value="true" />
</settings>
<typeAliases>
<package name="domain"/>
</typeAliases>
<!--3.配置需要引用的SQL映射文件的位置(SysUserMapper.xml将在后续步骤中创建)-->
<mappers>
<mapper resource="mapper/UserMapper.xml"></mapper>
</mappers>
</configuration>
mapper 的配置跟之前配置mybatis时并无多大区别
<?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="mapper.UserMapper">
</mapper>
③ 建立domain mapper service包
④ 建立spring配置文件
(1) 编写applicationContext.xml配置文件
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
(2)引用database.properties文件
<context:property-placeholder location="database.properties"></context:property-placeholder>
(3)配置数据源
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
注意:
这里username和password会和数据源重名,建议在database.properties文件中给username和password前面加上jdbc。
(4)配置sqlSessionFactory
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
(5)配置扫描包 MapperScannerConfigurer
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"></property>
</bean>
(6)注解实现依赖注入
<context:component-scan base-package="service"/>
⑤ 举例写一个查询用户表的例子
(1)UserMapper接口
@Repository
public interface UserMapper {
/**查询**/
public List<User> getAllUser();
}
(2)UserMapper.xml映射文件
<?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="mapper.UserMapper">
<!--查询-->
<select id="getAllUser" resultType="domain.User">
select * from User
</select>
</mapper>
(3)UserService接口
package service;
import domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserService {
/**查询**/
public List<User> getAllUser();
}
(4)UserServiceImpl实现类
package service;
import domain.User;
import mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
//查询
@Override
public List<User> getAllUser() {
return userMapper.getAllUser();
}
}
(5)测试类
import domain.User;
import lombok.val;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import service.UserServiceImpl;
import java.util.ArrayList;
import java.util.List;
public class test {
//查询
@Test
public void selectUser(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService)applicationContext.getBean("userService");
userService.getAllUser().stream().forEach(item->{
System.out.println(item);
});
}
}