SpringBoot整合MybatisPlus(详细)

什么是MyBatisPlus(MP)?

为简化开发,提升效率而生,对mybatis功能进行增强,但未做改变。
支持任何能使用 MyBatis 进行 CRUD, 并且支持标准 SQL 的数据库。

案例操作

新建springboot工程

SpringBoot整合MybatisPlus(详细)_第1张图片

若选择https://start.spring.io下一步失败

SpringBoot整合MybatisPlus(详细)_第2张图片

则选择Custom,输入:https://start.aliyun.com后下一步

在这里插入图片描述

SpringBoot整合MybatisPlus(详细)_第3张图片

添加需要的依赖

SpringBoot整合MybatisPlus(详细)_第4张图片

安装插件

file --> settings --> Plugins --> Marketplace中搜索MyBatisX --> Installed
SpringBoot整合MybatisPlus(详细)_第5张图片

添加其他依赖,全部依赖如下:

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>

		 <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
		
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.1.0version>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
  dependencies>

修改配置文件:

application.properties:改为application.yml

application.yml:

#端口号8080
server:
  port: 8080

#数据库名:mysql,用户名root,密码123456
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    
# mybatis-plus配置
mybatis-plus:
  # xml文件位置
  mapper-locations: classpath:mapper/*.xml  

/src/main/java/com.example.demo下设置:service,controller,mapper,entity包
/src/main/resouces下设置mapping文件夹

controller创建StudentController类;
service下创建StudentService接口,并创建impl包,创建StudentServiceImpl类;
mapper下创建StudentMapper接口;
entity下创建StudentInfo类;
mapping文件夹下创建StudentMapper.xml文件;

(本次未使用分页等功能,不需配置config)

结构如下图所示:

SpringBoot整合MybatisPlus(详细)_第6张图片

新建数据库测试表:student

CREATE TABLE student(
	id VARCHAR(2) COMMENT '学生ID',
	sname VARCHAR(20) COMMENT '学生姓名',
	classId VARCHAR(3) COMMENT '班级ID',
	birthday VARCHAR(5) COMMENT '学生生日',
	email VARCHAR(20) COMMENT '学生电子邮箱'
);

INSERT INTO student(id,sname,class_id,birthday,email)
VALUES(1,'张三',101,1016,'[email protected]'),(2,'李四',101,511,'[email protected]'),
	  (3,'王五',101,1016,'[email protected]'),(4,'赵六',103,615,'[email protected]');

SpringBoot整合MybatisPlus(详细)_第7张图片

StudentInfo类:

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("student")
public class StudentInfo {
    //若id为主键,则为@TableId("id")
    @TableField("id")
    private String id;

    @TableField("sname")
    private String sname;

    @TableField("classId")
    private String classId;

    @TableField("birthday")
    private String birthday;

    @TableField("email")
    private String email;
}

StudentMapper(继承BaseMapper)

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

@Mapper
public interface StudentMapper extends BaseMapper<StudentInfo> {
}

StudentService(继承IService)

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.StudentInfo;

public interface StudentService extends IService<StudentInfo> {
}

StudentServiceImpl(继承ServiceImplement)

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.StudentInfo;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentInfo> implements StudentService {
}

StudentController:

import com.example.demo.entity.StudentInfo;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/demo11")
public class StudentController {

    @Autowired(required = false)
    private StudentService studentService;

    /**
     * 查询学生信息
     * @param id
     * @return
     */
    @RequestMapping("getInfo/{id}")
    public StudentInfo getStudentInfo(@PathVariable String id){
        return studentService.getById(id);
    }

    /**
     * 插入学生信息
     * @param studentInfo
     */
    @RequestMapping("/insert")
    public void insertInfo(StudentInfo studentInfo){

        StudentInfo info=new StudentInfo();
        info.setId(studentInfo.getId());
        info.setSname(studentInfo.getSname());
        info.setClassId(studentInfo.getClassId());
        info.setBirthday(studentInfo.getBirthday());
        info.setEmail(studentInfo.getEmail());
        studentService.save(info);
    }

    /**
     * 查询全部学生信息
     * @return
     */
    @RequestMapping("/selectAll")
    public List<StudentInfo> selectAll(){
        return studentService.list();
    }

    /**
     * 根据id更新学生表信息
     * @param studentInfo
     */
    @RequestMapping("/update")
    public void updateById(StudentInfo studentInfo){

        StudentInfo info=new StudentInfo();
        info.setId(studentInfo.getId());
        info.setSname(studentInfo.getSname());
        info.setClassId(studentInfo.getClassId());
        info.setBirthday(studentInfo.getBirthday());
        info.setEmail(studentInfo.getEmail());
        studentService.updateById(info);
    }

    /**
     * 根据id删除学生信息
     * @param id
     */
    @RequestMapping("/delete")
    public void deleteById(String id){
        studentService.removeById(id);
    }

}

启动程序

SpringBoot整合MybatisPlus(详细)_第8张图片

使用postman测试

测试:查询id为2的学生信息

URL:

localhost:8080/demo11/getInfo/2

SpringBoot整合MybatisPlus(详细)_第9张图片

测试:插入一条学生信息

URL:

localhost:8080/demo11/insert?id=5&sname=小红[email protected]

SpringBoot整合MybatisPlus(详细)_第10张图片

查看数据库student表

SpringBoot整合MybatisPlus(详细)_第11张图片

测试:删除id为5的学生信息

URL:

localhost:8080/demo11/delete?id=5

SpringBoot整合MybatisPlus(详细)_第12张图片

查看数据库student表

SpringBoot整合MybatisPlus(详细)_第13张图片

测试:修改id为4的学生信息

URL:

localhost:8080/demo11/update?id=4&sname=小明[email protected]

SpringBoot整合MybatisPlus(详细)_第14张图片

查看数据库student表

SpringBoot整合MybatisPlus(详细)_第15张图片

测试:查询学生表中全部信息

URL:

localhost:8080/demo11/selectAll

SpringBoot整合MybatisPlus(详细)_第16张图片

测试完成

对控制台输出效果不满意,可添加日志使内容更详细,请移步为SpringBoot项目添加日志:slf4j

你可能感兴趣的:(后端,spring,boot,java)