搭建SpringBoot框架----MyBatis--plus

李法
2023-12-01

一、MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生

二、配置步骤

1、添加依赖

在pom.xml里添加Mybatis-plus依赖

<?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.hqyj</groupId>
    <artifactId>plusdemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>plusdemo</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.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <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.5.0</version>
        </dependency>
        <!-- 配置mybatis-plus生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.3.0</version>
            <!-- 当前依赖不向下传递 -->
            <optional>true</optional>
        </dependency>
        <!-- 配置mybatis-plus生成器默认模板 -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.3</version>
            <optional>true</optional>
        </dependency>
        <!-- 配置mybatis-plus分页插件 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.hqyj.PlusdemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

2、创建实体类

注解:@Data:简化java代码编写,为该类提供读写属性。简单来说就是不用再手动添加和get/set等方法

注解:@TableId(value = "aid",type = IdType.ASSIGN_ID):MP自动赋值

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class Astate {
    @TableId(value = "aid",type = IdType.ASSIGN_ID)
    private Integer aid;
    private String aname;
    private String abackup;
}

3、创建dao窗口

继承BaseMapper<>

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hqyj.pojo.Astate;

public interface AstateDao extends BaseMapper<Astate>{

}

3、创建service接口和实现类Impl

继承IService<>

import com.baomidou.mybatisplus.extension.service.IService;
import com.hqyj.pojo.Astate;

public interface AstateService extends IService<Astate> {
}

继承ServiceImpl<dao, pojo> 

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.dao.AstateDao;
import com.hqyj.pojo.Astate;
import com.hqyj.service.AstateService;
import org.springframework.stereotype.Service;

@Service
public class AstateServiceImpl extends ServiceImpl<AstateDao, Astate> implements AstateService {
}

4、创建controller

@RestController
@RequestMapping("/astate")
public class AstateController {
    @Autowired
    private AstateService as;

    @RequestMapping("/insert")
    public String insert(Astate a){
        System.out.println(a);
        try {
            as.save(a);
            return "1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0";
    }

    @RequestMapping("/update")
    public String update(Astate a){
        System.out.println(a);
        try {
            as.updateById(a);
            return "1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0";
    }

    @RequestMapping("/delete")
    public String delete(String aid){
        try {
            as.removeById(aid);
            return "1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "0";
    }

    @RequestMapping("/selectOne")
    public Astate selectOne(String aid){
           Astate astate = as.getById(aid);
            return astate;
    }

    @RequestMapping("/selectList")
    public List selectList(){

           List list = as.list();
           return list;
    }
}

 类似资料: