SpringBoot熱部署和整合Mybatis的過程
一、SpringBoot熱部署
熱部署,就是在應(yīng)用正在運行的時候升級軟件,卻不需要重新啟動應(yīng)用。即修改完代碼后不需要重啟項目即可生效。在SpringBoot中,可以使用DevTools工具實現(xiàn)熱部署
1.1 添加DevTools依賴
首先我們需要在pom文件中引入devtools的依賴,如下:
<!-- 熱部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
1.2 在idea中設(shè)置自動編譯
點擊 File-->Settings

如上圖,勾選上。
1.3 在Idea設(shè)置自動運行
快捷鍵 Ctrl+Shift+Alt+/ 后點擊 Registry ,勾選complier.automake.allow.when.app.running

然后我們來測試一下,運行項目,然后在運行時往/show2路徑修改輸出看看是否不用重啟項目也能發(fā)生改變。

修改之后,在控制臺可以看到:重新運行了一下項目。

并且再次訪問可以出現(xiàn)我們新添加的數(shù)據(jù)

則說明我們的熱部署生效
二、SpringBoot整合Mybatis
Spring整合MyBatis時需要進行大量配置,而SpringBoot整合MyBatis則可以簡化很多配置:
2.1 準(zhǔn)備數(shù)據(jù)
-- ---------------------------- -- Table structure for student -- ---------------------------- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES (1, 'LYL', '男', '廣州'); INSERT INTO `student` VALUES (2, 'HQX', '女', '揭陽');
添加pojo類:
package com.example.springbootdemo3.pojo;
public class Student {
private int id;
private String name;
private String sex;
private String address;
public Student() {
}
public Student(int id, String name, String sex, String address) {
this.id = id;
this.name = name;
this.sex = sex;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
" ]";
}
}2.2 添加相關(guān)依賴
那么這里我們需要添加Mysql驅(qū)動和Mabatis依賴
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>2.3 在配置文件進行數(shù)據(jù)源配置
然后在配置文件進行如下配置,配置數(shù)據(jù)源和sql日志輸出
# 配置數(shù)據(jù)源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///student?serverTimezone=UTC
username: root
password: 666666
# mybatis配置
mybatis:
# 映射文件位置
mapper-locations: com/example/springbootdemo3/mapper/*Mapper.xml
# 別名
type-aliases-package: com.example.springbootdemo3.pojo
#日志格式
logging:
pattern:
console: '%d{YYYY-MM-dd HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread]%cyan(%-50logger{50}):%msg%n'2.4 編寫Mapper接口和Mapper文件
然后新建一個mapper包,里面新建一個StudentMapper接口
package com.example.springbootdemo3.mapper;
import com.example.springbootdemo3.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface StudentMapper {
List<Student> findAll();
}
這里還要在resources目錄下新建一個與StudentMapper同級目錄和同名的.xml文件

內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootdemo3.mapper.StudentMapper">
<select id="findAll" resultType="com.example.springbootdemo3.pojo.Student">
select * from student;
</select>
</mapper>
2.5 測試
OK,從上面我們已經(jīng)新建了一個查詢所有的方法啊,現(xiàn)在在測試類我們看看能否成功獲取數(shù)據(jù)庫信息。測試類代碼如下:
package com.example.springbootdemo3.mapper;
import com.example.springbootdemo3.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class StudentMapperTest {
@Autowired
private StudentMapper studentMapper;
@Test
public void testFindAll(){
List<Student> students = studentMapper.findAll();
students.forEach(System.out::println);
}
}OK,可以看到也是能夠成功獲取信息的。

到此這篇關(guān)于SpringBoot熱部署和整合Mybatis的文章就介紹到這了,更多相關(guān)SpringBoot熱部署內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot在idea下debug調(diào)試熱部署問題
- IntelliJ?IDEA2022.3?springboot?熱部署含靜態(tài)文件(最新推薦)
- Idea2022版本配置SpringBoot熱部署的教程
- springboot基于IDEA環(huán)境熱加載與熱部署教程
- SpringBoot整合Mybatis-Plus+Druid實現(xiàn)多數(shù)據(jù)源配置功能
- SpringBoot?整合Mybatis-Plus并輸出SQL日志示例詳解
- IDEA創(chuàng)建SpringBoot項目整合mybatis時mysql-connector-java報錯異常的詳細(xì)分析
相關(guān)文章
Java中數(shù)組轉(zhuǎn)List的三種方法與對比分析
這篇文章主要給大家介紹了關(guān)于Java中數(shù)組轉(zhuǎn)List的三種方法與對比分析的相關(guān)資料,分別介紹了最常見方式、數(shù)組轉(zhuǎn)為List后,支持增刪改查的方式以及通過集合工具類Collections.addAll()方法,需要的朋友可以參考下2018-07-07
jdk8?FunctionalInterface注解源碼解讀
這篇文章主要介紹了jdk8?FunctionalInterface注解源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
springboot集成nacos讀取nacos配置數(shù)據(jù)的原理
這篇文章主要介紹了springboot集成nacos讀取nacos配置數(shù)據(jù)的原理,文中有詳細(xì)的代碼流程,對大家學(xué)習(xí)springboot集成nacos有一定的幫助,需要的朋友可以參考下2023-05-05

