SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據持久化的操作
接上回
上一篇我們簡單介紹了基于SpringBoot實現(xiàn)簡單的Web開發(fā),本節(jié)來看Web開發(fā)中必不可少的內容——數(shù)據持久化
先看項目結構:

1. 創(chuàng)建數(shù)據表
打開mysql,打開數(shù)據庫 test (沒有可以創(chuàng)建一個),創(chuàng)建表格 person
給 person 表創(chuàng)建兩個字段 id、name


2. 打開 pom.xml,添加相關依賴
<!-- 引入mybatis、mybatis-plus、mysql等依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>mybatis-spring-boot-starter 滿足了 mybatis在springboot下的拆箱即用
mybatis-plus-boot-starter 實現(xiàn)了 mybatis-plus 的自動化配置,同樣拆箱即用
注意:是mybatis-plus-boot-starter,不是mybatis-plus;前者包含后者的引用,如果只引用后者執(zhí)行程序會報錯!
由于mybatis-plus是基于mybatis的,所以兩者引用缺一不可
mysql-connector-java 是基礎的mysql驅動接口,這個也是不可或缺的
mybatis是安全、優(yōu)秀的java持久層框架,基于xml可靈活定制sql語句
mybatis-plus在mybatis的基礎上做了更進一步的簡化,可免去xml編寫
同時,mybatis-plus遵循非侵入式設計的原則,即完全兼容原mybatis的使用習慣,非常方便
3. 給application.properties添加數(shù)據庫配置
# mysql相關設置 spring.datasource.username=admin spring.datasource.password=admin spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
到這里可能有人會問,咋沒看到mybatis.xml的配置?不是一般都會有一句:
#指定Mybatis的Mapper文件 mybatis.mapper-locations=classpath:mapper/*xml
如果我們使用mybatis的原生功能,這一句配置是需要加上的,但是如果我們基于mybatis-plus,可以先不加這一句,因為它是免xml配置的!
4. 新建 model/Person
package com.example.hellospringboot.model;
public class Person {
private Integer id = 0;
private String name = "";
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}注意:類名 Person 要和數(shù)據庫表名 person 一致(首字母大寫是Java的類命名規(guī)則,這個沒有問題)
id和name兩個字段的名稱和類型也要和數(shù)據庫保持一致
5. 新建 mapper/PersonMapper
package com.example.hellospringboot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.hellospringboot.model.Person;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface PersonMapper extends BaseMapper<Person> {
}這里讓PersonMapper繼承自mybatis-plus提供的BaseMapper,這是啟用mybatis-plus免xml特性的關鍵!
BaseMapper為我們定制常用的數(shù)據庫增刪改查的方法,直接繼承使用即可!
6. 新建 service/PersonService 接口及其實現(xiàn)類 service/impl/PersonServiceImpl
package com.example.hellospringboot.service;
import com.example.hellospringboot.model.Person;
import java.util.List;
public interface PersonService {
Integer insert(Person person);
Integer update(Person person);
Integer delete(int id);
List<Person> select();
}package com.example.hellospringboot.service.impl;
import com.example.hellospringboot.mapper.PersonMapper;
import com.example.hellospringboot.model.Person;
import com.example.hellospringboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
PersonMapper mapper;
public Integer insert(Person person){
return mapper.insert(person);
}
public Integer update(Person person){
return mapper.updateById(person);
}
public Integer delete(int id){
return mapper.deleteById(id);
}
public List<Person> select(){
return mapper.selectList(null);
}
}我們給mapper新增了@Repository注解,可以讓Service自動裝載Mapper不報錯
通過代碼我們可以看到,繼承自BaseMapper<Person>的PersonMapper,不加任何代碼不寫任何xml,就可以支持Person數(shù)據模型的常見的增刪改查等操作,真的非常方便!
7. 新建 controller/PersonController
package com.example.hellospringboot.controller;
import com.example.hellospringboot.model.Person;
import com.example.hellospringboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonService service;
@PostMapping("/insert")
public Integer insert(Person person){
return service.insert(person);
}
@PostMapping("/update")
public Integer update(Person person){
return service.update(person);
}
@PostMapping("/delete")
public Integer delete(int id){
return service.delete(id);
}
@GetMapping("/select")
public List<Person> select(){
return service.select();
}
}我們這里使用了@RestController注解,這樣可以非常方便的測試我們的業(yè)務邏輯
這里可以看到,insert、update、delete三個寫方法我們使用了Post協(xié)議,select讀方法使用了Get協(xié)議
其實標準的RestApi風格另外還有Put和Delete協(xié)議,這里其實沒有嚴格的規(guī)定
由于Get協(xié)議的參數(shù)是直接暴露在url串里的,所以一般寫方法我們不建議使用Get協(xié)議
8. 使用Postman測試結果






我們在請求參數(shù)中分別傳入id和name,springboot框架會自動將其拼裝成Person對象,真的是非常智能化!
另外,得益于mybatis-plus免xml的特性,我們不用自己手寫任何的xml邏輯實現(xiàn),甚至通篇未出現(xiàn)任何大家常見的mybatis相關配置!
以上。
本節(jié)內容我們介紹了數(shù)據持久化的相關操作,并且是基礎傳統(tǒng)的關系型數(shù)據庫——mysql
到此這篇關于SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據持久化的文章就介紹到這了,更多相關SpringBoot mybatis-plus數(shù)據持久化內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JDK的一個Bug監(jiān)聽文件變更的初步實現(xiàn)思路
這篇文章主要介紹了JDK的一個Bug監(jiān)聽文件變更要小心了,本篇文章就帶大家簡單實現(xiàn)一個對應的功能,并分析一下對應的Bug和優(yōu)缺點,需要的朋友可以參考下2022-05-05
Java中compareTo()和compare()方法使用及區(qū)別詳解
這篇文章主要介紹了Java中compareTo()和compare()方法使用及區(qū)別的相關資料,compareTo()方法用于定義類的自然排序,適用于具有單一、固定排序方式的場景,compare()方法提供自定義排序的靈活性,適用于需要根據不同規(guī)則對對象進行排序的場景,需要的朋友可以參考下2025-01-01
了解java中的Clojure如何抽象并發(fā)性和共享狀態(tài)
Clojure是一種運行在Java平臺上的 Lisp 方言,Lisp是一種以表達性和功能強大著稱的編程語言,但人們通常認為它不太適合應用于一般情況,而Clojure的出現(xiàn)徹底改變了這一現(xiàn)狀。,需要的朋友可以參考下2019-06-06
解決mybatis返回boolean值時數(shù)據庫返回null的問題
這篇文章主要介紹了解決mybatis返回boolean值時數(shù)據庫返回null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
java使用compareTo實現(xiàn)一個類的對象之間比較大小操作
這篇文章主要介紹了java使用compareTo實現(xiàn)一個類的對象之間比較大小操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
詳解springboot項目帶Tomcat和不帶Tomcat的兩種打包方式
這篇文章主要介紹了詳解springboot項目帶Tomcat和不帶Tomcat的兩種打包方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Jmeter接口登錄獲取參數(shù)token報錯問題解決方案
這篇文章主要介紹了Jmeter接口登錄獲取參數(shù)token報錯問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07

