springboot——集成mybatis-plus(通用CRUD方法)
SpringBoot集成mybatis-plus
1. maven依赖
<!-- mybaits plus 插件 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
2.创建表
CREATE TABLE `customer` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`customer_name` varchar(255) DEFAULT NULL COMMENT '顾客名称',
`gender` varchar(255) DEFAULT NULL COMMENT '性别',
`telephone` varchar(255) DEFAULT NULL COMMENT '电话号码',
`register_time` timestamp NULL DEFAULT NULL COMMENT '注册时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 COMMENT='顾客表';
3.yml配置
### mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
# 如果使用baseMapper就注释mapper-locations,没有文件则会报错
#mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.ys.dao
4.entity
这里使用了lombok:使用可参考:https://www.cnblogs.com/yu-si/articles/15079874.html
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("customer")// @TableName映射数据库表名
public class Customer {
//@TableId 标识这是Id 主键
@TableId(type = IdType.AUTO)
private Long id;
// 如果字段和数据库列对应可省略该注解
// @TableField(value = "NAME",exist = true)
private String customerName;
private String gender;
private String telephone;
private String registerTime;
}
5.MybatisPlusPageConfig.java
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//@MapperScan("com.zszxz.plus.service.*.mapper*")
@MapperScan("com.ys.dao")
public class MybatisPlusPageConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInterceptor.setLimit(500);
return paginationInterceptor;
}
}
6.mapper.java
继承BaseMapper<>
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ys.entity.mybatisplus.Customer;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface CustomerMapper extends BaseMapper<Customer> {
}
7.Test测试类
package com.ys;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ys.dao.CustomerMapper;
import com.ys.entity.mybatisplus.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Resource
CustomerMapper customerMapper;
@Test
public void add() {
Customer customer = new Customer();
customer.setGender("男");
customer.setTelephone("66566");
int i;
for (i = 1; i < 20; i++) {
customer.setCustomerName("大牛"+i);
customerMapper.insert(customer);
}
System.out.println("============执行"+i+"条============");
}
@Test
public void update() {
Customer customer = new Customer();
customer.setId(0L);
customer.setCustomerName("二牛");
customer.setGender("男");
customer.setTelephone("66566");
customerMapper.updateById(customer);
}
@Test
public void delete() {
// customerMapper.deleteById(24L);//根据id删除
// 条件封装
QueryWrapper<Customer> wrapper = new QueryWrapper<Customer>();
wrapper.ge(true,"id",0L);
int delete = customerMapper.delete(wrapper);
System.out.println("============执行"+delete+"条============");
}
@Test
public void selectById() {
Customer customer = customerMapper.selectById(26L);
// Customer(id=26, customerName=大牛哥, gender=男, telephone=66566, registerTime=null)
System.out.println(customer);
}
@Test
public void selectByWrapper() {
// 条件封装
QueryWrapper<Customer> wrapper = new QueryWrapper<Customer>();
wrapper.eq("id",26L);
List<Customer> customers = customerMapper.selectList(wrapper);
// [Customer(id=26, customerName=大牛哥, gender=男, telephone=66566, registerTime=null)]
System.out.println(customers);
}
@Test
public void page() {
Page<Customer> page = new Page<>(1, 10);
IPage<Customer> customerIPage = customerMapper.selectPage(page, null);
List<Customer> records = customerIPage.getRecords();
for (Customer record : records) {
System.out.println("========="+record.toString());
}
}
}