Mybatis Plus

一、Mybatis Plus的简介

1、什么是Mybatis Plus

MyBatis-Plus(简称 MP)是一个基于 MyBatis 的增强工具,它对 Mybatis 的基础功能进行了增强,但未做任何改变。使得我们可以可以在 Mybatis 开发的项目上直接进行升级为 Mybatis-plus,正如它对自己的定位,它能够帮助我们进一步简化开发过程,提高开发效率。

Mybatis-Plus 其实可以看作是对 Mybatis 的再一次封装,升级之后,对于单表的 CRUD 操作,调用 Mybatis-Plus 所提供的 API 就能够轻松实现,此外还提供了各种查询方式、分页等行为。最最重要的,开发人员还不用去编写 XML,这就大大降低了开发难度

官网为:https://mp.baomidou.com

Mybatis是以编写SQL语句的工作量为代价换取高灵活性 

2、Mybatis和Mybatis Plus的区别 

  • 实现方式不同

MyBatis是基于XML或注解方式进行数据库操作的持久化框架,它提供了简单的CRUD操作及动态SQL生成等功能。而MyBatis-Plus是在MyBatis框架上的封装,通过对于增强简化后的API更加方便地进行开发,在性能、效率和易用性上都有一定的优化。

  • 功能支持不同

MyBatis-Plus相比于MyBatis提供了很多额外的功能,例如像条件构造器、代码生成器、分页插件、性能分析拦截器等实用的组件,使得开发者可以轻松快速完成业务逻辑的开发。而MyBatis相对较为原始,需要手写大量的SQL以完成各种功能的实现。

  • 编程风格有所差异

MyBatis需要定义mapper.xml文件,使用相应的SQL查询语句,实现 Mybatis 提供的方法;而MyBatis-Plus具有许多针对CRUD进行的简化方法,通过继承BaseMapper以及使用Lambda表达式,可以让我们像SpringDataJPA类似地使用接口编程方式进行数据库操作。

二、Mybatis Plus的实现方法

在IDEA上创建一个利用Mybatis Plus框架的构建的项目

1、在创建项目时,选择依赖

2、在pom.xml文件中继续添加依赖

以下是 Spring Boot中的全部依赖

<?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>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <!--表现层-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--持久层-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.18</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>





        <!--其他工具类-->
        <dependency><!--支持热启动-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </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>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.Demo1Application</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

3、用cmd创建Mysql数据库和对应的表

create database test;
use test;

create table if not exists tbl_produce(
    id bigint primary key auto_increment,
    name varchar(32) not null,
    catalog_id bigint,
    price numeric(8,2),
    store_num int
)engine=innodb default charset utf8;


create table if not exists tbl_catalog(
    id bigint primary key auto_increment,
    name varchar(32) not null unique,
    memo varchar(100) 
    )engine=innodb default charset utf8;

插入测试数据

insert into tbl_catalog values(1,'计算机图书','学计算机'),(2,'数学书','学数学'),(3,'课外是','增长知识')

 其中,memo表示类目的备注信息

在互联网应用开发中针对类目(即类别)的处理方法常见的有两种:

1、如果可以确定三级分类,则创建类目大表、类目小表和产品表

2、如果类目等级无法提前确定或者分类层数大于3,则考虑使用自关联的方式来定义类目标

结果如下图所示:

 4、使用MyBatisX插件针对表进行反向映射,生成对应的mapper接口、业务接口和对应的映射源文件

4.1在右上角点击database,来创建数据源

4.2 创建成功后,右键点击数据表的名称执行MyBatisX

 

生成映射文件

 

 5、写ResultJson类

package com.example.domain;

import com.example.entity.TblCatalog;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class JsonResult implements Serializable {
    private int  code;//自定义的响应状态码,不是http规范中的响应码,一般用于给前端更详细的响应信息
    private String message;//服务器生成的相应提示信息
    private Boolean success;//给前端一个简单的响应状态提示
    private Object data; //响应数据

    public static JsonResult success(String message, Object data) {
        JsonResult result=new JsonResult();
        result.setData(data);
        result.setMessage(message);
        result.setSuccess(true);
        result.setCode(2000);
        return result;
    }
}

6、Controller层

package com.example.action;

import com.example.domain.JsonResult;
import com.example.domain.PageBean;
import com.example.entity.TblCatalog;
import com.example.service.TblCatalogService;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/catalog")
public class CatalogController {
    @Autowired
    private TblCatalogService catalogService;
    @GetMapping
    public JsonResult getAllCatalog(){
        List<TblCatalog> catalogList=catalogService.listByPage(null);
        System.out.println(catalogList);
        return JsonResult.success("所有类目列表",catalogList);
    }
    @GetMapping("/show")
    public JsonResult getByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer size){
        PageBean pages=new PageBean();
        pages.setPageNum(page);
        pages.setRowsPerPage(size);
        List<TblCatalog> catalogList=catalogService.listByPage(pages);
        Map<String,Object> map=new HashMap<>();
        map.put("pages",pages);
        map.put("data",catalogList);
        System.out.println(map);
        return JsonResult.success("加载成功",map);
    }


}

 

 7、使用Postman进行测试

 可以看到报错为com.yan.mapper.CatalogMapper没有注册,所以需要添加MyBatisPlus配置类或者在 主类上添加配置

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值