MyBatis-Plus的入门
Pom依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- pom模型版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 项目信息 -->
<groupId>com.mad</groupId> <!-- 项目唯一标识 -->
<artifactId>mybatis-plus</artifactId> <!-- 项目名 -->
<version>1.0-SNAPSHOT</version> <!-- 版本 -->
<packaging>jar</packaging> <!-- 打包方式 (pom,war,jar) -->
<!-- 定义父目录继承关系 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<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>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</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.8</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- maven插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.2.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
项目结构
配置文件
application.properties
#定义服务器端口
server.port=8080
#定义tomcat语言编码
server.tomcat.uri-encoding=utf-8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
SpringBoot启动类
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
package com.mad;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(value = "com.mad.mapper")
public class MyBatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisPlusApplication.class,args);
}
}
实体类Student
// 由于此处类名与表名一致,未使用注解指定表名
// @TableName(value = "tb_name")
@Data
public class Student {
// value与数据库主键列名一致,若实体类属性名与表主键列名一致可省略value
@TableId(value = "sid",type = IdType.AUTO) //指定自增策略
private Integer sid;
private String sname;
private Integer age;
private String sex;
private String department;
private String address;
private String birthplace;
}
Mapper文件
package com.mad.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mad.entity.Student;
public interface StudentMapper extends BaseMapper<Student> {
}
测试类(单表操作)
实际测试时直接使用注解 @SpringBootTest ,studentMapper会注入失败,需要指定classes = MyBatisPlusApplication.class
其中 students.forEach(System.out::println); 是JDK8新增的语法,类似 lambda的语法糖
此版本的条件查询使用的是QueryWrapper,之前的版本是EntityMapper
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mad.MyBatisPlusApplication;
import com.mad.entity.Student;
import com.mad.mapper.StudentMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyBatisPlusApplication.class)
public class SampleTest {
@Autowired
StudentMapper studentMapper;
// mybatis-plus的单表操作
@Test
public void test(){
// 根据id查询时需要给实体类的对应主键的属性添加注解 @TableId
// 1、根据id查询单条
Student student = studentMapper.selectById(1);
System.out.println(student);
// 2、根据id批量查询
List<Integer> idList = new ArrayList<>();
idList.add(2);
idList.add(3);
List<Student> ids = studentMapper.selectBatchIds(idList);
ids.forEach(i -> System.out.println("根据id批量查询:"+i));
// 3、无条件查询所有数据
List<Student> students = studentMapper.selectList(null);
// students.forEach(System.out::println);
students.forEach(i -> System.out.println("无条件查询所有数据:"+i));
// 4、使用Map封装条件参数查询
Map<String,Object> map = new HashMap<>();
map.put("sname","小王");
map.put("age",18);
List<Student> mapList = studentMapper.selectByMap(map);
mapList.forEach(i -> System.out.println("使用Map封装条件参数查询:"+i));
// 5、无条件分页查询
Page<Student> studentPage = studentMapper.selectPage(new Page<>(1, 3), null);
List<Student> records = studentPage.getRecords();
List<OrderItem> orders = studentPage.getOrders();
records.forEach(i -> System.out.println("无条件分页查询:"+i));
orders.forEach(i -> System.out.println("无条件分页查询orders:"+i));
// 6、条件查询单条数据(若查询结果为多条则报错:TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2)
Student wrapper01 = studentMapper.selectOne(new QueryWrapper<Student>()
.eq("sname", "小王")
.like("department", "信息")
.eq("sid",1));
System.out.println("条件查询单条数据:"+wrapper01);
// 7、条件查询多条数据 且 指定结果集
List<Student> wrapper02 = studentMapper.selectList(new QueryWrapper<Student>()
.select("sname", "address")
.between("age", 20, 25)
.ne("sex", "女"));
wrapper02.forEach(i -> System.out.println("条件查询多条数据-指定结果集:"+i));
// 8、根据id更新
Student update01 = new Student();
update01.setSid(1);
update01.setDepartment("信息工程");
studentMapper.updateById(update01);
// 9、根据条件更新
Student update02 = new Student();
update02.setAddress("四川");
studentMapper.update(update02 , new UpdateWrapper<Student>()
.eq("address","重庆"));
// delete 和 insert 同理,可根据方法名直接理解如何使用
}
}