spring-boot配置MySQL數據庫連接、Hikari連接池、和Mybatis的簡單方法

此方法爲極簡配置,支持MySQL數據庫多庫連接、支持Hikari連接池、支持MyBatis(包括Dao類和xml文件位置的配置)。

如果需要更靈活的自定義配置(比如支持分頁插件),請參考:http://blog.csdn.net/clementad/article/details/51776151

1、pom.xml中引入依賴:

	<!-- Begin of DB related -->
		<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 -->
我們使用了mybatis-spring-boot-starter,並讓它把tomcat-jdbc連接池排除掉,這樣spring-boot就會尋找是否有HikariCP可用,第二個依賴就被找到了,然後mysql-connector也有了。

2、application.yml中的相關配置:

spring:
  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/
其中,datasource.url最後面不跟dbName,這樣就可以支持多個db的情況,使用的時候只需要在sql語句的table名前面裏面指定db名字就行了。

3、Dao接口代碼:

package com.xjj.dao;

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);
}
只需要用@Mapper註解,就可以支持被Mybatis找到,並支持在方法上面寫SQL語句。

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("八"));

	}






發佈了201 篇原創文章 · 獲贊 397 · 訪問量 204萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章