• SpringBoot保姆级教程(八)热部署 & 整合MyBatis


    目录

    一.热部署

    二.整合MyBatis 


    一.热部署

    热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用。即修改完代码(后端或前端代码都可以)后不需要重启项目即可生效。在SpringBoot中,可以使用DevTools 工具实现热部署
    1
    1.添加 DevTools 依赖
    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-devtoolsartifactId>
    4. dependency>

    添加后报红就刷新Maven。

    2.idea中设置自动编译

    点击 File -- >Settings
    找到Compiler,勾选Build project automatically

    3.在Idea设置自动运行

    快捷键 Ctrl+Shift+Alt+/ 后点击 Registry ,勾选 complier.automake.allow.when.app.running

    但是complier.automake.allow.when.app.running这个选项在IntelliJ IDEA 2021.2之后的版本迁移到高级设置中,如下

    此时热部署即可生效 。 

    二.整合MyBatis 

    Spring 整合 MyBatis 时需要进行大量配置,而 SpringBoot 整合MyBatis则可以简化很多配置:
    1
    1.准备数据库数据
    1. CREATE DATABASE student;
    2. USE student;
    3. DROP TABLE IF EXISTS student;
    4. CREATE TABLE student (
    5. id int(11) NOT NULL AUTO_INCREMENT,
    6. stuName varchar(255) DEFAULT NULL,
    7. sex varchar(10) DEFAULT NULL,
    8. stuAddress varchar(255) DEFAULT NULL,
    9. PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    10. insert into student(id,stuName,sex,stuAddress) values(1,'小明','男','北京'),(2,'尚尚','女','北京');

    2.创建SpringBoot项目,添加MyBatis起步依赖和Mysql驱动依赖

    除了SpringMVC(在Web模块)的依赖,还要添加Mybatis和MySQL驱动的依赖

    测试依赖是默认自动引入的,不需要手动引入

    以下步骤完了之后的项目结构:

    3.编写实体类

    1. public class Student {
    2. private int id;
    3. private String stuName;
    4. private String sex;
    5. private String stuAddress;
    6. //省略get/set/toString方法
    7. }

    4.编写Mapper接口

    1. import com.first.springbootmybatis.domian.Student;
    2. import org.apache.ibatis.annotations.Mapper;
    3. import java.util.List;
    4. @Mapper
    5. public interface StudentMapper {
    6. List findAll();
    7. }

    5.编写Mapper映射文件

    要在resources下创建StudentMapper接口的映射文件,要和该接口在java文件下路径一致,且要一层一层的创建。即先创com,再创first,再创springbootmybatis

    1. "1.0" encoding="UTF-8"?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.first.springbootmybatis.mapper.StudentMapper">
    6. <select id="findAll" resultType="student">
    7. select * from student
    8. select>
    9. mapper>

    6.编写配置文件

    创建application.yml

    1. # 数据源
    2. spring:
    3. datasource:
    4. driver-class-name: com.mysql.cj.jdbc.Driver
    5. url: jdbc:mysql:///student?serverTimezone=UTC
    6. username: root
    7. password: root
    8. # mybatis配置
    9. mybatis:
    10. # 映射文件位置
    11. mapper-locations: com/first/springbootmybatis/mapper/*Mapper.xml
    12. # 使别名domain下的文件都可以使用别名
    13. type-aliases-package: com.first.springbootmybatis.domain
    14. #日志格式
    15. logging:
    16. pattern:
    17. console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'

    7.编写测试类

    在test文件下创建,记得路径也要对应

    1. import com.first.springbootmybatis.domian.Student;
    2. import org.junit.jupiter.api.Test;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.boot.test.context.SpringBootTest;
    5. import java.util.List;
    6. // 测试类注解,可以在运行测试代码时加载Spring容 器
    7. @SpringBootTest
    8. public class StudentMapperTest {
    9. @Autowired
    10. private StudentMapper studentMapper;
    11. @Test
    12. public void testFindAll(){
    13. List all = studentMapper.findAll();
    14. all.forEach(System.out::println);
    15. }
    16. }

    运行测试类查看是否查询到了所有学生的信息。

  • 相关阅读:
    人工智能基础 作业6
    Centos7.4重启提示gurb2/i386-pc/normal.mod not found
    Java项目:JSP网上在线酒类商城系统网站
    Python: 数据类型转换总结(list-np.array-torch.tensor)
    2022 杭电多校 第一场
    计算机组成原理——总结
    【java】【SpringBoot】【四】原理篇 bean、starter、核心原理
    【计算机网络】VLAN原理和配置
    分析Java 8中 Objects 类源码
    动态内存管理(malloc free calloc realloc)
  • 原文地址:https://blog.csdn.net/weixin_44593822/article/details/127134346