MybatisConfig.java文件

  1 import com.alibaba.druid.pool.DruidDataSource;
  2 import com.xman.common.mybatis.SqlMonitorManager;
  3 import org.apache.ibatis.plugin.Interceptor;
  4 import org.apache.ibatis.session.SqlSessionFactory;
  5 import org.mybatis.spring.SqlSessionFactoryBean;
  6 import org.mybatis.spring.annotation.MapperScan;
  7 import org.slf4j.Logger;
  8 import org.slf4j.LoggerFactory;
  9 import org.springframework.beans.factory.annotation.Value;
 10 import org.springframework.context.annotation.Bean;
 11 import org.springframework.context.annotation.Configuration;
 12 import org.springframework.core.env.StandardEnvironment;
 13 import org.springframework.core.io.ClassPathResource;
 14 import org.springframework.core.io.Resource;
 15 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 16 import org.springframework.core.io.support.ResourcePatternResolver;
 17 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 18 import org.springframework.util.ClassUtils;
 19 
 20 import javax.sql.DataSource;
 21 import java.io.IOException;
 22 import java.util.Properties;
 23 
 24 /**
 25  * Created by chai on 2017/10/11.
 26  */
 27 @Configuration  //用作配置信息
 28 @MapperScan(basePackages = "com.xman.rainbow.dao")
 29 public class MybatisConfig {
 30 
 31     private static Logger logger = LoggerFactory.getLogger(MybatisConfig.class);
 32 
 33     @Value("${datasource.driverClass}")
 34     private String jdbcDriver;
 35 
 36     @Value("${datasource.username}")
 37     private String username;
 38 
 39     @Value("${datasource.password}")
 40     private String password;
 41 
 42     @Value("${datasource.jdbcUrl}")
 43     private String jdbcUrl;
 44 
 45     @Value("${datasource.maxIdle}")
 46     private String maxIdle;
 47 
 48     //最小、最大
 49     @Value("${datasource.minIdle}")
 50     private String minIdle;
 51     @Value("${datasource.maxActive}")
 52     private int maxActive;
 53 
 54     @Value("${datasource.maxWait}")
 55     private String maxWait;     //配置获取连接等待超时的时间
 56 
 57     @Value("${datasource.validationQuery}")
 58     private String validationQuery;
 59 
 60     @Value("${datasource.testBorrow}")
 61     private boolean testOnBorrow;
 62 
 63     @Value("${datasource.testOnReturn}")
 64     private boolean testOnReturn;
 65 
 66     @Value("${datasource.testWhileIdle}")
 67     private boolean testWhileIdle;
 68 
 69     @Value("${datasource.timeBetweenEvictionRunsMills}")
 70     private long timeBetweenEvictionRunsMills;  //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
 71 
 72     @Value("${datasource.minEvictableIdleTimeMillis}")
 73     private long minEvictableTimeMills; //配置一个连接在池中最小生存的时间,单位是毫秒
 74 
 75     @Bean   //为Spring容器所管理
 76     public DataSource dataSource() {
 77         DruidDataSource dataSource = new DruidDataSource();
 78         dataSource.setDriverClassName(this.jdbcDriver);
 79         dataSource.setUsername(this.username);
 80         dataSource.setPassword(this.password);
 81         dataSource.setUrl(this.jdbcUrl);
 82         dataSource.setMaxActive(this.maxActive);
 83         dataSource.setValidationQuery(this.validationQuery);
 84         dataSource.setTestOnBorrow(this.testOnBorrow);
 85         dataSource.setTestOnReturn(this.testOnReturn);
 86         dataSource.setTestWhileIdle(this.testWhileIdle);
 87         dataSource.setTimeBetweenConnectErrorMillis(this.timeBetweenEvictionRunsMills);
 88         dataSource.setMinEvictableIdleTimeMillis(minEvictableTimeMills);
 89         return dataSource;
 90     }
 91 
 92     @Bean
 93     public SqlSessionFactory sqlSessionFactory() throws Exception {
 94         logger.debug("start sqlSessionFactory");
 95         final SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
 96 //        设置datasource
 97         sqlSessionFactory.setDataSource(dataSource());
 98 //        设置mybatis configuration 扫描路径
 99         sqlSessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
100         sqlSessionFactory.setFailFast(true);
101         //自动扫描entity目录
102         sqlSessionFactory.setMapperLocations(getResource("mappers", "**/*.xml"));
103         SqlMonitorManager sqlMonitorManager = new SqlMonitorManager();
104         Properties properties = new Properties();
105         properties.setProperty("show_sql", "true");
106         sqlMonitorManager.setProperties(properties);
107 
108         PageInterceptor pageInterceptor = new PageInterceptor();
109         Properties property = new Properties();
110         properties.setProperty("databaseType", "mysql");
111         pageInterceptor.setProperties(property);
112         sqlSessionFactory.setPlugins(new Interceptor[]{sqlMonitorManager, pageInterceptor});
113         return sqlSessionFactory.getObject();
114     }
115 
116     private Resource[] getResource(String basePackage, String pattern) throws IOException {
117         String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
118                 + ClassUtils.convertClassNameToResourcePath(new StandardEnvironment()
119                 .resolveRequiredPlaceholders(basePackage)) + "/" + pattern;
120         Resource[] resources = new PathMatchingResourcePatternResolver().getResources(packageSearchPath);
121         return resources;
122     }
123 
124     /**
125      * 配置事务管理组件
126      * @return
127      */
128     @Bean
129     public DataSourceTransactionManager transactionManager() {
130         logger.debug("start transactionManager");
131         return new DataSourceTransactionManager(dataSource());
132     }
133 }

mybatis-config.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 3 <configuration>
 4     <settings>
 5         <setting name="cacheEnabled" value="true"/>
 6         <setting name="lazyLoadingEnabled" value="true"/>
 7         <setting name="aggressiveLazyLoading" value="true"/>
 8         <setting name="useGeneratedKeys" value="true"/>
 9         <setting name="defaultExecutorType" value="SIMPLE"/>
10         <setting name="defaultStatementTimeout" value="10"/>
11     </settings>
12 </configuration>
View Code

相关文章:

  • 2022-12-23
  • 2021-11-04
  • 2022-12-23
  • 2021-11-21
猜你喜欢
  • 2021-12-12
  • 2022-02-07
相关资源
相似解决方案