SpringBoot熱部署和整合Mybatis的過程
一、SpringBoot熱部署
熱部署,就是在應(yīng)用正在運(yùn)行的時(shí)候升級(jí)軟件,卻不需要重新啟動(dòng)應(yīng)用。即修改完代碼后不需要重啟項(xiàng)目即可生效。在SpringBoot中,可以使用DevTools工具實(shí)現(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è)置自動(dòng)編譯
點(diǎn)擊 File-->Settings
如上圖,勾選上。
1.3 在Idea設(shè)置自動(dòng)運(yùn)行
快捷鍵 Ctrl+Shift+Alt+/ 后點(diǎn)擊 Registry ,勾選complier.automake.allow.when.app.running
然后我們來測(cè)試一下,運(yùn)行項(xiàng)目,然后在運(yùn)行時(shí)往/show2路徑修改輸出看看是否不用重啟項(xiàng)目也能發(fā)生改變。
修改之后,在控制臺(tái)可以看到:重新運(yùn)行了一下項(xiàng)目。
并且再次訪問可以出現(xiàn)我們新添加的數(shù)據(jù)
則說明我們的熱部署生效
二、SpringBoot整合Mybatis
Spring整合MyBatis時(shí)需要進(jìn)行大量配置,而SpringBoot整合MyBatis則可以簡(jiǎn)化很多配置:
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ū)動(dòng)和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 在配置文件進(jìn)行數(shù)據(jù)源配置
然后在配置文件進(jìn)行如下配置,配置數(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文件
然后新建一個(gè)mapper包,里面新建一個(gè)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目錄下新建一個(gè)與StudentMapper同級(jí)目錄和同名的.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 測(cè)試
OK,從上面我們已經(jīng)新建了一個(gè)查詢所有的方法啊,現(xiàn)在在測(cè)試類我們看看能否成功獲取數(shù)據(jù)庫信息。測(cè)試類代碼如下:
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot在idea下debug調(diào)試熱部署問題
- IntelliJ?IDEA2022.3?springboot?熱部署含靜態(tài)文件(最新推薦)
- Idea2022版本配置SpringBoot熱部署的教程
- springboot基于IDEA環(huán)境熱加載與熱部署教程
- SpringBoot整合Mybatis-Plus+Druid實(shí)現(xiàn)多數(shù)據(jù)源配置功能
- SpringBoot?整合Mybatis-Plus并輸出SQL日志示例詳解
- IDEA創(chuàng)建SpringBoot項(xiàng)目整合mybatis時(shí)mysql-connector-java報(bào)錯(cuò)異常的詳細(xì)分析
相關(guān)文章
SpringBoot使用Jackson配置全局時(shí)間日期格式
本文主要介紹了SpringBoot使用Jackson配置全局時(shí)間日期格式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析
這篇文章主要給大家介紹了關(guān)于Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析的相關(guān)資料,分別介紹了最常見方式、數(shù)組轉(zhuǎn)為L(zhǎng)ist后,支持增刪改查的方式以及通過集合工具類Collections.addAll()方法,需要的朋友可以參考下2018-07-07jdk8?FunctionalInterface注解源碼解讀
這篇文章主要介紹了jdk8?FunctionalInterface注解源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java實(shí)現(xiàn)動(dòng)態(tài)代理的實(shí)例代碼
代理模式是常用的java設(shè)計(jì)模式,他的特征是代理類與委托類有同樣的接口,代理類主要負(fù)責(zé)為委托類預(yù)處理消息、過濾消息、把消息轉(zhuǎn)發(fā)給委托類,以及事后處理消息等,這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)動(dòng)態(tài)代理的相關(guān)資料,需要的朋友可以參考下2021-09-09springboot集成nacos讀取nacos配置數(shù)據(jù)的原理
這篇文章主要介紹了springboot集成nacos讀取nacos配置數(shù)據(jù)的原理,文中有詳細(xì)的代碼流程,對(duì)大家學(xué)習(xí)springboot集成nacos有一定的幫助,需要的朋友可以參考下2023-05-05