SpringBoot整合Mybatis配置druid数据库连接池
1、springboot得到的最终效果是一个简化到极致的web开发,但是只要牵扯到web开发,就绝对不可能缺少数据层操作,所有的开发都一定秉持着MVC设计模式的原则,MVC里面业务层不可少,数据层永远要与业务层绑定在一起,既然要进行数据层的操作,那么肯定首选的一定就是mybatis,因为mybatis整合处理之后尤其是与spring整合里面可以直接避免掉DAO层的编写,同时VO类也是最干净的,这一点上绝对要比其他的ORMapping组件都方便。配置druid数据源这个数据库连接池的配置是有某里提供的,并且由于其性能很高,同时具备有很好的监控性,所以已经开始广泛的使用了。首先创建一个数据库的创建脚本。CREATE TABLE `dept` ( `dname` varchar(100) DEFAULT NULL, `loc` varchar(50) DEFAULT NULL, `deptno` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`deptno`)) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;

3、如果要进行数据源的整合处理,直接修改application.yml配置文件即可:server: port: 8080spring: messages: basename: i18n/Message,i18n/Pages #资源文件的名称 datasource: #配置当前要使用的数据源的操作类型 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/mldn_1 username: root password: root dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200

5、测试一下当前的连接池是否可用。package com.gwolf.test;import com.gwolf.StartSpringBootMain;import com.gwolf.controller.MessageController;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 javax.annotation.Resource;import javax.sql.DataSource;@SpringBootTest(classes = StartSpringBootMain.class)@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfigurationpublic class TestDataSource { @Resource private DataSource dataSource; @Test public void testConnection() throws Exception{ System.out.println(this.dataSource.getConnection()); } }
