網站首頁 編程語言 正文
一. spring boot簡介
springboot特點:基于編程式和注解式配置,習慣(約定)優于配置。
springboot優勢:
①:springboot讓配置更簡單
springboot推出了starter組件,即自動配置器,我們只需要在應用中引入相關組件的starter即可完成配置,從spring應用中復雜而繁瑣的配置中解脫出來,實現項目的快速搭建。
springboot官方提供的starter命名格式為spring-boot-starter-×××
第三方提供的starter命名格式為×××-spring-boot-starter
②:springboot讓開發更簡單
springboot支持內嵌容器,如tomcat,jetty,應用自帶服務器,直接啟動應用即可,無需額外安裝服務器。
springboot提供了強大的開發工具包,支持熱啟動。
③:springboot讓測試更簡單
springboot內置了常用的測試框架,僅需引入一個spring-boot-starter-test依賴包,即可進行各種測試
④:springboot讓部署更加簡單
springboot應用默認打包jar包
⑤:springboot讓監控更簡單
springboot就是一款自帶監控的框架,專門提供了監控組件來完成這個工作。
眺望springcloud:
spring為應對互聯網應用的要求(高可靠,高并發,能夠負載均衡等),基于springboot推出了微服務框架springcloud,因此springcloud完全建立在springboot之上。
二. 開發一個springboot應用,引入依賴
<!--引入springboot父項目:該父項目中內置了很多習慣性配置和jar版本號之間的關聯-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.4</version><!--此處版本號可以省略,因為springboot父項目中已經內置配置好了-->
</dependency>
<!--引入springboot開發工具包,可以支持熱啟動-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 引入mybatis的starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!--引入springboot測試支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
<!--引入springboot構建插件,用于打包生成可執行jar文件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.4</version>
</plugin>
</plugins>
</build>
parent:spring-boot-starter-parent:引入springboot父項目:該父項目中內置了很多習慣性配置和jar版本號之間的關聯
dependencies:
①:spring-boot-starter-web:web組件
②:spring-boot-devtools:引入springboot開發工具包,可以支持熱啟動
③:mybatis-spring-boot-starter
④:mysql-connector-java
⑤:spring-boot-starter-test:springboot測試組件
⑥:pagehelper-spring-boot-starter:分頁組件
build:
spring-boot-maven-plugin:引入springboot構建插件,用于打包生成可執行jar文件
三. 開發springboot啟動類
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//說明本應用為springboot應用,
// 而且還說明這是一個springboot的啟動類,同時還說明這是一個配置類
//而且還是一個自動配置類
//主類所在的包,就是springboot應用的根包
//在springboot應用中,一般所有的組件如:controller,service,和dao等對象都應該在應用根包下或其子包下
@SpringBootApplication
//自動掃描映射器,并放入spring容器
@MapperScan("org.xupt.ygq.demo.dao")
public class MyApp {
public static void main(String[] args) {
//該語句為springboot應用的啟動語句
SpringApplication.run(MyApp.class,args);
}
}
兩個注解:
①:@SpringBootApplication:
說明本應用為springboot應用,而且還說明這是一個springboot的啟動類,同時還說明這是一個配置類。
主類所在的包,就是springboot應用的根包,在springboot應用中,一般所有的組件如:controller,service,和dao,等對象都應該在應用根包或者根包的子包下面
②:@MapperScan:自動掃描映射器(也就是dao包),并存放在spring容器中
一條語句:
SpringApplication.run(MyApp.class,args);啟動語句!!!
四. 配置application.properties資源文件
#配置內置服務器端口號,默認為8080
server.port=9999
#配置根日志的輸出級別,默認為info
logging.level.root=info
#配置org.xupt.ygq.demo.dao包下的日志輸出級別為trace,可以打印mybatis映射器執行結果
logging.level.org.xupt.ygq.demo.dao=trace
logging.level.org.xupt.ygq.demo=debug
#配置springmvc的日志
logging.level.web=trace
#配置數據源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/wddb
spring.datasource.username=root
spring.datasource.password=123456
#配置mybatis
#配置映射文件位置,在類路徑下,mapper目錄下的任意層級文件夾下的以.xml結尾的文件
mybatis.mapper-locations=classpath:mapper/**/*.xml
#pagehelper
#配置方言,即配置使用哪一個數據庫
pagehelper.helper-dialect=mysql
#分頁的合理化配置,即如果請求頁碼不合理時,自動合理化,比如:請求-9頁,自動合理化成第一頁
pagehelper.reasonable=true
五.因為要做分頁查詢,封裝分頁
?5.1 分頁類
import java.util.List;
public class Page<T> {
//當前頁
private Integer current;
//首頁
private Integer first;
//上頁
private Integer pre;
//下頁
private Integer next;
//尾頁
private Integer last;
//每頁最大記錄數
private Integer pageSize;
//總記錄數
private Long total;
//總頁數
private Integer pages;
//當前頁實際記錄數
private Integer currSize;
//當前頁數據記錄
private List<T> list;
public Integer getCurrent() {
return current;
}
public void setCurrent(Integer current) {
this.current = current;
}
public Integer getFirst() {
return first;
}
public void setFirst(Integer first) {
this.first = first;
}
public Integer getPre() {
return pre;
}
public void setPre(Integer pre) {
this.pre = pre;
}
public Integer getNext() {
return next;
}
public void setNext(Integer next) {
this.next = next;
}
public Integer getLast() {
return last;
}
public void setLast(Integer last) {
this.last = last;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public Integer getCurrSize() {
return currSize;
}
public void setCurrSize(Integer currSize) {
this.currSize = currSize;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
@Override
public String toString() {
return "Page{" +
"current=" + current +
", first=" + first +
", pre=" + pre +
", next=" + next +
", last=" + last +
", pageSize=" + pageSize +
", total=" + total +
", pages=" + pages +
", currSize=" + currSize +
", list=" + list +
'}';
}
}
類定義成泛型的:因為查詢到的結果list,里面裝的對象是什么,是不確定的。
5.2 分頁參數
public class PageParam {
private Integer pageNum=1;//請求頁碼
private Integer pageSize=5;//每頁最大記錄數
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
在進行分頁之前要先設置分頁參數(包含兩個屬性,頁碼pageName和每頁記錄數pageSize)
PageHelper.startPage(pageParam);
5.3 分頁查詢api
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.xupt.ygq.demo.common.page.Page;
import org.xupt.ygq.demo.common.page.PageParam;
import org.xupt.ygq.demo.common.page.QueryAction;
import java.util.List;
public class Utils {
public static <T> Page<T> getPage(PageParam pageParam, QueryAction<T> queryAction){
//在查詢之前設置分頁參數,再執行查詢和設置分頁參數之間不能有其他的查詢。
//該方法用于設置分頁參數對象(含有頁碼pageName和每頁記錄數pageSize兩個屬性)
PageHelper.startPage(pageParam);
List<T> list = queryAction.executeQuery();//執行一個查詢,獲得List集合
//緊挨在查詢之后構造分頁信息對象
PageInfo<T> pageInfo = new PageInfo<>(list);
Page<T> page = new Page<>();
page.setCurrent(pageInfo.getPageNum());//當前頁
page.setFirst(1);//首頁
page.setPre(pageInfo.getPrePage());//上一頁
page.setNext(pageInfo.getNextPage());//下一頁
page.setLast(pageInfo.getPages());//最后一頁
page.setPageSize(pageInfo.getPageSize());//每頁最大記錄數
page.setTotal(pageInfo.getTotal());//總記錄數
page.setPages(pageInfo.getPages());//總頁數
page.setCurrSize(pageInfo.getSize());//當前頁實際記錄數
page.setList(pageInfo.getList());//當前頁數據記錄
return page;
}
}
定義一個泛型生成分頁對象的泛型類:(兩個參數)
①:PageParame:聲明分頁參數,
②:QueryAction:一個查詢行為的接口,表示要進行分頁的查詢行為
其中要注意分頁信息對象:PageInfo:我們對于自己的分頁對象的設置基于分頁信息對象
最后返回一個分頁對象:
5.4 定義抽象的查詢行為
import java.util.List;
/*
* 表示抽象的查詢的行為,該查詢將獲得一個List集合
* */
public interface QueryAction<T> {
public List<T> executeQuery();
}
5.5 封裝返回結果
public class Result<T> {
public static final int CODE_OK = 200;
public static final int CODE_ERR_BUSINESS = 500;
public static final int CODE_ERR_SYS = 530;
public static final int CODE_ERR_UNLOGINED = 520;
public static <T>Result<T> ok(){
return new Result(true,CODE_OK,null,null);
}
public static <T>Result<T> ok(String message){
return new Result(true,CODE_OK,message,null);
}
public static <T>Result<T> ok(T data){
return new Result(true,CODE_OK,null,data);
}
public static <T>Result<T> ok(String message,T data){
return new Result(true,CODE_OK,message,data);
}
public static <T>Result<T> err(int errCode ,String message){
return new Result(false,errCode,message,null);
}
public static <T>Result<T> err(int errCode ,String message,T data){
return new Result(false,errCode,message,data);
}
private boolean success;//是否成功
private int code;//200 成功 500 業務失敗,530 系統錯誤,520 未登錄
private String message;//概要信息
private T data;
public Result(boolean success, int code, String message, T data) {
this.success = success;
this.code = code;
this.message = message;
this.data = data;
}
public boolean isSuccess() {
return success;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
}
六. 開發model和dto
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class Employee {
private String e_id;
private Integer d_id;//部門編號
private Integer e_sex;
private String e_name;
private Date e_birth;
public String getE_id() {
return e_id;
}
public void setE_id(String e_id) {
this.e_id = e_id;
}
public Integer getD_id() {
return d_id;
}
public void setD_id(Integer d_id) {
this.d_id = d_id;
}
public Integer getE_sex() {
return e_sex;
}
public void setE_sex(Integer e_sex) {
this.e_sex = e_sex;
}
public String getE_name() {
return e_name;
}
public void setE_name(String e_name) {
this.e_name = e_name;
}
//聲明生成json的時間格式和時區
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT=8")
public Date getE_birth() {
return e_birth;
}
public void setE_birth(Date e_birth) {
this.e_birth = e_birth;
}
@Override
public String toString() {
return "Employee{" +
"e_id='" + e_id + '\'' +
", d_id=" + d_id +
", e_sex=" + e_sex +
", e_name='" + e_name + '\'' +
", e_birth=" + e_birth +
'}';
}
}
import java.util.Date;
//表示封裝查詢條件的dto
public class EmployeeQueryDto extends PageParam {
private String e_id;
private Integer d_id;//部門編號
private Integer e_sex;
private String e_name;
private Date e_birth_start;
private Date e_birth_end;
public String getE_id() {
return e_id;
}
public void setE_id(String e_id) {
this.e_id = e_id;
}
public Integer getD_id() {
return d_id;
}
public void setD_id(Integer d_id) {
this.d_id = d_id;
}
public Integer getE_sex() {
return e_sex;
}
public void setE_sex(Integer e_sex) {
this.e_sex = e_sex;
}
public String getE_name() {
return e_name;
}
public void setE_name(String e_name) {
this.e_name = e_name;
}
@DateTimeFormat(pattern = "yyyy-MM-dd")//對前端傳來的時間格式說明
public Date getE_birth_start() {
return e_birth_start;
}
public void setE_birth_start(Date e_birth_start) {
this.e_birth_start = e_birth_start;
}
public Date getE_birth_end() {
return e_birth_end;
}
@DateTimeFormat(pattern = "yyyy-MM-dd")//對前端傳來的時間格式說明
public void setE_birth_end(Date e_birth_end) {
this.e_birth_end = e_birth_end;
}
}
dto繼承了PageParam:繼承了他的分頁參數,方便一起從前端頁面傳回數據。這樣子就可以把dto和分頁參數融合在一起進行參數傳遞。
七. 開發控制器組件
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xupt.ygq.demo.common.Result;
import org.xupt.ygq.demo.common.page.Page;
import org.xupt.ygq.demo.dto.EmployeeQueryDto;
import org.xupt.ygq.demo.model.Employee;
import org.xupt.ygq.demo.service.EmpService;
@RestController
public class MyController {
@Autowired
private EmpService empService;
@GetMapping("/emp")
//分頁查詢需要分頁參數,dto繼承了分頁參數類,所以他里面就有分頁參數
public Result empList(EmployeeQueryDto dto){
Page<Employee> page = empService.getEmpPage(dto);
return Result.ok(page);
}
}
三個注解:
①:RestController
②:Autowired
③:GetMapping
定義處理方法:傳遞參數
八. 開發業務對象
import org.xupt.ygq.demo.common.page.Page;
import org.xupt.ygq.demo.common.page.PageParam;
import org.xupt.ygq.demo.model.Employee;
public interface EmpService {
public Page<Employee> getEmpPage(PageParam pageParam);
}
?這里為什么用PageParam接受呢,因為dto對象時PageParam的子類,所以用父類型接收。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xupt.ygq.demo.common.Utils;
import org.xupt.ygq.demo.common.page.Page;
import org.xupt.ygq.demo.common.page.PageParam;
import org.xupt.ygq.demo.dao.EmpDao;
import org.xupt.ygq.demo.model.Employee;
import org.xupt.ygq.demo.service.EmpService;
@Service//表示本對象是業務對象,并且受spring容器管理
@Transactional//聲明本業務對象的所有方法都是事務性的
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpDao empDao;
/*
* pageHelper是一個適用于MyBatis的分頁插件,通過該插件可以很方便的實現分頁查詢
* */
/*@Override
public Page<Employee> getEmpPage(PageParam pageParam) {
QueryAction<Employee> action = new QueryAction<Employee>() {
@Override
public List<Employee> executeQuery() {
return empDao.findEmpList();
}
} ;
return Utils.getPage(pageParam,action);
}*/
// @Override
// public Page<Employee> getEmpPage(PageParam pageParam) {
// QueryAction<Employee> action =()->{
// return empDao.findEmpList();
// };
//
// return Utils.getPage(pageParam,action);
// }
@Override
public Page<Employee> getEmpPage(PageParam pageParam) {
//QueryAction<Employee> action =()-> empDao.findEmpList();
return Utils.getPage(pageParam,()-> empDao.findEmpList(pageParam));
}
}
實現類里面有一個重要的點:就是調用分頁查詢api的時候,參數QueryAction接口的實現是內部類,再加上接口中只有一個方法,所以可以簡化書寫
九. 開發持久層
import org.xupt.ygq.demo.common.page.PageParam;
import org.xupt.ygq.demo.model.Employee;
import java.util.List;
public interface EmpDao {
//@Select("select * from t_emp")
public List<Employee> findEmpList(PageParam pageParam);
}
持久層返回的List在Service實現中被封裝成了page對象。
十. 開發mapper
<?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">
<mapper namespace="org.xupt.ygq.demo.dao.EmpDao">
<!-- 需求: 通過給定屬性 查詢用戶 -->
<select id="findEmpList" resultType= "org.xupt.ygq.demo.model.Employee">
select * from t_emp
<where>
<!--<include refid="sql_query_where"></include>-->
<if test="e_id!=null and e_id!='' ">
and e_id like concat('%',#{e_id},'%')
</if>
<if test="e_name!=null and e_name!='' ">
and e_name like concat('%',#{e_name},'%')
</if>
<if test="e_sex!=null ">
and e_sex=#{e_sex}
</if>
<if test="d_id!=null ">
and d_id=#{d_id}
</if>
<if test="e_birth_start!=null ">
and e_date >= #{e_date_start}
</if>
<if test="e_birth_end!=null ">
and e_date <= #{e_date_end}
</if>
</where>
</select>
</mapper>
整個分頁由插件完成!!!
mapper需要在application.properties中配置映射路徑。
還需要注意映射的包:
?
十一. 測試?
?
原文鏈接:https://blog.csdn.net/weixin_45836787/article/details/125895956
相關推薦
- 2022-01-14 path.join()和path.resolve()區別
- 2022-03-31 python常用內置模塊你了解嗎_python
- 2023-08-15 antdv Input組件maxLength屬性設置默認值
- 2022-09-01 Android使用Intent傳遞組件大數據_Android
- 2021-11-26 Redis分布式鎖詳細介紹_Redis
- 2022-06-01 C語言的動態內存管理你了解嗎_C 語言
- 2022-06-29 Python?高級教程之線程進程和協程的代碼解析_python
- 2022-10-25 Git中bundle命令的使用詳解_相關技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支