1. 依赖
mybatis相关:
com.baomidou
mybatis-plus-boot-starter
3.4.2
com.baomidou
mybatis-plus
3.3.1
org.mybatis.spring.boot
mybatis-spring-boot-autoconfigure
2.0.1
com.baomidou
mybatis-plus-extension
3.4.2
org.springframework.boot
spring-boot-starter-data-jpa
数据库:pg为例子
com.alibaba
druid-spring-boot-starter
1.2.8
true
org.postgresql
postgresql
42.2.5
2,打包配置注意
注意要将mapper文件打进去
${project.basedir}/lib
BOOT-INF/lib/
**/*.jar
src/main/resources
true
**.yml
**.xml
mock/*.json
mapper/**/*.xml
3,application配置文件
两个数据源
spring:
datasource:
jpa:
show-sql: true
one:
driver-class-name: org.postgresql.Driver
jdbc-url: jdbc:postgresql://数据库1链接地址?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
username:
password:
two:
driver-class-name: org.postgresql.Driver
jdbc-url: jdbc:postgresql://数据库2链接地址?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
username:
password:
4,DataSource配置
第一个数据源:
注意点:使用MybatisSqlSessionFactoryBean这个bean类
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com...module.common.*.mapper", sqlSessionTemplateRef = "oneSqlSessionTemplate")
public class DataSourceConfigMysqlOne {
@Resource
private MybatisPlusInterceptor mybatisPlusInterceptor;
@Bean(name = "oneDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.one")
public DataSource oneDateSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "oneSqlSessionFactory")
@Primary
public SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource datasource)
throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/postgres/*.xml"));
// 多数据源控制台打印sql,配置任意一个数据源即可
MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
mybatisConfiguration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
bean.setConfiguration(mybatisConfiguration);
// 设置分页插件
bean.setPlugins(mybatisPlusInterceptor);
return bean.getObject();
}
@Bean(name = "oneTransactionManager")
@Primary
public DataSourceTransactionManager oneTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("oneSqlSessionTemplate")
// 表示这个数据源是默认数据源
@Primary
public SqlSessionTemplate oneSqlSessionTemplate(
@Qualifier("oneSqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}
第二个数据源:
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com...module.importantPsr.mapper", sqlSessionTemplateRef = "twoSqlSessionTemplate")
public class DataSourceConfigMysqlTwo {
@Resource
private MybatisPlusInterceptor mybatisPlusInterceptor;
// 将这个对象放入Spring容器中
@Bean(name = "twoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.two")
public DataSource twoDateSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "twoSqlSessionFactory")
public SqlSessionFactory oneSqlSessionFactory(@Qualifier("twoDataSource") DataSource datasource)
throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/mysqld/*.xml"));
bean.setPlugins(mybatisPlusInterceptor);
return bean.getObject();
}
@Bean(name = "twoTransactionManager")
public DataSourceTransactionManager twoTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("twoSqlSessionTemplate")
public SqlSessionTemplate oneSqlSessionTemplate(
@Qualifier("twoSqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}
mybatis配置:
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false
* 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 配置分页数据库
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
// 防止全表更新和删除
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
5,启动类正常写
@SpringBootApplication
public class AtomServerApplication {
public static void main(String[] args) {
SpringApplication.run(AtomServerApplication.class, args);
}
}
1. datasource的配置类里面要注意对应上mapper的xml所在位置,即mybatis的xml文件位置
2. datasource的配置类里面@MapperScan 对应上自己的mapper接口类的路径
3. 分页看自己需求是否要配置
4. application文件不需要配置mybatis相关项
最后,希望对你有用啦!