SpringBoot整合MyBatis以及多数据源
springboot整合Mybatis
注意:
1、mapper映射文件存放位置:
①存放在java文件夹中
当mapper映射文件存放的位置在src/mian/java中,即和mapper接口文件位置一致时。由于maven编译时会忽略配置xml文件,所以我们需要手动配置过滤其java文件夹下的xml文件即resource下的xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
②存放在resources下
放在同一目录结构
==>
放在resources下的文件夹要和接口中的文件夹目录结构一致。
注意:这里新建包名要一个一个建
,不要想java目录中的结果一样,直接把所有的包名写在一起。
例如:接口目录 src/main/org/lc/mybatis/mapper/xxxx.java
mappes映射文件目录 resources/org/lc/mybatis/mapper/xxxx.mapper
放在不同目录结构
==>
例如:resources/mapper/mybatis01/xxxx.mapper
则需要在yaml中重新指定mappe映射位置:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
mybatis:
#映射resources资源目录下的mapper文件夹下的任意目录下的xml文件
mapper-locations: classpath:/mapper/**/*.xml
2、@Mapper @MapperScan(s)
@Mapper 扫描单个mapper接口
@MapperScans({
@MapperScan(),
@MapperScan()
})
@MapperScan() 扫描指定包下的所有mapper接口,多数据源时,则需要指定对应的sqlSessionFactoryRef
,sqlSessionTemplateRef
@MapperScan(basePackages = "org.lc.mybatis.mapper.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryOne",sqlSessionTemplateRef = "sqlSessionTemplateOne")
@Configuration
public class MybatisConfig {
}
单数据源配置:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.lc</groupId>
<artifactId>mybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.27</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
Lombok插件的使用
:==>
生成getter setter及 tostring
@Getter
@Setter
@ToString
public class User {
private Integer id;
private String username;
private String address;
}
**@Mapper **扫描mapper注入容器中
@Mapper
public interface UserMapper {
List<User> getAllUser();
}
<?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="org.lc.mybatis.mapper.UserMapper">
<select id="getAllUser" resultType="org.lc.mybatis.bean.User">
select * from user
</select>
</mapper>
使用
==>
@SpringBootTest
class MybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
List<User> allUser = userMapper.getAllUser();
System.out.println(allUser);
}
}
多数据源配置:
spring:
datasourceone:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: 123456
datasourcetwo:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://47.96.141.44:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: xxxx
数据源属性注入
==>
@Configuration
public class DataSourceConfig {
@ConfigurationProperties("spring.datasourceone")
@Bean
public DataSource dataSourceOne(){
return DruidDataSourceBuilder.create().build();
}
@ConfigurationProperties("spring.datasourcetwo")
@Bean
public DataSource dataSourceTwo(){
return DruidDataSourceBuilder.create().build();
}
}
SqlSessionFactory与SqlSessionTemplate配置
==>
@MapperScans({
//数据源一的对应的mapper接口位置
@MapperScan(basePackages = "org.lc.mybatis.mapper.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryOne",sqlSessionTemplateRef = "sqlSessionTemplateOne"),
//数据源二的对应的mapper接口位置
@MapperScan(basePackages = "org.lc.mybatis.mapper.mapper2",sqlSessionFactoryRef ="sqlSessionFactoryTwo",sqlSessionTemplateRef = "sqlSessionTemplatelTwo")
})
@Configuration
public class MybatisConfig {
/**
* 数据源一注入
*/
@Qualifier("dataSourceOne")
@Autowired
DataSource dataSourceOne;
/**
* 数据源二注入
*/
@Resource(name = "dataSourceTwo")
DataSource dataSourceTwo;
/**
* SqlSessionFactory 属性配置
* @return
*/
@Bean
public SqlSessionFactory sqlSessionFactoryOne(){
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceOne);
try {
//配置在resources下的自定义mapper文件位置
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mybatis01/**/*.xml"));
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* SqlSessionTemplate 一模板操作配置
* @return
*/
@Bean
public SqlSessionTemplate sqlSessionTemplateOne(){
return new SqlSessionTemplate(sqlSessionFactoryOne());
}
@Bean
public SqlSessionFactory sqlSessionFactoryTwo(){
SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceTwo);
try {
//配置在resources下的自定义mapper文件位置
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mybatis02/**/*.xml"));
return sqlSessionFactoryBean.getObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean
public SqlSessionTemplate sqlSessionTemplatelTwo(){
return new SqlSessionTemplate(sqlSessionFactoryTwo());
}
}
同理=>
①当mapper.xml在java文件夹下,则不需要配置mapper.xml的位置
②当mapper.xml在resources自定义的文件夹下,则需要配置mapper.xml的位置
//配置在resources下的自定义mapper文件位置
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mybatis02/**/*.xml")
因为多数据源的关系,所以所有在yaml的基本mybatis的配置失效,需要重新在SqlSessionFactory配置