目录
MyBatisPlus是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率。
1.入门案例
①创建SpringBoot项目,添加Spring Web和Mysql driver
②手动在pom.xml文件中添加mybatis plus和druid依赖
<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</artifactId>
<version>1.2.18</version>
</dependency>
③创建数据库mybatis,创建User表。
④JDBC配置(application.yml)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mp?serverTimezone=UTC
username: root
password: root
⑤制作实体类(类名与数据库表名类似,类的属性和表字段对应)
domain.User.java
package com.example.domain;
public class User {
private long id;
private int age;
private String name, password, tel;
public long getId() {
return id;
}
@java.lang.Override
public java.lang.String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", name='" + name + '\'' +
", password='" + password + '\'' +
", tel='" + tel + '\'' +
'}';
}
public void setId(long id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
⑥定义数据接口,继承BaseMapper
dao.UserDao.java
package com.example.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper<User> {
}
⑦测试类测试功能
package com.example;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MpDemoApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testGetAll() {
List<User> userList = userDao.selectList(null);
System.out.println(userList);
}
}
2. 标准数据层CRUD开发
测试类
package com.example;
import com.example.dao.UserDao;
import com.example.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MpDemoApplicationTests {
@Autowired
private UserDao userDao;
@Test
void testSave() {
User user = new User();
user.setName("鼠鼠");
user.setPassword("666");
user.setAge(13);
user.setTel("88888");
userDao.insert(user);
}
@Test
void testDelete() {
userDao.deleteById(4);
}
@Test
void testUpdate() {
User user = new User();
user.setId(1L);
user.setName("Tom88");
user.setPassword("tom888");
userDao.updateById(user);
}
@Test
void testGetById() {
User user = userDao.selectById(2L);
System.out.println(user);
}
@Test
void testGetAll() {
List<User> userList = userDao.selectList(null);
System.out.println(userList);
}
}
3.Lombok
Lombok,一个Java类库,提供了一组注解,简化POJO实体类的开发。
在pom.xml中添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
domain.User.java
package com.example.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
public class User {
private long id;
private int age;
private String name, password, tel;
}
或
package com.example.domain;
import lombok.Data;
@Data
public class User {
private long id;
private int age;
private String name, password, tel;
}
4.分页查询
设置分页拦截器作为Spring管理的bean。
config.Mpconfig.java
package com.example.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpInterceper() {
// 1. 定义Mp拦截器
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return mpInterceptor;
}
}
测试类
@Test
void testGetByPage() {
IPage page = new Page(1, 2);
userDao.selectPage(page, null);
System.out.println("当前页码值:" + page.getCurrent());
System.out.println("每页显示数:" + page.getSize());
System.out.println("一共多少页:" + page.getPages());
System.out.println("一共多少条数据:" + page.getTotal());
System.out.println("数据:" + page.getRecords());
}