欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot使用MyBatis實(shí)現(xiàn)數(shù)據(jù)的CRUD

 更新時(shí)間:2024年11月20日 09:48:37   作者:G皮T  
MyBatis是一個(gè)輕量級(jí)的對(duì)象關(guān)系映射(Object-Relational Mapping,ORM)框架,它允許開(kāi)發(fā)者通過(guò)編寫(xiě)SQL動(dòng)態(tài)查詢數(shù)據(jù)庫(kù),而無(wú)需顯式地操作JDBC,對(duì)于增刪改查操作,MyBatis提供了一種基于XML或注解的方式來(lái)進(jìn)行,本文介紹了SpringBoot使用MyBatis實(shí)現(xiàn)數(shù)據(jù)的CRUD

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

在這里插入圖片描述

1.創(chuàng)建項(xiàng)目 & 引入依賴

創(chuàng)建一個(gè) 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ù)表的自動(dòng)初始化

在項(xiàng)目的 resources 目錄下新建 db 目錄,并添加 schema.sql 文件,然后在此文件中寫(xiě)入創(chuàng)建 user 表的 SQL 語(yǔ)句,以便進(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ù)庫(kù)連接,并加上數(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 在啟動(dòng)時(shí)就會(huì)自動(dòng)創(chuàng)建 user 表。

3.實(shí)現(xiàn)實(shí)體對(duì)象建模

用 MyBatis 來(lái)創(chuàng)建實(shí)體,見(jiàn)以下代碼:

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 的專(zhuān)屬注解。

4.實(shí)現(xiàn)實(shí)體和數(shù)據(jù)表的映射關(guān)系

實(shí)現(xiàn)實(shí)體和數(shù)據(jù)表的映射關(guān)系可以在 Mapper 類(lèi)上添加注解 @Mapper,見(jiàn)以下代碼。建議以后直接在入口類(lèi)加 @MapperScan("com.example.demo.mapper"),如果對(duì)每個(gè) 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,見(jiàn)以下代碼:

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";
    }
}
  • 啟動(dòng)項(xiàng)目,訪問(wèn) http://localhost:8080/user/add?name=pp&age=20,會(huì)自動(dòng)添加一個(gè) name=ppage=20 的數(shù)據(jù)。

在這里插入圖片描述

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

  • 訪問(wèn) http://localhost:8080/user/delbyid?id=1,可以刪除 id1 的數(shù)據(jù)。

在這里插入圖片描述

6.配置分頁(yè)功能

6.1 增加分頁(yè)支持

分頁(yè)功能可以通過(guò) PageHelper 來(lái)實(shí)現(xiàn)。要使用 PageHelper,則需要添加如下依賴,并增加 Thymeleaf 支持。

<!-- 增加對(duì)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)建分頁(yè)配置類(lèi)

創(chuàng)建分頁(yè)配置類(lèi)來(lái)實(shí)現(xiàn)分頁(yè)的配置,見(jià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 這個(gè)類(lèi)是用來(lái)做配置的。
  • @Bean:表示啟動(dòng) PageHelper 攔截器。
  • offsetAsPageNum:設(shè)置為 true 時(shí),會(huì)將 RowBounds 第一個(gè)參數(shù) offset 當(dāng)成 pageNum 頁(yè)碼使用。
  • rowBoundsWithCount:設(shè)置為 true 時(shí),使用 RowBounds 分頁(yè)會(huì)進(jìn)行 count 查詢。
  • reasonable:?jiǎn)⒂煤侠砘瘯r(shí),如果 pageNum<1 會(huì)查詢第一頁(yè),如果 pageNum>pages 會(huì)查詢最后一頁(yè)。

7.實(shí)現(xiàn)分頁(yè)控制器

創(chuàng)建分頁(yè)列表控制器,用以顯示分頁(yè)頁(yè)面,見(jiàn)以下代碼:

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)前是第幾頁(yè)。默認(rèn)值是 0
  • size:每頁(yè)顯示多少條數(shù)據(jù)。默認(rèn)值是 5。
  • PageHelper.startPage(start,size,"id desc") : 根據(jù) start、size 進(jìn)行分頁(yè),并且設(shè)置 id 倒排序。
  • List<User>:返回當(dāng)前分頁(yè)的集合。
  • PageInfo<User>:根據(jù)返回的集合創(chuàng)建 Pagelnfo 對(duì)象。
  • model.addAttribute("page", page):把 page (PageInfo 對(duì)象)傳遞給視圖,以供后續(xù)顯示。

8.創(chuàng)建分頁(yè)視圖

接下來(lái),創(chuàng)建用于視圖顯示的 list.html,其路徑為 resources/template/list.html

在視圖中,通過(guò) page.pageNum 獲取當(dāng)前頁(yè)碼,通過(guò) page.pages 獲取總頁(yè)碼數(shù),見(jiàn)以下代碼:

<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" >[首頁(yè)]</a>
    <a th:href="@{/listall(start=${page.pageNum-1})}" rel="external nofollow"  rel="external nofollow" >[上頁(yè)]</a>
    <a th:href="@{/listall(start=${page.pageNum+1})}" rel="external nofollow"  rel="external nofollow" >[下頁(yè)]</a>
    <a th:href="@{/listall(start=${page.pages})}" rel="external nofollow"  rel="external nofollow" >[末頁(yè)]</a>
    <div>當(dāng)前頁(yè)/總頁(yè)數(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>

啟動(dòng)項(xiàng)目,多次訪問(wèn) http://localhost:8080/user/add?name=pp&age=26 增加數(shù)據(jù),然后訪問(wèn) http://localhost:8080/listall 可以查看到分頁(yè)列表。

在這里插入圖片描述

但是,上述代碼有一個(gè)缺陷:顯示分頁(yè)處無(wú)論數(shù)據(jù)多少都會(huì)顯示“上頁(yè)、下頁(yè)”。所以,需要通過(guò)以下代碼加入判斷,如果沒(méi)有上頁(yè)或下頁(yè)則不顯示。

<a  th:if="${not page.IsFirstPage}" th:href="@{/listall(start=${page.pageNum-1})}" rel="external nofollow"  rel="external nofollow" >[上頁(yè)]</a>
<a  th:if="${not page.IsLastPage}" th:href="@{/listall(start=${page.pageNum+1})}" rel="external nofollow"  rel="external nofollow" >[下頁(yè)]</a>

上述代碼的作用是:如果是第一頁(yè),則不顯示“上頁(yè)”;如果是最后一頁(yè),則不顯示“下頁(yè)”。

在這里插入圖片描述

在這里插入圖片描述

還有一種更簡(jiǎn)單的方法:在 Mapper 中直接返回 page 對(duì)象,見(jiàn)以下代碼:

@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)值,且請(qǐng)求地址也沒(méi)有指定參數(shù)值,則項(xiàng)目運(yùn)行時(shí)會(huì)報(bào)錯(cuò)。
    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:頁(yè)碼。
  • pageSize:每頁(yè)顯示多少記錄。

在這里插入圖片描述

以上就是SpringBoot使用MyBatis實(shí)現(xiàn)數(shù)據(jù)的CRUD的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot MyBatis數(shù)據(jù)CRUD的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論