I. Main points of this paper
Druid is used to manage the connection pool of our database. This article will introduce how SpringBoot can integrate the connection pool of our database. Ali’s Druid provides basic monitoring, but it can be a security issue if not configured correctly. A complete catalog of articles in the series
-
Druid springboot integration
-
Junit 5 unit tests
-
Automatic rollback of unit test operation DB data
-
springboot + mybatis + druid
-
Yml and Properties interswitch online
Second, development environment
- JDK 1.8
- Maven 3.6.2
- Mybatis 1.3.0
- Springboot 2.4.3
- Mysql 5.6.46
- junit 5
- druid
- idea 2020
Modify pom.xml to add dependencies
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
Copy the code
3. Modify the configuration file
Modify the application-dev.properties file.
logging.file.path=./logs
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.url=JDBC: mysql: / / 9.135. XXX. XXX: 3306 / MMC? useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQuer ies=true&serverTimezone=GMT%2B8
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.async-close-connection-enable=true
spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true; druid.stat.slowSqlMillis=2000
spring.datasource.druid.filters=stat,wall
spring.datasource.druid.initial-size=2
spring.datasource.druid.max-active=8
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=5
spring.datasource.druid.max-wait=2000
spring.datasource.druid.min-evictable-idle-time-millis=30001
spring.datasource.druid.min-idle=2
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.time-between-eviction-runs-millis=10000
spring.datasource.druid.validation-query=select 1 from dual
spring.datasource.druid.validation-query-timeout=1
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=dc8d31b188f343b19b9c6cf888c933ba
Copy the code
5. Add configuration classes
Comment out the start class memberApplication. Java and add @mapperscan.
// @MapperScan(basePackages = "com.mmc.lesson.member.mapper")
@SpringBootApplication
public class MemberApplication {
public static void main(String[] args) { SpringApplication.run(MemberApplication.class, args); }}Copy the code
2, write memberService. Java, such writing will be beneficial when the subsequent increase of multiple data sources.
@Configuration
@MapperScan(basePackages = { "com.mmc.lesson.member.mapper"}, sqlSessionTemplateRef = "druidSqlSessionTemplate")
public class MemberDataSourceConfiguration {
@Bean(name = "druidDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid")
public DataSource dataSource(a) {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
/** * session factory. */
@Bean(name = "druidSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("druidDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mysqlMappers/*.xml"));
bean.setVfs(SpringBootVFS.class);
return bean.getObject();
}
/** * Currently only unit tests use transactions. */
@Bean(name = "druidTransactionManager")
public PlatformTransactionManager platformTransactionManager(@Qualifier("druidDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/** * mybatis to use the session
@Bean(name = "druidSqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("druidSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return newSqlSessionTemplate(sqlSessionFactory); }}Copy the code
Six, run it
1. Write unit tests.
@Slf4j
@ActiveProfiles("dev")
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional // Automatically roll back data inserted into DB by unit tests
public class MemberServiceTest {
@Resource
private MemberService memberService;
/** * add. */
@Test
public void testAdd(a) {
TblMemberInfo member = new TblMemberInfo();
member.setUname("zhangsan");
member.setUsex(1);
member.setUbirth(new Date());
member.setUtel("888");
member.setUaddr("Ling Xiao Dian");
member.setState(0);
member.setDelFlag(0);
member.setUphoto(null);
TblMemberInfo ret = memberService.save(member);
Assertions.assertThat(ret).isNotNull();
log.info("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); log.info(ret.getUname()); }}Copy the code
2. Effect.
[2021-02-25 12:03:41974.] [main] [DEBUG] [c.m.l.member.mapper.TblMemberInfoMapper.insert:? ] - ==> Parameters:null, zhangsan(String), 1(Integer), 2021-02-25 12:03:41729.(Timestamp), 888(String), Ling Xiao Dian (String),null.null.0(Integer), 0(Integer), null
[2021-02-25 12:03:41990.] [main] [DEBUG] [c.m.l.member.mapper.TblMemberInfoMapper.insert:? ] - <== Updates:1
[2021-02-25 12:03:41991.] [main] [DEBUG] [org.mybatis.spring.SqlSessionUtils:? ] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2d64c100]
[2021-02-25 12:03:42032.] [main] [INFO] [com.mmc.lesson.member.service.MemberServiceTest:? ] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -2021-02-25 12:03:42032.] [main] [INFO] [com.mmc.lesson.member.service.MemberServiceTest:? ] - zhangsanCopy the code
3, start the application, browser accesshttp://localhost:8080/druid/index.html.
Seven, summary
Druid is easy to use, but the company does not advocate using it, so this article is mainly Hikari. Next article “Build large distributed services (8) Docker build development environment installation ES”
Add me to exchange learning!