Java教程

Mybatis中自定义实例化SqlSessionFactoryBean,java技术体系平台

本文主要是介绍Mybatis中自定义实例化SqlSessionFactoryBean,java技术体系平台,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

stat-view-servlet:

enabled: true

url-pattern: /druid/*

使用SpringBoot作为项目框架自然简单,没有很多的xml配置文件,不需要配置额外的tomcat,不管是开发还是部署都非常方便。但高度集成有时候也会带来一些麻烦。比如你上司要求你在mybatis中集成插件并可以识别common模块的mybatis.xml映射文件。

这个时候你可能首先会百度如何配置mybatis插件、如何配置多模块的mapper-locations,然后有很多博文会说在SqlSessionFactoryBean设置即可。你可能会这么设置:

@Autowired

private SqlSessionFactoryBean sqlSessionFactoryBean;

但是结果不是那么尽人意,初始化的结果为null。这是因为高版本的mybatis实现机制做了一些修改,我们没办法通过@Autowired来实例化SqlSessionFactoryBean对象。所以我们必须自己来实例化SqlSessionFactoryBean对象,而实例化SqlSessionFactoryBean对象的关键就是设置DataSource数据源。我们可以通过如下代码来实例化过SqlSessionFactoryBean。

/**

  • mybatis配置

  • @author linzhiqinag

*/

@Configuration

public class MybatisConfig {

private Logger logger = LoggerFactory.getLogger(MybatisConfig.class);

@Value("${mybatis.mapper-locations}")

private String mapperLocation;

@Value("${common-mybatis.mapper-locations}")

private String commonMapperLocation;

@Value("${spring.datasource.druid.username}")

private String username;

@Value("${spring.datasource.druid.password}")

private String password;

@Value("${spring.datasource.druid.url}")

private String dbUrl;

@Value("${spring.datasource.druid.initial-size}")

private int initialSize;

@Value("${spring.datasource.druid.min-idle}")

private int minIdle;

@Value("${spring.datasource.druid.max-active}")

private int maxActive;

@Value("${spring.datasource.druid.max-wait}")

private long maxWait;

@Value("${spring.datasource.druid.driver-class-name}")

private String driverClassName;

@Value("${spring.datasource.druid.min-evictable-idle-time-millis}")

private long minEvictableIdleTimeMillis;

@Value("${spring.datasource.druid.time-between-eviction-runs-millis}")

private long timeBetweenEvictionRunsMillis;

@Value("${spring.datasource.druid.validation-query}")

private String validationQuery;

@Value("${spring.datasource.druid.test-while-idle}")

private boolean testWhileIdle;

@Value("${spring.datasource.druid.test-on-borrow}")

private boolean testOnBorrow;

@Value("${spring.datasource.druid.test-on-return}")

private boolean testOnReturn;

@Value("${spring.datasource.druid.filter.stat.log-slow-sql}")

private boolean logSlowSql;

@Value("${spring.datasource.druid.filter.stat.slow-sql-millis}")

private long slowSqlMillis;

@Bean

public DruidDataSource dataSource() {

DruidDataSource druidDataSource = new DruidDataSource();

try {

druidDataSource.setUsername(username);

druidDataSource.setPassword(password);

druidDataSource.setUrl(dbUrl);

druidDataSource.setFilters(“stat,wall”);

druidDataSource.setInitialSize(initialSize);

druidDataSource.setMinIdle(minIdle);

druidDataSource.setMaxActive(maxActive);

druidDataSource.setMaxWait(maxWait);

druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

druidDataSource.setUseGlobalDataSourceStat(true);

druidDataSource.setDriverClassName(driverClassName);

druidDataSource.setValidationQuery(validationQuery);

druidDataSource.setTestWhileIdle(testWhileIdle);

druidDataSource.setTestOnBorrow(testOnBorrow);

druidDataSource.setTestOnReturn(testOnReturn);

// 设置需要的过滤

List statFilters =new ArrayList<>();

StatFilter statFilter = new StatFilter();

statFilter.setLogSlowSql(logSlowSql);

statFilter.setSlowSqlMillis(slowSqlMillis);

statFilters.add(statFilter);

// 设置慢SQL

druidDataSource.setProxyFilters(statFilters);

} catch (Exception e) {

e.printStackTrace();

}

return druidDataSource;

}

@Bean

public SqlSessionFactoryBean mysqlSessionFactory(DataSource dataSource) throws Exception {

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

sqlSessionFactoryBean.setDataSource(dataSource);

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

Resource[] resources1 = resolver.getResources(mapperLocation);

Resource[] resources2 = resolver.getResources(commonMapperLocation);

Resource[] resources = new Resource[resources1.length+resources2.length];

for (int i=0;i<resources1.length;i++) {

resources[i] = resources1[i];

}

int initSize = resources1.length;

for (int i=0;i<resources2.length;i++) {

resources[initSize+i] = resources2[i];

}

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

sqlSessionFactoryBean.setMapperLocations(resources);

sqlSessionFactoryBean.setPlugins(new Interceptor[]{new CatMybatisInterceptor(dbUrl)});

return sqlSessionFactoryBean;

这篇关于Mybatis中自定义实例化SqlSessionFactoryBean,java技术体系平台的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!