欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot熱部署和整合Mybatis的過程

 更新時(shí)間:2023年10月16日 15:06:27   作者:會(huì)洗碗的CV工程師  
熱部署,就是在應(yīng)用正在運(yùn)行的時(shí)候升級(jí)軟件,卻不需要重新啟動(dòng)應(yīng)用,本文給大家詳細(xì)介紹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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringMVC的執(zhí)行過程淺析

    SpringMVC的執(zhí)行過程淺析

    這篇文章主要給大家介紹了關(guān)于SpringMVC的執(zhí)行過程的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用SpringMVC具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • SpringBoot使用Jackson配置全局時(shí)間日期格式

    SpringBoot使用Jackson配置全局時(shí)間日期格式

    本文主要介紹了SpringBoot使用Jackson配置全局時(shí)間日期格式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 詳解Java Spring各種依賴注入注解的區(qū)別

    詳解Java Spring各種依賴注入注解的區(qū)別

    這篇文章主要介紹了詳解Java Spring各種依賴注入注解的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • Java中數(shù)組轉(zhuǎn)List的三種方法與對(duì)比分析

    Java中數(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-07
  • jdk8?FunctionalInterface注解源碼解讀

    jdk8?FunctionalInterface注解源碼解讀

    這篇文章主要介紹了jdk8?FunctionalInterface注解源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Java實(shí)現(xiàn)動(dòng)態(tài)代理的實(shí)例代碼

    Java實(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-09
  • SpringBoot整合OpenFeign的坑

    SpringBoot整合OpenFeign的坑

    最近試用SpringBoot+K8S,遇到了個(gè)坑,通過OpenFeign請(qǐng)求返回值LocalDateTime發(fā)生了異常,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解一下
    2021-07-07
  • Java詳細(xì)講解依賴注入的方式

    Java詳細(xì)講解依賴注入的方式

    Idea中使用@Autowire注解會(huì)出現(xiàn)提示黃線,強(qiáng)迫癥患者看著很難受,使用構(gòu)造器注入或者setter方法注入后可解決,下面我們一起來看看
    2022-06-06
  • Java 高并發(fā)八:NIO和AIO詳解

    Java 高并發(fā)八:NIO和AIO詳解

    本文主要介紹Java 高并發(fā)NIO和AIO 的知識(shí),這里整理了詳細(xì)的資料,并詳細(xì)介紹了 1. 什么是NIO 2. Buffer 3. Channel 4. 網(wǎng)絡(luò)編程 5. AIO的知識(shí),有需要的小伙伴可以參考下
    2016-09-09
  • springboot集成nacos讀取nacos配置數(shù)據(jù)的原理

    springboot集成nacos讀取nacos配置數(shù)據(jù)的原理

    這篇文章主要介紹了springboot集成nacos讀取nacos配置數(shù)據(jù)的原理,文中有詳細(xì)的代碼流程,對(duì)大家學(xué)習(xí)springboot集成nacos有一定的幫助,需要的朋友可以參考下
    2023-05-05

最新評(píng)論