spring boot配置MySQL數(shù)據(jù)庫(kù)連接、Hikari連接池和Mybatis的簡(jiǎn)單配置方法
此方法為極簡(jiǎn)配置,支持MySQL數(shù)據(jù)庫(kù)多庫(kù)連接、支持Hikari連接池、支持MyBatis(包括Dao類和xml文件位置的配置)。
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就會(huì)尋找是否有HikariCP可用,第二個(gè)依賴就被找到了,然后mysql-connector也有了。
2、application.yml中的相關(guān)配置:
spring: profiles: active: dev datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 hikari: maxLifetime: 1765000 #一個(gè)連接的生命時(shí)長(zhǎng)(毫秒),超時(shí)而且沒被使用則被釋放(retired),缺省:30分鐘,建議設(shè)置比數(shù)據(jù)庫(kù)超時(shí)時(shí)長(zhǎng)少30秒以上 maximumPoolSize: 15 #連接池中允許的最大連接數(shù)。缺省值:10;推薦的公式:((core_count * 2) + effective_spindle_count) mybatis: mapperLocations: classpath:mapper/*.xml --- # 開發(fā)環(huán)境配置 spring: profiles: dev datasource: url: jdbc:mysql://localhost:3306/ --- # 測(cè)試環(huán)境配置 spring: profiles: test datasource: url: jdbc:mysql://192.168.0.12:3306/ --- # 生產(chǎn)環(huán)境配置 spring: profiles: prod datasource: url: jdbc:mysql://192.168.0.13:3306/
其中,datasource.url最后面不跟dbName,這樣就可以支持多個(gè)db的情況,使用的時(shí)候只需要在sql語(yǔ)句的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語(yǔ)句。
4、XML文件:
在resources目錄下創(chuàng)建mapper目錄,然后創(chuàng)建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">
<!-- 插入數(shù)據(jù)庫(kù)用戶表 -->
<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、測(cè)試:
@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("八"));
}
總結(jié)
以上所述是小編給大家介紹的spring boot配置MySQL數(shù)據(jù)庫(kù)連接、Hikari連接池和Mybatis的簡(jiǎn)單配置方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
基于SpringMVC實(shí)現(xiàn)網(wǎng)頁(yè)登錄攔截
SpringMVC的處理器攔截器類似于Servlet開發(fā)中的過濾器Filter,用于對(duì)處理器進(jìn)行預(yù)處理和后處理。因此,本文將為大家介紹如何通過SpringMVC實(shí)現(xiàn)網(wǎng)頁(yè)登錄攔截功能,需要的小伙伴可以了解一下2021-12-12
Java使用TarsosDSP庫(kù)實(shí)現(xiàn)音頻的處理和格式轉(zhuǎn)換
在音頻處理領(lǐng)域,Java雖然有原生的音頻處理類庫(kù),但其功能相對(duì)基礎(chǔ),而TarsosDSP是一個(gè)強(qiáng)大的開源音頻處理庫(kù),提供了豐富的功能,本文將介紹如何在Java中結(jié)合使用TarsosDSP庫(kù),來實(shí)現(xiàn)音頻的處理和格式轉(zhuǎn)換,需要的朋友可以參考下2025-04-04
SpringBoot啟動(dòng)類@SpringBootApplication注解背后的秘密
這篇文章主要介紹了SpringBoot啟動(dòng)類@SpringBootApplication注解背后的秘密,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心的方法
本文主要介紹了SpringBoot項(xiàng)目改為SpringCloud項(xiàng)目使用nacos作為注冊(cè)中心,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
SpringBoot 二維碼生成base64并上傳OSS的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot 二維碼生成base64并上傳OSS的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
使用java.nio.file?庫(kù)優(yōu)雅的操作文件詳解
這篇文章主要介紹了使用java.nio.file?庫(kù)優(yōu)雅的操作文件詳解,需要的朋友可以參考下2023-05-05

