最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何使用MybatisPlus快速進行增刪改查詳解

 更新時間:2022年08月05日 11:45:35   作者:xiaozhuxuejava  
增刪改查在日常開發(fā)中是再正常不多的一個需求了,下面這篇文章主要給大家介紹了關(guān)于如何使用MybatisPlus快速進行增刪改查的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

前言:

mybatisplus 可以說是對mybatis更好的拓展,一些簡單的增刪改查的操作已經(jīng)被作者實現(xiàn),我們只需引用即可。

1.數(shù)據(jù)庫建表

這里使用的是MySQL數(shù)據(jù)庫,表名為student

2.新建一個springboot項目

這里使用的idea

(1)、引入相應(yīng)的jar包

修改一下springboot的版本 最好與此一致,其他版本不確定是否兼容

這里如有需要復(fù)制時,注意空白格,直接復(fù)制可能會報錯

    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/>
    </parent>

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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)、快速啟動項目

在此之前,先看看一看我的項目結(jié)構(gòu)

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-vdJ6cRtB-1649861709999)(C:\Users\jsj rj201zdp\AppData\Roaming\Typora\typora-user-images\1649861657658.png)]

新建一個controller包,在controller包下新建一個HelloController.java

package com.zhu.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

springboot啟動類,運行main即可

package com.zhu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MybatisplusDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisplusDemoApplication.class, args);
    }
}

在瀏覽器(這里使用的谷歌瀏覽器)中輸入地址: http://localhost:8080/test/hello

至此,一個springboot項目快速啟動完成,下面我們需要引入mybatisplus相關(guān)依賴

3.springboot結(jié)合mybatisplus

(1)、引入mybatisplus以及其他依賴

<!--mybatisplus-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--數(shù)據(jù)庫連接-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

(2)、創(chuàng)建application.yml文件,修改配置

# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/此處為你的數(shù)據(jù)庫名?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: 你的數(shù)據(jù)庫用戶名
    password: 你的數(shù)據(jù)庫密碼
mybatis-plus:
  # xml文件掃描
  mapper-locations: classpath*:/mapper/**Mapper.xml

(3)、創(chuàng)建mybaisplus配置類

package com.zhu.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@MapperScan("com.zhu.mapper")//mapper接口掃描注解
@EnableTransactionManagement
public class MyBatisPlusConfig {//分頁配置,本博客不展示分頁操作

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        return paginationInterceptor;
    }
}

(4)、創(chuàng)建實體類

package com.zhu.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 
 * </p>
 *
 * @author xiaozhu
 * @since 2022-04-13
 */
//使用lombok,簡化了代碼,不用書寫set get等方法
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 自動遞增
     */
    @TableId(value = "sno", type = IdType.AUTO)
    private Integer sno;

    private String sname;

    private String sex;

    private Integer age;

    private Integer clas;
}

(5)、創(chuàng)建mapper接口

package com.zhu.mapper;

import com.zhu.entity.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xiaozhu
 * @since 2022-04-13
 */
public interface StudentMapper extends BaseMapper<Student> {

}

(6)、創(chuàng)建service接口及其實現(xiàn)類

package com.zhu.service;

import com.zhu.entity.Student;
import com.baomidou.mybatisplus.extension.service.IService;

/**
 * <p>
 *  服務(wù)類
 * </p>
 *
 * @author xiaozhu
 * @since 2022-04-13
 */
public interface StudentService extends IService<Student> {

}
package com.zhu.service.impl;

import com.zhu.entity.Student;
import com.zhu.mapper.StudentMapper;
import com.zhu.service.StudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服務(wù)實現(xiàn)類
 * </p>
 *
 * @author xiaozhu
 * @since 2022-04-13
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

}

(7)、創(chuàng)建controller

package com.zhu.controller;


import com.zhu.entity.Student;
import com.zhu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaozhu
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    
    @Autowired
    private StudentService studentService;

    //返回所有學(xué)生
    @GetMapping("/allStudent")
    public List<Student> findAllStudent(){

        return studentService.list();
    }

    //根據(jù)學(xué)號查詢學(xué)生
    @GetMapping("/findBySno/{sno}")
    public Student findBySno(@PathVariable("sno") Integer sno){

        return studentService.getById(sno);
    }

    //根據(jù)學(xué)號刪除學(xué)生信息(此方法可以使用軟件postman進行測試)
    @DeleteMapping("/deleteBySno/{sno}")
    public boolean deleteBySno(@PathVariable("sno") Integer sno){
        return  studentService.removeById(sno);
    }

    //增加一個學(xué)生信息(此方法可以使用軟件postman進行測試),注意學(xué)號自增
    @PostMapping("/add")
    public boolean add(@RequestBody Student student){
        return studentService.save(student);
    }

    //根據(jù)學(xué)號修改學(xué)生信息(此方法可以使用軟件postman進行測試),注意學(xué)號自增
    @PutMapping("/update")
    public boolean update(@RequestBody Student student){
        return studentService.updateById(student);
    }
}

