1. SpringBoot進階用法
1.1 Maven坐标查詢
網址: https://mvnrepository.com/
可以現在查找maven包的資訊
1.2 Lombok插件
1.3 添加jar封包件
<!--添加lombok依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
1.4 Lombok使用
作用: 通過程式自動生成實體對象的get/set/toString/equals/hashCode等方法.
鍊式加載原理: 重新開機了POJO的set方法. 傳回目前對象
public User setId(Integer id) {
this.id = id;
return this;
}
1.5 關于Lombok面試問題(初級)
問題: lombok的使用需要在IDE中提前安裝插件!!!,如果項目在Linux系統中部署釋出.是否需要提前安裝插件!!!
答案: 不要!!!
原因: lombok插件編譯期有效.(編譯期:由xxx.java檔案編譯為xxxx.class檔案).在打包之前class檔案中已經包含了set/get等方法,是以項目打包之後可以直接運作.無需安裝插件!!!.
2. SpringBoot整合Mybatis
2.1 導入資料庫
檢查資料庫是否可用
說明: 正常的情況下mysql服務項 開機自啟. 有時由于某種原因 導緻資料庫服務啟動失敗.
問題描述: 資料庫連結報錯.顯示連結不可用.
檢查服務項:
資料庫版本: 使用mariadb, 不要使用Mysql 5.8
2.2 資料庫導入和導出
導出資料庫: 将mysql中的資料庫以 xxx.sql檔案進行轉儲.
導入資料庫: 讀取xxx.sql檔案 之後工具執行其中的sql,最終實作資料的導入功能.
說明: 上述的操作稱之為資料庫冷備份. 一般在生産環境下 為了保證資料的安全.一般都會定期冷備份.(周期3-7天左右) 一般一式3份. 資料庫的冷備份是恢複資料最後有效的手段.
特點: 冷備份容易丢失資料. 熱備份可以實作實時備份.
2.3 建立項目
建立maven項目
pom.xml檔案 添加jar包
<!--<parent>往後放,友善一起複制-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<!--跳過測試類打包-->
<skipTests>true</skipTests>
</properties>
<!--依賴:按需導入-->
<dependencies>
<!--啟動項 web啟用springMVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<!--springboot啟動項(器)在包的内部SpringBoot
已經完成了項目的整合(配置)使用者拿來就用
-->
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok依賴-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入資料庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springBoot資料庫連接配接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring整合mybatis 暫時 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
<!--SpringBoot項目與Maven整合的一個插件
可以通過插件 執行項目打包/測試/文檔生成等操作
注意事項:該插件不能省略
如果省略啟動時報錯:利用指令啟動時(項目釋出時) java -jar xxxx.jar
沒有主清單資訊
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.4 編輯主啟動類
package com.jt;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.jt.mapper")//Spring 容器内部為接口建立代理對象
//JDK的動态代理
public class SpringBootRun {
public static void main(String[] args) {
SpringApplication.run(SpringBootRun.class,args);
}
}
2.5 編輯POJO實體對象
說明: 一般實體對象隻需要添加get/set/toString等方法,無需添加構造方法.
package com.jt.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
//@Component 将user對象交給Spring容器管理
//實體對象的作用 就是用來實作參數的傳遞
public class User implements Serializable {
//序列化:保證資料傳輸完整性
private Integer id;
private String username;
private String password;
private String phone;
private String email;
private Integer status;
private Date datetime;
private Date date;
}
2.6序列化作用
一般如果需要對象進行傳遞時,要求POJO對象必須實作序列化接口.否則資料傳輸必然報錯.
2.7整合Mybatis
步驟:
1.添加jar封包件依賴
2.SpringBoot整合Mybatis 添加配置檔案 1.連接配接資料庫 2.SpringBoot整合Mybatis
3.建立Mapper接口
4.建立XML映射檔案.
-1 導入jar封包件
1.mybatis包
2.資料庫驅動包
3.JDBC包
-2 關于資料源配置application.yml
serverTimezone=GMT%2B8& %2B= + GMT+8& 時區
useUnicode=true&characterEncoding=utf8& 是否使用unicode編碼及設定字元集
autoReconnect=true 是否自動重連
allowMultiQueries=true 是否允許批量操作
server:
port: 8090
# No newline at end of file
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
-3 Mybatis配置檔案
server:
port: 8090
# No newline at end of file
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
#如果資料庫密碼以數字0開頭 則必須使用""号包裹
#password: "01234"
#SpringBoot整合Mybatis配置
mybatis:
#定義别名包:實作映射對象
type-aliases-package: com.jt.pojo
#加載 映射檔案,一個接口對應一個映射檔案
mapper-locations: classpath:/mybatis/*.xml
#開啟駝峰映射
configuration:
map-underscore-to-camel-case: true
#是否打日志
debug: false
-4 編輯UserMapper接口/映射檔案
1.編輯UserMapper接口
package com.jt.Mapper;
import com.jt.pojo.User;
import java.util.List;
public interface UserMapper1 {
List<User> getAll();
}
.編輯UserMapper.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">
<!--namespace必須與接口一一對應, 一個映射檔案對應一個接口-->
<mapper namespace="com.jt.Mapper.UserMapper1">
<select id="getAll" resultType="User">
select * from user
</select>
<!--駝峰命名規則
表字段: user_id,user_name
對象的屬性: userId,userName
resultType: 保證屬性與字段名稱必須一緻.
Mybatis提供了駝峰命名規則:
規則: 字
段user_id~~~去除_線~~~之後映射對象的屬性
userId-->
</mapper>
-5 Spring容器管理Mapper接口
說明: 利用@mapperScan注解為接口建立代理對象,
2.4 編輯主啟動類 中已添加
-6編輯測試案例
package com.jt;
import com.jt.Mapper.UserMapper1;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**要求:
* 1.測試包路徑必須在主啟動類的同包及子包中
* 2.從Spring容器中擷取并調用對象
*/
@SpringBootTest
public class TestMybatis {
/**需求:需要動态注入UseMapper接口,之後實作getAll的測試!!!
* */
@Autowired
private UserMapper1 u1;
@Test
public void testget(){
System.out.println(u1.getClass());
List<User> list=u1.getAll();
for (User i: list){
System.out.println(i);
}
}
}
3.SpringBoot整合作業
1.将SpringBoot整合Mybatis 寫2遍 springboot_demo2_mybatis2/mybatis3
2.練習Mybatis sql操作
1.根據Id=1 查詢使用者
2.根據name=“王昭君” 和sex=“女” 查詢資料.
3.利用模糊查詢: 查詢name包含"精"的資料.
4.查詢demo_user表中的資料要求按照年齡排序,如果年齡相同按照性别排序.
5.根據name和age動态查詢查詢資料 如果name和age不為null則拼接where條件
中間使用 and連結
Sql: select * from demo_user where name=“xxx” and age=“xxx”
6.查詢ID=1,3,4,6,7的資料