一、pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/>
</parent>
<groupId>com.kd</groupId>
<artifactId>MyBatis-Plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyBatis-Plus</name>
<description>MyBatis-Plus</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/store?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.initial-size=20
spring.datasource.druid.min-idle=10
spring.datasource.druid.max-active=100
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.kd.mybatisplus.pojo
三、Product
package com.kd.mybatisplus.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
@TableName("t_product")
public class Product implements Serializable {
private Integer id;
@TableField("category_id")
private Integer categoryId;
@TableField("item_type")
private String itemType;
private String title;
@TableField("sell_point")
private String sellPoint;
private Long price;
private Integer num;
private String image;
private Integer status;
private Integer priority;
@TableField(value = "created_user", fill = FieldFill.INSERT)
private String createdUser;
@TableField(value = "created_time", fill = FieldFill.INSERT)
private Date createdTime;
@TableField(value = "modified_user", fill = FieldFill.INSERT_UPDATE)
private String modifiedUser;
@TableField(value = "modified_time", fill = FieldFill.INSERT_UPDATE)
private Date modifiedTime;
}
四、ProductMapper
package com.kd.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kd.mybatisplus.entity.Product;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ProductMapper extends BaseMapper<Product> {
}
五、MyMetaObjectHandler
package com.kd.mybatisplus.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createdTime", Date.class, new Date());
this.strictInsertFill(metaObject, "modifiedTime", Date.class, new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "modifiedTime", Date.class, new Date());
}
}
六、所用案例:MyBatisPlusApplicationTests
package com.kd.mybatisplus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kd.mybatisplus.entity.Product;
import com.kd.mybatisplus.mapper.ProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@SpringBootTest
class MyBatisPlusApplicationTests {
@Autowired
private ProductMapper productMapper;
@Test
void insert() {
Product product = new Product();
product.setTitle("小米");
product.setPrice(2999L);
product.setItemType("旗舰店");
int row = productMapper.insert(product);
log.info("Product--新增数据", row);
}
@Test
void deleteById() {
int row = productMapper.deleteById(1700704257);
log.info("Product--删除数据", row);
}
@Test
void deleteBatchIds() {
ArrayList<Integer> idList = new ArrayList<>();
idList.add(1700704257);
idList.add(228503554);
int row = productMapper.deleteBatchIds(idList);
log.info("Product--批量删除数据", row);
}
@Test
void deleteByMap() {
Map<String, Object> colunm = new HashMap<>();
colunm.put("title", "小米");
colunm.put("price", 2999L);
int row = productMapper.deleteByMap(colunm);
log.info("Product--对象删除数据", row);
}
@Test
void delete() {
UpdateWrapper<Product> wrapper = new UpdateWrapper<>();
wrapper.gt("price", 2000).or().likeRight("title", "小米");
int row = productMapper.delete(wrapper);
log.info("Product--条件构造器:删除数据", row);
}
@Test
void updateById() {
Product product = new Product();
product.setId(10000001);
product.setTitle("小米");
int row = productMapper.updateById(product);
log.info("Product--根据Id更新数据", row);
}
@Test
void update() {
Product product = new Product();
product.setId(10000001);
UpdateWrapper<Product> wrapper = new UpdateWrapper<>();
wrapper.gt("price", 80).set("title", "小米").set("price", "3999").set(false, "num", null);
int row = productMapper.update(product, wrapper);
log.info("Product--更新数据", row);
}
@Test
void selectById() {
Product product = productMapper.selectById(10000001);
log.info("Product--根据ID查询", product);
System.err.println(product);
}
@Test
void selectBatchIds() {
List<Integer> ids = new ArrayList<>();
ids.add(10000001);
ids.add(228503554);
List<Product> products = productMapper.selectBatchIds(ids);
log.info("Product--根据ID批量查询", products);
products.forEach(System.out::println);
}
@Test
void selectByMap() {
Map<String, Object> colunm = new HashMap<>();
colunm.put("title", "小米");
List<Product> products = productMapper.selectByMap(colunm);
log.info("Product--根据columnMap条件", products);
products.forEach(System.out::println);
}
@Test
void selectList() {
UpdateWrapper<Product> wrapper = new UpdateWrapper<>();
wrapper.gt("price", 1999).eq("title", "小米");
List<Product> products = productMapper.selectList(wrapper);
log.info("Product--根据 entity 条件,查询全部记录", products);
products.forEach(System.out::println);
}
@Test
void selectPage() {
IPage<Product> page = new Page<>(1, 5);
QueryWrapper<Product> wrapper = new QueryWrapper<>();
wrapper.eq("status", 1);
IPage<Product> pageInfo = productMapper.selectPage(page, wrapper);
List<Product> products = pageInfo.getRecords();
products.forEach(System.out::println);
log.info("Product--根据 entity 条件,查询全部记录(并翻页)", products);
}
}