此方法为极简配置,支持MySQL数据库多库连接、支持Hikari连接池、支持MyBatis(包括Dao类和xml文件位置的配置)。
如果需要更灵活的自定义配置(比如支持分页插件),请参考:http://blog.csdn.net/clementad/article/details/51776151
1、pom.xml中引入依赖:
<!-- Begin of DB related -->我们使用了mybatis-spring-boot-starter,并让它把tomcat-jdbc连接池排除掉,这样spring-boot就会寻找是否有HikariCP可用,第二个依赖就被找到了,然后mysql-connector也有了。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- End of DB related -->
2、application.yml中的相关配置:
spring:其中,datasource.url最后面不跟dbName,这样就可以支持多个db的情况,使用的时候只需要在sql语句的table名前面里面指定db名字就行了。
profiles:
active: dev
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
hikari:
maxLifetime: 1765000 #一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒以上
maximumPoolSize: 15 #连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
mybatis:
mapperLocations: classpath:mapper/*.xml
---
# 开发环境配置
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/
---
# 测试环境配置
spring:
profiles: test
datasource:
url: jdbc:mysql://192.168.0.12:3306/
---
# 生产环境配置
spring:
profiles: prod
datasource:
url: jdbc:mysql://192.168.0.13:3306/
3、Dao接口代码:
package com.xjj.dao;只需要用@Mapper注解,就可以支持被Mybatis找到,并支持在方法上面写SQL语句。
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.xjj.entity.Person;
@Mapper
public interface PersonDAO {
@Select("SELECT id, first_name AS firstName, last_name AS lastName, birth_date AS birthDate, sex, phone_no AS phoneNo"
+ " FROM test.t_person WHERE id=#{0};")
public Person getPersonById(int id);
public int insertPerson(Person person);
public int updatePersonById(Person person);
public int updatePersonByPhoneNo(Person person);
}
4、XML文件:
在resources目录下创建mapper目录,然后创建xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xjj.dao.PersonDAO">
<!-- 插入数据库用户表 -->
<insert id="insertPerson">
INSERT INTO test.t_person(first_name,last_name,birth_date,sex,phone_no,update_dt)
VALUES(#{firstName},#{lastName},#{birthDate},#{sex},#{phoneNo},NOW())
</insert>
<update id="updatePersonById">
UPDATE test.t_person SET
first_name=#{firstName}, last_name=#{lastName}, birth_date=#{birthDate}, sex=#{sex}, phone_no=#{phoneNo}
WHERE id=#{id}
</update>
<update id="updatePersonByPhoneNo">
UPDATE test.t_person SET
first_name=#{firstName}, last_name=#{lastName}, birth_date=#{birthDate}, sex=#{sex}
WHERE phone_no=#{phoneNo}
</update>
</mapper>
5、测试:
@Test
public void dbTest() throws JsonProcessingException{
Person person2 = personDAO.getPersonById(2);
logger.info("person no 2 is: {}", objectMapper.writeValueAsString(person2));
person2.setFirstName("八");
personDAO.updatePersonById(person2);
person2 = personDAO.getPersonById(2);
logger.info("person no 2 after update is: {}", objectMapper.writeValueAsString(person2));
assertThat(person2.getFirstName(), equalTo("八"));
}