springboot中集成mybatis-plus可以省去编写mybatis框架中的*.xml文件。
环境:springboot 3.0.0 ; jdk17
-- 创建数据库
DROP DATABASE IF EXISTS springboot_mybatis;
CREATE DATABASE springboot_mybatis;
USE springboot_mybatis;
-- 创建表
CREATE TABLE person(
`id` INT NOT NULL AUTO_INCREMENT,
`age` INT NOT NULL,
`birthday` DATE DEFAULT NULL,
`email` VARCHAR(255) DEFAULT NULL,
`gender` CHAR(1) DEFAULT NULL,
`name` VARCHAR(255) DEFAULT NULL,
`salary` DOUBLE NOT NULL,
PRIMARY KEY(`id`)
)CHARSET=utf8;
4.0.0
com.lingyi
springboot_mybatis
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
3.0.0
17
17
3.0.0-M8
17
1.18.24
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.projectlombok
lombok
${lombok.version}
com.baomidou
mybatis-plus-boot-starter
3.5.3.1
org.springframework.boot
spring-boot-configuration-processor
mysql
mysql-connector-java
runtime
8.0.13
src/main/java
**/*.xml
true
src/main/resources
**.*
org.springframework.boot
spring-boot-maven-plugin
package com.lingyi.mybatis.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "person")
public class Person {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
private Integer age;
private Date birthday;
private String email;
private String name;
private String gender;
private Double salary;
}
相比于mybatis需要自己声明方法,MP提供了一个通用的BaseMapper,有很多通用的方法,通过继承,我们就可以直接使用,简化开发。
在包com.lingyi.mybatis.mapper下创建PersonMapper.java文件
package com.lingyi.mybatis.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lingyi.mybatis.bean.Person;
import org.apache.ibatis.annotations.Mapper;
/***
* BaseMapper是MyBatisPlus内置的通用Mapper,提供了很多的方法
* 如果它提供的方法不能满足业务需求,我们可以在这里开发新的方法
*/
@Mapper
public interface PersonMapper extends BaseMapper {
}
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.110.45:3306/pms?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password: Ncti@2023
package com.lingyi.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@MapperScan指定mapper文件所在的包,即可将Mapper注入到Spring的容器中,basePackages可以包含多个值
@MapperScan(basePackages = {"com.lingyi.mybatis.mapper"})
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception{
SpringApplication.run(com.lingyi.mybatis.Application.class,args);
}
}
package com.lingyi.mybatis;
import com.lingyi.mybatis.bean.Person;
import com.lingyi.mybatis.mapper.PersonMapper;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestPersonMapper {
@Autowired
private PersonMapper personMapper;
@Test
public void testPersonMapper(){
Person person = personMapper.selectById(1);
System.out.println(person);
}
}