SpringBoot(配置druid数据源、配置MyBatis、事务控制、druid 监控)

SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少
数据层操作,所有的开发都一定秉持着 MVC 设计模式的原则,MVC 里面业务层不可少,数据层永远要与业务层
绑定在一起,既然要进行数据层的操作,那么肯定首选的一定就是 MyBatis,因为 MyBatis 整合处理之后
尤其是与 Spring 整合里面可以直接避免掉 DAO 层的编写, 同时 VO 类也是最干净的,这一点上绝对要比
其它的 ORMapping 组件都方便。
2.1、配置 druid 数据源

这个数据库连接池的配置是由阿里提供的,并且由于其性能很高,同时具备有很好的监控性,在实际的开发之中
已经开始广泛的使用了。

1、 首先编写一个数据库创建脚本:

drop database if exists study ;
create database if not exists study CHARACTER SET UTF8 ;
USE study ;
CREATE TABLE dept (
    deptno        BIGINT        AUTO_INCREMENT ,
    dname        VARCHAR(50) ,
    CONSTRAINT pk_deptno PRIMARY KEY(deptno)
) ;
INSERT INTO dept(dname) VALUES ('开发部') ;
INSERT INTO dept(dname) VALUES ('财务部') ;
INSERT INTO dept(dname) VALUES ('市场部') ;
INSERT INTO dept(dname) VALUES ('后勤部') ;
INSERT INTO dept(dname) VALUES ('公关部') ;
2、 随后要进行druid 的数据源的配置,如果要想使用druid 的数据源,那么首先一定要去修改 pom.xml 
配置文件,引入以下包:

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>5.0.4</version>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.31</version>
</dependency>

3、 如果要进行数据源的整合处理,直接修改 application.properties配置文件即可:

server:
  port: 80
spring:
  messages:
    basename: i18n/Messages,i18n/Pages
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource    # 配置当前要使用的数据源的操作类型
    driver-class-name: org.gjt.mm.mysql.Driver      # 配置MySQL的驱动程序类
    url: jdbc:mysql://localhost:3306/study           # 数据库连接地址
    username: root                                  # 数据库用户名
    password: mysqladmin                            # 数据库连接密码
    dbcp2:                                          # 进行数据库连接池的配置
      min-idle: 5                                   # 数据库连接池的最小维持连接数    
      initial-size: 5                               # 初始化提供的连接数
      max-total: 5                                  # 最大的连接数
      max-wait-millis: 200                          # 等待连接获取的最大超时时间

spring.datasource.url=jdbc:mysql://localhost:3306/study?
characterEncoding=utf-8&useSSL=false&useUnicode=true
spring.datasource.username=root
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 连接池指定 springboot2.02版本默认使用HikariCP 此处要替换成Druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

## 初始化连接池的连接数量 大小,最小,最大
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
## 配置获取连接等待超时的时间
spring.datasource.druid.maxWait=60000
4、 如果这个时候你需要进行 junit 代码测试,则一定要将 mybatis 开发包配置过来,因为只有在 
mybatis 开发包里面才会将 druid 的配置的数据库连接池变为所需要的 DataSource 数据源对象。

       <dependency>
           <groupId>org.mybatis.spring.boot</groupId>
           <artifactId>mybatis-spring-boot-starter</artifactId>
           <version>2.0.0</version>
       </dependency>
5、 测试一下当前的连接池是否可用

package com.microboot.test;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.microboot.StartSpringBootMain;

@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestDataSource {
    @Resource
    private DataSource dataSource;
    @Test
    public void testConnection() throws Exception {
        System.out.println(this.dataSource);
    }
}

猜你喜欢

转载自blog.csdn.net/Leon_Jinhai_Sun/article/details/88173259