SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據(jù)持久化的操作
接上回
上一篇我們簡單介紹了基于SpringBoot實現(xiàn)簡單的Web開發(fā),本節(jié)來看Web開發(fā)中必不可少的內(nèi)容——數(shù)據(jù)持久化
先看項目結(jié)構(gòu):
1. 創(chuàng)建數(shù)據(jù)表
打開mysql,打開數(shù)據(jù)庫 test (沒有可以創(chuàng)建一個),創(chuàng)建表格 person
給 person 表創(chuàng)建兩個字段 id、name
2. 打開 pom.xml,添加相關(guān)依賴
<!-- 引入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 是基礎(chǔ)的mysql驅(qū)動接口,這個也是不可或缺的
mybatis是安全、優(yōu)秀的java持久層框架,基于xml可靈活定制sql語句
mybatis-plus在mybatis的基礎(chǔ)上做了更進一步的簡化,可免去xml編寫
同時,mybatis-plus遵循非侵入式設(shè)計的原則,即完全兼容原mybatis的使用習慣,非常方便
3. 給application.properties添加數(shù)據(jù)庫配置
# mysql相關(guān)設(shè)置 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ù)據(jù)庫表名 person 一致(首字母大寫是Java的類命名規(guī)則,這個沒有問題)
id和name兩個字段的名稱和類型也要和數(shù)據(jù)庫保持一致
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特性的關(guān)鍵!
BaseMapper為我們定制常用的數(shù)據(jù)庫增刪改查的方法,直接繼承使用即可!
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ù)據(jù)模型的常見的增刪改查等操作,真的非常方便!
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è)務(wù)邏輯
這里可以看到,insert、update、delete三個寫方法我們使用了Post協(xié)議,select讀方法使用了Get協(xié)議
其實標準的RestApi風格另外還有Put和Delete協(xié)議,這里其實沒有嚴格的規(guī)定
由于Get協(xié)議的參數(shù)是直接暴露在url串里的,所以一般寫方法我們不建議使用Get協(xié)議
8. 使用Postman測試結(jié)果
我們在請求參數(shù)中分別傳入id和name,springboot框架會自動將其拼裝成Person對象,真的是非常智能化!
另外,得益于mybatis-plus免xml的特性,我們不用自己手寫任何的xml邏輯實現(xiàn),甚至通篇未出現(xiàn)任何大家常見的mybatis相關(guān)配置!
以上。
本節(jié)內(nèi)容我們介紹了數(shù)據(jù)持久化的相關(guān)操作,并且是基礎(chǔ)傳統(tǒng)的關(guān)系型數(shù)據(jù)庫——mysql
到此這篇關(guān)于SpringBoot整合mybatis/mybatis-plus實現(xiàn)數(shù)據(jù)持久化的文章就介紹到這了,更多相關(guān)SpringBoot mybatis-plus數(shù)據(jù)持久化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java連接mysql數(shù)據(jù)庫以及mysql驅(qū)動jar包下載和使用方法
這篇文章主要給大家介紹了關(guān)于Java連接mysql數(shù)據(jù)庫以及mysql驅(qū)動jar包下載和使用方法,MySQL是一款常用的關(guān)系型數(shù)據(jù)庫,它的JDBC驅(qū)動程序使得我們可以通過Java程序連接MySQL數(shù)據(jù)庫進行數(shù)據(jù)操作,需要的朋友可以參考下2023-11-11mybatis 集合嵌套查詢和集合嵌套結(jié)果的區(qū)別說明
這篇文章主要介紹了mybatis 集合嵌套查詢和集合嵌套結(jié)果的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java實現(xiàn)Excel數(shù)據(jù)驗證功能
在Java中,開發(fā)者可以使用一些開源的庫(如Apache POI)來添加、修改和處理Excel中的數(shù)據(jù),下面我們就來看看如何使用Java實現(xiàn)添加,修改和刪除Excel數(shù)據(jù)驗證吧2023-10-10淺談SpringCloud的微服務(wù)架構(gòu)組件
這篇文章主要介紹了淺談SpringCloud的微服務(wù)架構(gòu)組件,Spring Cloud根據(jù)分布式服務(wù)協(xié)調(diào)治理的需求成立了許多子項目,每個項目通過特定的組件去實現(xiàn),需要的朋友可以參考下2023-04-04詳解Java如何實現(xiàn)一個像String一樣不可變的類
說到?String?大家都知道?String?是一個不可變的類;雖然用的很多,那不知道小伙伴們有沒有想過怎么樣創(chuàng)建一個自己的不可變的類呢?這篇文章就帶大家來實踐一下,創(chuàng)建一個自己的不可變的類2022-11-11