简介
官网介绍,MyBatis-Plus(简称 MP)是一个 [MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。详细了解见官网:https://baomidou.com/
引入依赖
引入主要的依赖:
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
配置分页插件
package com.elite.springboot.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mybatisplus配置类
*/
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
/**
* 针对 update 和 delete 语句 作用: 阻止恶意的全表更新删除
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
}
启动类同样需要配置扫描mapper包
@SpringBootApplication
@MapperScan("com.elite.springboot.mapper")
public class SpringbootMybatisPlusApp
{
public static void main( String[] args ) {
SpringApplication.run(SpringbootMybatisPlusApp.class,args);
}
}
实体类
由于我的mysql版本是8.0.30,注解@TableName 需要指定表名并添加"``",否则会报语法错误,需要指定主键ID,表字段和实体类字段一致可以不用指定。
@Data
@Getter
@Setter
//注意表明的大小写
@TableName("`order`")
public class Order {
//订单ID
@TableId(value = "order_id",type = IdType.AUTO)
private Integer order_id;
//订单号
private Integer order_no;
//商品编号
private Integer product_id;
//用户
private Integer user_id;
//'订单产品数量'
private Integer order_num ;
//'订单金额'
private BigDecimal order_amt ;
//订单状态 0待确认,1已确认,2已收货,3已取消,4已完成,5已作废
private String order_status;
//支付状态 0待支付,1已支付,2部分支付,3已退款,4拒绝退款
private String pay_status;
//创建时间
private Date create_time;
//更新时间
private Date update_time;
//创建用户
private String create_user;
//更新用户
private String update_user;
}
映射类Mapper
/**
* 定义的一个Mapper类,继承mybatisplus封装的类BaseMapper
*/
public interface OrderMapper extends BaseMapper<Order> {
}
MybatisPlus官方提供核心接口BaseMapper,只要继承与指定实体类,我们这边就可以进行用提供的核心接口操作数据库了。核心接口源码如下:
int insert(T entity);
int deleteById(Serializable id);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
int delete(@Param("ew") Wrapper<T> queryWrapper);
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
T selectById(Serializable id);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
T selectOne(@Param("ew") Wrapper<T> queryWrapper);
Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
<E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
测试类
/**
* 测试订单类
*/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
@Resource
OrderMapper orderMapper;
/**
* 保存订单
*/
@Test
public void SaveOrder() {
Order order = new Order();
order.setOrder_no(3);
order.setProduct_id(3);
order.setUser_id(3);
order.setOrder_num(2);
order.setOrder_amt(BigDecimal.valueOf(30));
order.setOrder_status("下单");
order.setPay_status("未支付");
order.setCreate_user("plus");
order.setUpdate_user("plus");
//调用新增订单方法Insert
orderMapper.insert(order);
}
/**
* 更新订单数据
*/
@Test
public void UpdateOrder() {
Order order = new Order();
order.setOrder_id(6);
order.setOrder_status("发货");
order.setPay_status("支付");
int count = orderMapper.updateById(order);
if (count > 0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败");
}
}
/**
* 根据订单ID获取订单 SELECT order_id,order_no,product_id,user_id,order_num,
* order_amt,order_status,pay_status,create_time,update_time,create_user,update_user FROM `order` WHERE order_id=?
*
*/
@Test
public void GetOrder() {
Order order = orderMapper.selectById(5);
System.out.println(order);
}
/**
* 获取订单列表
* SELECT order_id,order_no,product_id,user_id,order_num,order_amt,order_status,
* pay_status,create_time,update_time,create_user,update_user FROM `order`
*/
@Test
public void GetOrderList() {
QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//可以指定查询的条件
//QueryWrapper(LambdaQueryWrapper) 和 UpdateWrapper(LambdaUpdateWrapper) 的父类
//可以指定条件
//queryWrapper.ge("order_no","2");//大于等于 >=
//le
//like
//nolike 等具体参考 https://baomidou.com/pages/10c804/#abstractwrapper
List<Order> orderList = orderMapper.selectList(queryWrapper);
for( Order order :orderList){
System.out.println(order);
}
}
/**
* 删除订单
*/
@Test
public void DelOrder() {
int count = orderMapper.deleteById(7);
if (count > 0 ){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
}