Springboot接入MyBatisPlus
1、什么是MyBatisPlus?
Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
通过封装一些基础通用的curd方法,我们不用再在xml文件中编写sql语句,就可以直接调用api进行对数据库的操作。
2、MyBatisPlus环境准备
2.1 创建一个springboot项目,在pom文件下添加如下依赖:
-
<dependency>
-
<groupId>com.baomidou</groupId>
-
<artifactId>mybatis-plus-boot-starter</artifactId>
-
<version>3.4.1</version>
-
</dependency>
2.2 创建一个包用来存放 mapper 文件
2.3 在 Spring Boot 主类上面使用 @MapperScan 注解扫描我们自定义的 mapper
2.4 自定义自己的 mapper,该 mapper 将继承 com.baomidou.mybatisplus.core.mapper.BaseMapper
2.5 在我们的服务中使用 @Autowired 注解自动注入自定义的 mapper
3、具体使用
3.1 基础使用
-
-
public interface OnlinePriceMapper extends BaseMapper<OnlinePrice> {
-
-
}
mapper中没有定义任何方法
-
package com.online.analyze.service.impl;
-
-
import com.online.analyze.mapper.OnlinePriceMapper;
-
import com.online.analyze.model.OnlinePrice;
-
import org.junit.jupiter.api.Test;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.boot.test.context.SpringBootTest;
-
-
import java.util.Date;
-
-
/**
-
* @author lijianxi
-
* @date 2022年03月18日 11:13 上午
-
*/
-
@SpringBootTest
-
public class MapperTest {
-
@Autowired
-
OnlinePriceMapper onlinePriceMapper;
-
-
@Test
-
public void testMapper() {
-
OnlinePrice onlinePrice = new OnlinePrice();
-
onlinePrice.setId(3999222L);
-
onlinePrice.setAddDate(new Date());
-
onlinePrice.setDescription("test");
-
onlinePrice.setSummary("test");
-
onlinePrice.setProcessMethod("修改数据");
-
//新增
-
int result = onlinePriceMapper.insert(onlinePrice);
-
if (result > 0) {
-
System.out.println("插入成功");
-
}
-
//查询
-
OnlinePrice price = onlinePriceMapper.selectById(3999222L);
-
System.out.println(price.getDescription() "查询成功");
-
//更新
-
onlinePrice.setDescription("修改后");
-
onlinePriceMapper.updateById(onlinePrice);
-
System.out.println(onlinePriceMapper.selectById(3999222L).getDescription() "修改成功");
-
//删除
-
if (onlinePriceMapper.deleteById(3999222L) > 0) {
-
System.out.println("删除成功");
-
}
-
-
-
}
-
}
通过注入mapper,就可以使用 mapper 的 insert(插入)、selectById(根据ID查找)、updateById(根据ID更新)和 deleteById(根据ID删除)等简单的 CRUD 操作
常用的增删改查方法可以查看BaseMapper接口
-
//
-
// Source code recreated from a .class file by IntelliJ IDEA
-
// (powered by FernFlower decompiler)
-
//
-
-
package com.baomidou.mybatisplus.core.mapper;
-
-
import com.baomidou.mybatisplus.core.conditions.Wrapper;
-
import com.baomidou.mybatisplus.core.metadata.IPage;
-
import java.io.Serializable;
-
import java.util.Collection;
-
import java.util.List;
-
import java.util.Map;
-
import org.apache.ibatis.annotations.Param;
-
-
public interface BaseMapper<T> extends Mapper<T> {
-
int insert(T entity);
-
-
int deleteById(Serializable id);
-
-
int deleteByMap(; Map<String, Object> columnMap)
-
-
int delete(; Wrapper<T> queryWrapper)
-
-
int deleteBatchIds(; Collection<? extends Serializable> idList)
-
-
int updateById(; T entity)
-
-
int update(; T entity, Wrapper<T> updateWrapper)
-
-
T selectById(Serializable id);
-
-
List<T> selectBatchIds(; Collection<? extends Serializable> idList)
-
-
List<T> selectByMap(; Map<String, Object> columnMap)
-
-
T selectOne(; Wrapper<T> queryWrapper)
-
-
Integer selectCount(; Wrapper<T> queryWrapper)
-
-
List<T> selectList(; Wrapper<T> queryWrapper)
-
-
List<Map<String, Object>> selectMaps(; Wrapper<T> queryWrapper)
-
-
List<Object> selectObjs(; Wrapper<T> queryWrapper)
-
-
<E extends IPage<T>> E selectPage(E page, ; Wrapper<T> queryWrapper)
-
-
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, ; Wrapper<T> queryWrapper)
-
}
3.2 wapper构建
除了通过已定义的方法来查询还可以通过 Wrapper 构建查询条件
-
@Test
-
public void testMapper(){
-
//查询id为3999222数据信息
-
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
-
queryWrapper.eq("id", 3999222L);
-
OnlinePrice onlinePrice = onlinePriceMapper.selectOne(queryWrapper);
-
System.out.println(onlinePrice.getId());
-
//查询添加日期在区间内并且user_city不为null的数据
-
QueryWrapper<OnlinePrice> queryWrapper1 = new QueryWrapper<>();
-
queryWrapper1.between("add_date","2022-02-02","2022-02-10");
-
queryWrapper1.isNotNull("user_city");
-
String summary ="test";
-
// 第一个参数为是否执行条件,为true则执行该条件
-
queryWrapper1.eq(StringUtils.isNotBlank(summary),"summary",summary);
-
List<OnlinePrice> onlinePrices = onlinePriceMapper.selectList(queryWrapper1);
-
for (OnlinePrice price : onlinePrices) {
-
System.out.println(price);
-
}
-
}
3.3分页功能
-
@Test
-
public void testPage() {
-
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
-
queryWrapper.isNotNull("description");
-
//每页四个
-
Page<OnlinePrice> page = new Page<>(1, 4);
-
Page<OnlinePrice> onlinePricePage = onlinePriceMapper.selectPage(page, queryWrapper);
-
for (OnlinePrice record : page.getRecords()) {
-
System.out.println(record);
-
}
-
}
3.4 service层接口
除了 BaseMapper 接口,MyBatis Plus 还提供了 IService 接口,该接口对应 Service 层。MyBatis Plus 的通用 Service CRUD 实现了 IService 接口,进一步封装 CRUD
该接口使用 get(查询单行)、remove(删除)、list(查询集合)和 page(分页)前缀命名的方式进行区别。
-
@Autowired
-
OnlinePriceService onlinePriceService;
-
@Test
-
public void testService(){
-
OnlinePrice price = new OnlinePrice();
-
price.setId(22222222L);
-
price.setDescription("test");
-
onlinePriceService.save(price);
-
QueryWrapper<OnlinePrice> queryWrapper = new QueryWrapper<>();
-
queryWrapper.eq("description","test");
-
OnlinePrice one = onlinePriceService.getOne(queryWrapper);
-
Page<OnlinePrice> page = new Page<>(1,4);
-
QueryWrapper<OnlinePrice> queryWrapper1 = new QueryWrapper<>();
-
queryWrapper1.between("add_date", "2022-02-02", "2022-02-10");
-
onlinePriceService.page(page,queryWrapper1);
-
}
以上介绍了mybatisplus的常用基础用法,mybatisplus还有自动代码生成等其他功能,可以自动生成代码,以后有空继续学习分享
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhfaik
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01