package com.xcn.dsxm.main_public.database.config;

import com.alibaba.druid.pool.DruidDataSource;

import com.github.pagehelper.PageHelper;

import org.apache.ibatis.session.SqlSessionFactory;

import org.mybatis.spring.SqlSessionFactoryBean;

import org.mybatis.spring.annotation.MapperScan;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

import java.sql.SQLException;

import java.util.Properties;

import static com.xcn.dsxm.main_public.database.config.DBConfig_xcn_admin.MYBATIS_BASEPACKAGE;

/**

* 在SpringBoot中集成MyBatis,可以在mapper接口上添加@Mapper注解,将mapper注入到Spring,但是如果每一给mapper都添加@mapper注解会很麻烦,

* 这时可以使用@MapperScan注解来扫描包。

* 经测试发现,@MapperScan注解只会扫描包中的接口,不会扫描类,所以可以在包中写Provider类。

* @MapperScan("com.demo.mapper"):扫描指定包中的接口

* @MapperScan("com.demo.*.mapper"):一个*代表任意字符串,但只代表一级包,比如可以扫到com.demo.aaa.mapper,不能扫到com.demo.aaa.bbb.mapper

* @MapperScan("com.demo.**.mapper"):两个*代表任意个包,比如可以扫到com.demo.aaa.mapper,也可以扫到com.demo.aaa.bbb.mapper

*/

@Configuration

@MapperScan(basePackages = MYBATIS_BASEPACKAGE,sqlSessionFactoryRef = "sqlSessionFactory_xcn_admin")

public class DBConfig_xcn_admin {

public static final String MYBATIS_BASEPACKAGE = "com.xcn.dsxm.main.database.dao";

public static final String MAPPER_LOCATION = "classpath*:mybatis/*.xml";

@Value("${xcn_admin_database.druid.url}")

private String url;

@Value("${xcn_admin_database.druid.username}")

private String username;

@Value("${xcn_admin_database.druid.password}")

private String password;

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

private String driverClassName;

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

private int initialSize;

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

private int minIdle;

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

private int maxActive;

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

private int maxWait;

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

private int timeBetweenEvictionRunsMillis;

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

private int minEvictableIdleTimeMillis;

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

private String validationQuery;

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

private boolean testWhileIdle;

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

private boolean testOnBorrow;

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

private boolean testOnReturn;

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

private boolean poolPreparedStatements;

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

private int maxPoolPreparedStatementPerConnectionSize;

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

private String filters;

@Value("{spring.datasource.druid.connectionProperties}")

private String connectionProperties;

/**

* 关闭数据源,否则会造成

* (*) property for user to setup

*/

@Bean(destroyMethod = "close", initMethod = "init")

public DataSource dbconfig_xcn_admin() {

DruidDataSource datasource = new DruidDataSource();

datasource.setUrl(url);

datasource.setUsername(username);

datasource.setPassword(password); //这里可以做加密处理

datasource.setDriverClassName(driverClassName);

//configuration

datasource.setInitialSize(initialSize);

datasource.setMinIdle(minIdle);

datasource.setMaxActive(maxActive);

datasource.setMaxWait(maxWait);

datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

datasource.setValidationQuery(validationQuery);

datasource.setTestWhileIdle(testWhileIdle);

datasource.setTestOnBorrow(testOnBorrow);

datasource.setTestOnReturn(testOnReturn);

datasource.setPoolPreparedStatements(poolPreparedStatements);

datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);

try {

datasource.setFilters(filters);

} catch (SQLException e) {

e.printStackTrace();

}

datasource.setConnectionProperties(connectionProperties);

return datasource;

}

@Bean(name = "transactionManager_xcn_admin")

public DataSourceTransactionManager transactionManager_xcn_admin() {

return new DataSourceTransactionManager(dbconfig_xcn_admin());

}

@Bean(name = "sqlSessionFactory_xcn_admin")

public SqlSessionFactory sqlSessionFactory_xcn_admin(@Qualifier("dbconfig_xcn_admin") DataSource masterDataSource) throws Exception {

org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();

configuration.setCallSettersOnNulls(true); //当数据库值为null时,也返回字段

final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

sessionFactory.setDataSource(masterDataSource);

sessionFactory.setConfiguration(configuration); //加载配置项

sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DBConfig_xcn_admin.MAPPER_LOCATION));

return sessionFactory.getObject();

}

@Bean(name = "pageHelper_xcn_admin")

public PageHelper pageHelper() {

PageHelper pageHelper = new PageHelper();

Properties p = new Properties();

p.setProperty("offsetAsPageNum", "true");

p.setProperty("rowBoundsWithCount", "true");

pageHelper.setProperties(p);

return pageHelper;

}

}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