SpringBoot使用MyBatis實(shí)現(xiàn)數(shù)據(jù)的CRUD
本篇博客將通過 MyBatis 來實(shí)現(xiàn)常用的數(shù)據(jù)增加、刪除、修改、查詢和分頁功能。

1.創(chuàng)建項(xiàng)目 & 引入依賴
創(chuàng)建一個 Spring Boot 項(xiàng)目,并引入 MyBatis 和 MySQL 依賴。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.實(shí)現(xiàn)數(shù)據(jù)表的自動初始化
在項(xiàng)目的 resources 目錄下新建 db 目錄,并添加 schema.sql 文件,然后在此文件中寫入創(chuàng)建 user 表的 SQL 語句,以便進(jìn)行初始化數(shù)據(jù)表。具體代碼如下:
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在 application.properties 配置文件中配置數(shù)據(jù)庫連接,并加上數(shù)據(jù)表初始化的配置。具體代碼如下:
spring.datasource.initialize=true spring.datasource.initialization-mode=always spring.datasource.schema=classpath:db/schema.sql
完整的 application.properties 文件如下:
spring.datasource.url=jdbc:mysql://127.0.0.1/book?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true spring.datasource.username=xxxx spring.datasource.password=xxxxxx spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true spring.datasource.initialization-mode=always spring.datasource.schema=classpath:db/schema.sql spring.thymeleaf.cache=false server.port=8080
這樣,Spring Boot 在啟動時就會自動創(chuàng)建 user 表。
3.實(shí)現(xiàn)實(shí)體對象建模
用 MyBatis 來創(chuàng)建實(shí)體,見以下代碼:
package com.example.demo.entity;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private int age;
}
從上述代碼可以看出,用 MyBatis 創(chuàng)建實(shí)體是不需要添加注解 @Entity 的,因?yàn)?@Entity 屬于 JPA 的專屬注解。
4.實(shí)現(xiàn)實(shí)體和數(shù)據(jù)表的映射關(guān)系
實(shí)現(xiàn)實(shí)體和數(shù)據(jù)表的映射關(guān)系可以在 Mapper 類上添加注解 @Mapper,見以下代碼。建議以后直接在入口類加 @MapperScan("com.example.demo.mapper"),如果對每個 Mapper 都加注解則很麻煩。
package com.example.demo.mapper;
import com.example.demo.entity.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User queryById(@Param("id") int id);
@Select("SELECT * FROM user")
List<User> queryAll();
@Insert({"INSERT INTO user(name,age) VALUES(#{name},#{age})"})
int add(User user);
@Delete("DELETE FROM user WHERE id = #{id}")
int delById(int id);
@Update("UPDATE user SET name=#{name},age=#{age} WHERE id = #{id}")
int updateById(User user);
@Select("SELECT * FROM user")
Page<User> getUserList();
}
5.實(shí)現(xiàn)增加、刪除、修改和查詢功能
創(chuàng)建控制器實(shí)現(xiàn)操作數(shù)據(jù)的 API,見以下代碼:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping("/querybyid")
User queryById(int id) {
return userMapper.queryById(id);
}
@RequestMapping("/")
List<User> queryAll() {
return userMapper.queryAll();
}
@RequestMapping("/add")
String add(User user) {
return userMapper.add(user) == 1 ? "success" : "failed";
}
@RequestMapping("/updatebyid")
String updateById(User user) {
return userMapper.updateById(user) == 1 ? "success" : "failed";
}
@RequestMapping("/delbyid")
String delById(int id) {
return userMapper.delById(id) == 1 ? "success" : "failed";
}
}
- 啟動項(xiàng)目,訪問
http://localhost:8080/user/add?name=pp&age=20,會自動添加一個name=pp、age=20的數(shù)據(jù)。


- 訪問
http://localhost:8080/user/updatebyid?name=pipi&age=26&id=1,會實(shí)現(xiàn)對id=1的數(shù)據(jù)的更新,更新為name=pipi、age=26。

- 訪問
http://localhost:8080/user/querybyid?id=1,可以查找到id=1的數(shù)據(jù),此時的數(shù)據(jù)是name=pipi、age=26。

- 訪問
http://localhost:8080/user/,可以查詢出所有的數(shù)據(jù)。