至此,我們完成了mybatisplus的快速的入門級別的增刪改查

總結(jié)

到此這篇關(guān)于如何使用MybatisPlus快速進行增刪改查的文章就介紹到這了,更多相關(guān)MybatisPlus增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談java線程join方法使用方法

    淺談java線程join方法使用方法

    這篇文章主要介紹了淺談java線程join方法使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 詳解Spring AOP 實現(xiàn)“切面式”valid校驗

    詳解Spring AOP 實現(xiàn)“切面式”valid校驗

    本篇文章主要介紹了詳解Spring AOP 實現(xiàn)“切面式”valid校驗,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Java中Set與List的關(guān)系與區(qū)別介紹

    Java中Set與List的關(guān)系與區(qū)別介紹

    這篇文章主要介紹了Java中Set與List的關(guān)系與區(qū)別介紹,本文總結(jié)它們兩個接口都是繼承自Collection、它們之間的存儲方式不一樣,需要的朋友可以參考下
    2015-03-03
  • Spring Validation和Hibernate Validator結(jié)合國際化代碼實例

    Spring Validation和Hibernate Validator結(jié)合國際化代碼實例

    這篇文章主要介紹了Spring Validation和Hibernate Validator結(jié)合國際化代碼實例,我們需要對請求參數(shù)進行非空、長度、正確性進行校驗, 本文主要講解Spring Validation 和 Hibernate Validator, 同時整合i18n(國際化)實現(xiàn)參數(shù)校驗自動,需要的朋友可以參考下
    2023-10-10
  • Java自定義映射resultMap定義及用法

    Java自定義映射resultMap定義及用法

    MyBatis的每一個查詢映射的返回類型都是ResultMap,當(dāng)我們提供返回類型屬性是resultType時,MyBatis會自動給我們把對應(yīng)值賦給resultType所指定對象的屬性,當(dāng)我們提供返回類型是resultMap時,將數(shù)據(jù)庫中列數(shù)據(jù)復(fù)制到對象的相應(yīng)屬性上,可以用于復(fù)制查詢,兩者不能同時用
    2022-11-11
  • java如何實現(xiàn)字符串中的字母排序

    java如何實現(xiàn)字符串中的字母排序

    這篇文章主要介紹了java如何實現(xiàn)字符串中的字母排序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java中如何使用Redis GEO測算經(jīng)緯度距離

    Java中如何使用Redis GEO測算經(jīng)緯度距離

    GEO也是Redis中提供的一種數(shù)據(jù)類型,可以將其理解成ZSet有序集合類型,本文給大家介紹Java-使用Redis GEO測算經(jīng)緯度距離,感興趣的朋友一起看看吧
    2023-11-11
  • 淺談cookie 和session 的區(qū)別

    淺談cookie 和session 的區(qū)別

    下面小編就為大家?guī)硪黄獪\談cookie 和session 的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Spring切面優(yōu)先級與基于xml的AOP實現(xiàn)方法詳解

    Spring切面優(yōu)先級與基于xml的AOP實現(xiàn)方法詳解

    這篇文章主要介紹了Spring切面的優(yōu)先級與基于xml的AOP的詳細步驟,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • Java日常練習(xí)題,每天進步一點點(54)

    Java日常練習(xí)題,每天進步一點點(54)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08

最新評論

东台市| 台山市| 清流县| 贵港市| 衡山县| 黑河市| 宁国市| 宜川县| 崇仁县| 二连浩特市| 江达县| 合肥市| 互助| 平果县| 扎鲁特旗| 封丘县| 湖州市| 景泰县| 黄龙县| 鄂伦春自治旗| 奉新县| 宜川县| 永吉县| 永嘉县| 周口市| 将乐县| 松滋市| 福建省| 中方县| 苍南县| 阿城市| 迁西县| 闵行区| 新晃| 湾仔区| 紫阳县| 广东省| 来安县| 上饶市| 长岛县| 济宁市|