SpringBoot整合MyBatis-Plus(MP)总结

在MyBatis-Plus(MP)技术出现之前,我们依旧需要自己写SQL语句来实现DAO层,但是MP技术出现之后,我们只需要在创建实体类的基础上使用MP来实现DAO层,加上SpringBoot可以很简单创建接口,使得Java后端开发变得非常简单。
虽然在我学习的过程中,感觉MP技术很简单,但我真正动手去实现的时候,也出现了不少的问题,甚至让我卡了挺久,这让我明白了理论和实践相结合是一件非常重要的事情。

一、环境配置

springboot:2.6.3
mybatis-plus-boot-starter:3.4.0

其他配置如MySQL驱动等依照springboot:2.6.3的父级依赖确定版本。
pom.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--connector-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我这个文件自从我后面加了lombok依赖之后就一直报红,但是运行的时候却没有出现有什么错误,我感觉很奇怪。
在这里插入图片描述
在我配置环境的过程中,发现mybatis-plus-boot-starter的版本必须是3.4.0才可以正常运行。
如果3.4.3的版本,就会下载很久并且最后报错:

Cannot resolve org.springframework:spring-tx:5.3.18

调成3.4.2的时候却会报这个错误:

Cannot resolve com.github.jsqlparser:jsqlparser:4.0

我感觉挺奇怪的,网上也没有什么解决方案,导致我在这里花费了不少时间,可能是我的springboot版本比较高,也可能是其他问题。
当然了,这个配置是可以运行的,如果有其他组合的配置其实也是可以的,只需要确定在maven刷新的时候,确定有没有真正导包即可(不会在控制台报错)。

二、配置连接数据库信息

application.yml中,写如下配置:

# 配置数据库信息
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC
    username: root
    password: 123456

url中的mytest是我的数据库名字。
这里我们需要注意:
如果你的MySQL版本不是8以上版本的,driver-class-name那里写成:

driver-class-name: com.mysql.jdbc.Driver

并且在url里面不需要加上:?serverTimezone=UTC
完整配置信息:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC
    username: root
    password: 123456

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

三、项目结构和数据库

我简单做了一个小dome,主要看看这个MP技术到底能不能实现,这个项目结构是有问题的,比如我没有创建service层,我是直接使用controller层调用dao层,我感觉应该没什么问题,不过在真实的开发中,最好还是写service层,这样很多代码不会放在一起,这样很乱。
在这里插入图片描述
我的数据库(mytest)很简单,一共有两张表:
在这里插入图片描述
结构如下:

category_表
在这里插入图片描述
product表:
在这里插入图片描述

四、DAO层

实体类:
在这里插入图片描述

package com.example.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class category_ {
    private int id;
    private String name;
}
package com.example.demo.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class product {

    private int id;
    private String name;
    private double price;

}

dao接口:
这里我们还是需要注意,我们是写的接口,而不是类,我刚开始的时候创建的是接口,它直接让我实现MP的方法,我还以为我是哪里搞错了。

package com.example.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.domain.category_;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface CategoryDao extends BaseMapper<category_> {

}
package com.example.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.domain.product;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ProductDao extends BaseMapper<product> {

}

五、controller层

在controller层中,需要先创建R对象类来把dao查询到的数据封装,这样才可以在前端直接接收到json数据。
R对象类如下所示:

package com.example.demo.controller.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R {
    private boolean flag;
    private Object data;

    public R(Boolean flag){
        this.flag = flag;
    }

    public R(Object object){
        this.flag = true;
        this.data = object;
    }

}

接下来我挨个介绍我的接口以及在postman软件的结果:
在controller类前面需要调用ProductDao 和CategoryDao 并且进行自动装配 :

@Autowired
private ProductDao productDao;

@Autowired
private CategoryDao  categoryDao;

hello请求中,查询了所有的category表的数据,代码如下:

	@GetMapping("/hello")
    public R hello(){
        R r = new R(productDao.selectList(null));
        return r;
    }

在postman的结果如下:
在这里插入图片描述

{
    "flag": true,
    "data": [
        {
            "id": 1,
            "name": "Product1",
            "price": 44.3
        },
        {
            "id": 2,
            "name": "Product2",
            "price": 8273.2
        },
        {
            "id": 3,
            "name": "Product3",
            "price": 823.2
        },
        {
            "id": 21,
            "name": "24",
            "price": 3213.0
        },
        {
            "id": 231,
            "name": "123",
            "price": 123.0
        }
    ]
}

在hhh请求中,查询了一个product数据库的数据:

    @RequestMapping("/hhh")
    public R hhh(){
        return new R(productDao.selectById(1));
    }

结果如下:
在这里插入图片描述

{
    "flag": true,
    "data": {
        "id": 1,
        "name": "Product1",
        "price": 44.3
    }
}

接下来是删除操作:

    // 删除操作
    @DeleteMapping("/d/{id}")
    public R t(@PathVariable int id){
        return new R(categoryDao.deleteById(id));
    }

删除操作需要根据id删除,总不能删了全部吧。
结果如下:
删除8号:
在这里插入图片描述
在这里插入图片描述

{
    "flag": true,
    "data": 1
}

添加操作:

    // 添加操作
    @PostMapping("/p")
    public R add(){
        return new R(categoryDao.insert(new category_(10,"121212")));
    }

在这里插入图片描述

{
    "flag": true,
    "data": 1
}

更新操作:

    @PutMapping("/u/{id}")
    public R update(@PathVariable int id){
        return new R(categoryDao.updateById(new category_(id,"erewre")));
    }

在这里插入图片描述
controller全部代码:

package com.example.demo.controller;

import com.example.demo.controller.utils.R;
import com.example.demo.dao.CategoryDao;
import com.example.demo.dao.ProductDao;
import com.example.demo.domain.category_;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private CategoryDao  categoryDao;

    @GetMapping("/hello")
    public R hello(){
        R r = new R(productDao.selectList(null));
        return r;
    }

    @RequestMapping("/hhh")
    public R hhh(){
        return new R(productDao.selectById(1));
    }

    @RequestMapping("/hh")
    public R hh(){
        return new R(categoryDao.selectList(null));
    }

    @GetMapping("/t")
    public R t(){
        return new R(false);
    }

    // 删除操作
    @DeleteMapping("/d/{id}")
    public R t(@PathVariable int id){
        return new R(categoryDao.deleteById(id));
    }

    // 添加操作
    @PostMapping("/p")
    public R add(){
        return new R(categoryDao.insert(new category_(10,"121212")));
    }

    @PutMapping("/u/{id}")
    public R update(@PathVariable int id){
        return new R(categoryDao.updateById(new category_(id,"erewre")));
    }

}

这样就介绍完了各种MP技术的基本操作,其实还有很多需要我去学习,希望以后可以继续总结。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值