- 訪問
http://localhost:8080/user/delbyid?id=1,可以刪除id為1的數(shù)據(jù)。

6.配置分頁功能
6.1 增加分頁支持
分頁功能可以通過 PageHelper 來實(shí)現(xiàn)。要使用 PageHelper,則需要添加如下依賴,并增加 Thymeleaf 支持。
<!-- 增加對PageHelper的支持 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <!--增加thymeleaf支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
6.2 創(chuàng)建分頁配置類
創(chuàng)建分頁配置類來實(shí)現(xiàn)分頁的配置,見以下代碼:
package com.example.demo.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class PageHelperConfig {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
代碼解釋如下。
@Configuration:表示 PageHelperConfig 這個類是用來做配置的。@Bean:表示啟動 PageHelper 攔截器。offsetAsPageNum:設(shè)置為true時,會將 RowBounds 第一個參數(shù)offset當(dāng)成pageNum頁碼使用。rowBoundsWithCount:設(shè)置為true時,使用 RowBounds 分頁會進(jìn)行count查詢。reasonable:啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最后一頁。
7.實(shí)現(xiàn)分頁控制器
創(chuàng)建分頁列表控制器,用以顯示分頁頁面,見以下代碼:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
public class UserListController {
@Autowired
UserMapper userMapper;
@RequestMapping("/listall")
public String listCategory(Model m, @RequestParam(value="start", defaultValue="0")int start, @RequestParam(value="size", defaultValue="5") int size) throws Exception {
PageHelper.startPage(start,size,"id desc");
List<User> cs = userMapper.queryAll();
PageInfo<User> page = new PageInfo<>(cs);
m.addAttribute("page", page);
return "list";
}
}
start:在參數(shù)里接收當(dāng)前是第幾頁。默認(rèn)值是0。size:每頁顯示多少條數(shù)據(jù)。默認(rèn)值是5。PageHelper.startPage(start,size,"id desc"): 根據(jù)start、size進(jìn)行分頁,并且設(shè)置id倒排序。List<User>:返回當(dāng)前分頁的集合。PageInfo<User>:根據(jù)返回的集合創(chuàng)建 Pagelnfo 對象。model.addAttribute("page", page):把page(PageInfo對象)傳遞給視圖,以供后續(xù)顯示。
8.創(chuàng)建分頁視圖
接下來,創(chuàng)建用于視圖顯示的 list.html,其路徑為 resources/template/list.html。
在視圖中,通過 page.pageNum 獲取當(dāng)前頁碼,通過 page.pages 獲取總頁碼數(shù),見以下代碼:
<div class="with:80%">
<div th:each="u : ${page.list}">
<span scope="row" th:text="${u.id}">id</span>
<span th:text="${u.name}">name</span>
</div>
</div>
<div>
<a th:href="@{listall?start=1}" rel="external nofollow" >[首頁]</a>
<a th:href="@{/listall(start=${page.pageNum-1})}" rel="external nofollow" rel="external nofollow" >[上頁]</a>
<a th:href="@{/listall(start=${page.pageNum+1})}" rel="external nofollow" rel="external nofollow" >[下頁]</a>
<a th:href="@{/listall(start=${page.pages})}" rel="external nofollow" rel="external nofollow" >[末頁]</a>
<div>當(dāng)前頁/總頁數(shù):<a th:text="${page.pageNum}" th:href="@{/listall(start=${page.pageNum})}" rel="external nofollow" ></a>
/<a th:text="${page.pages}" th:href="@{/listall(start=${page.pages})}" rel="external nofollow" rel="external nofollow" ></a></div>
</div>
啟動項(xiàng)目,多次訪問 http://localhost:8080/user/add?name=pp&age=26 增加數(shù)據(jù),然后訪問 http://localhost:8080/listall 可以查看到分頁列表。

但是,上述代碼有一個缺陷:顯示分頁處無論數(shù)據(jù)多少都會顯示“上頁、下頁”。所以,需要通過以下代碼加入判斷,如果沒有上頁或下頁則不顯示。
<a th:if="${not page.IsFirstPage}" th:href="@{/listall(start=${page.pageNum-1})}" rel="external nofollow" rel="external nofollow" >[上頁]</a>
<a th:if="${not page.IsLastPage}" th:href="@{/listall(start=${page.pageNum+1})}" rel="external nofollow" rel="external nofollow" >[下頁]</a>
上述代碼的作用是:如果是第一頁,則不顯示“上頁”;如果是最后一頁,則不顯示“下頁”。


還有一種更簡單的方法:在 Mapper 中直接返回 page 對象,見以下代碼:
@Select("SELECT * FROM user")
Page<User> getUserList();
然后在控制器中這樣使用:
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserListControllerB {
@Autowired
UserMapper userMapper;
// http://localhost:8080/listall2?pageNum=1&pageSize=2
@RequestMapping("/listall2")
// 如果方法的參數(shù)不指定默認(rèn)值,且請求地址也沒有指定參數(shù)值,則項(xiàng)目運(yùn)行時會報錯。
public Page<User> getUserList(@RequestParam(value="pageNum",defaultValue="0")int pageNum, @RequestParam(value = "pageSize", defaultValue = "5") int pageSize)
//public Page<User> getUserList(Integer pageNum, Integer pageSize)
{
PageHelper.startPage(pageNum, pageSize);
Page<User> userList= userMapper.getUserList();
return userList;
}
}
代碼解釋如下。
pageNum:頁碼。pageSize:每頁顯示多少記錄。

以上就是SpringBoot使用MyBatis實(shí)現(xiàn)數(shù)據(jù)的CRUD的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot MyBatis數(shù)據(jù)CRUD的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot整合MyBatis實(shí)現(xiàn)CRUD操作項(xiàng)目實(shí)踐
- SpringBoot整合MyBatis Plus實(shí)現(xiàn)基本CRUD與高級功能
- SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式
- SpringBoot整合Mybatis Plus實(shí)現(xiàn)基本CRUD的示例代碼
- springboot+mybatis-plus實(shí)現(xiàn)內(nèi)置的CRUD使用詳解
- SpringBoot+Mybatis+Vue 實(shí)現(xiàn)商品模塊的crud操作
- 詳解springboot+mybatis-plue實(shí)現(xiàn)內(nèi)置的CRUD使用詳情
- SpringBoot整合Mybatis實(shí)現(xiàn)CRUD
- Spring Boot整合MyBatis-Plus實(shí)現(xiàn)CRUD操作的示例代碼
相關(guān)文章
Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用
在開發(fā)過程中我們常常會用到兩個數(shù)據(jù)庫,一個數(shù)據(jù)用來實(shí)現(xiàn)一些常規(guī)的增刪改查,另外一個數(shù)據(jù)庫用來實(shí)時存數(shù)據(jù)。進(jìn)行數(shù)據(jù)的統(tǒng)計分析??梢宰x寫分離。可以更好的優(yōu)化和提高效率;或者兩個數(shù)據(jù)存在業(yè)務(wù)分離的時候也需要多個數(shù)據(jù)源來實(shí)現(xiàn)2022-06-06
如何使用Java實(shí)現(xiàn)請求deepseek
這篇文章主要為大家詳細(xì)介紹了如何使用Java實(shí)現(xiàn)請求deepseek功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
elasticsearch節(jié)點(diǎn)間通信的基礎(chǔ)transport啟動過程
這篇文章主要為大家介紹了elasticsearch節(jié)點(diǎn)間通信的基礎(chǔ)transport啟動過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Java長度不足左位補(bǔ)0的3種實(shí)現(xiàn)方法
這篇文章主要介紹了Java長度不足左位補(bǔ)0的3種實(shí)現(xiàn)方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
WIN7系統(tǒng)JavaEE(java)環(huán)境配置教程(一)
這篇文章主要介紹了WIN7系統(tǒng)JavaEE(java+tomcat7+Eclipse)環(huán)境配置教程,本文重點(diǎn)在于java配置,感興趣的小伙伴們可以參考一下2016-06-06
Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)-連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫
這篇文章給大家分享了Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)中連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫的相關(guān)知識點(diǎn)和代碼,有興趣的可以學(xué)習(xí)參考下。2018-07-07
Java中Runnable和Callable分別什么時候使用
提到 Java 就不得不說多線程了,就算你不想說,面試官也得讓你說呀,那說到線程,就不得不說Runnable和Callable這兩個家伙了,二者在什么時候使用呢,下面就來和簡單講講2023-08-08

