版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013219624/article/details/83117773
1.添加Maven依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
2.db.properties配置
dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
dataSource.user=root
dataSource.password=root
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=25
dataSource.prepStmtCacheSqlLimit=204
dataSource.databaseName=test
dataSource.serverName=127.0.0.1
dataSource.portNumber=3306
dataSource.minimumIdle=1
dataSource.maximumPoolSize=5
3.DataSourceConfig配置
@Configuration
@PropertySources(value = {@PropertySource("classpath:db.properties")})
public class DataSourceConfig {
@Autowired
private Environment environment;
@Bean("dataSource")
public DataSource getDataSource() {
Properties props = new Properties();
props.setProperty("dataSourceClassName", environment.getRequiredProperty("dataSourceClassName"));
props.setProperty("dataSource.user", environment.getRequiredProperty("dataSource.user"));
props.setProperty("dataSource.password", environment.getRequiredProperty("dataSource.password"));
props.setProperty("dataSource.databaseName", environment.getRequiredProperty("dataSource.databaseName"));
props.setProperty("dataSource.serverName", environment.getRequiredProperty("dataSource.serverName"));
props.setProperty("dataSource.portNumber", environment.getRequiredProperty("dataSource.portNumber"));
props.setProperty("dataSource.cachePrepStmts", environment.getRequiredProperty("dataSource.cachePrepStmts"));
props.setProperty("dataSource.prepStmtCacheSize", environment.getRequiredProperty("dataSource.prepStmtCacheSize"));
props.setProperty("dataSource.prepStmtCacheSqlLimit", environment.getRequiredProperty("dataSource.prepStmtCacheSqlLimit"));
props.setProperty("dataSource.useUnicode", "true");
props.setProperty("dataSource.characterEncoding", "utf8");
HikariConfig hikariConfig = new HikariConfig(props);
String minimumIdle = environment.getRequiredProperty("dataSource.minimumIdle");
String maximumPoolSize = environment.getRequiredProperty("dataSource.maximumPoolSize");
if (StringUtils.isNotBlank(minimumIdle)) {
hikariConfig.setMaximumPoolSize(Integer.parseInt(minimumIdle));
}
if (StringUtils.isNotBlank(maximumPoolSize)) {
hikariConfig.setMinimumIdle(Integer.parseInt(maximumPoolSize));
}
return new HikariDataSource(hikariConfig);
}
}
4.MyBatisConfig配置
@Configuration
@MapperScan("com.boomsecret.dao.mapper")
public class MyBatisConfig implements TransactionManagementConfigurer {
@Autowired
@Qualifier("dataSource")
private DataSource dataSource;
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
try {
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
源码 https://gitee.com/jsjack_wang/springboot-demo dev-hikaricp-mybatis分支