iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Mybatis Plus使用条件构造器增删改查功能的示例分析
  • 189
分享到

Mybatis Plus使用条件构造器增删改查功能的示例分析

2023-06-15 00:06:55 189人浏览 八月长安
摘要

这篇文章主要介绍mybatis Plus使用条件构造器增删改查功能的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!java后端层级结构Controller 接口层接口层比较好理解,它是面向WEB网络的接口,使

这篇文章主要介绍mybatis Plus使用条件构造器增删改查功能的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

java后端层级结构


Controller 接口层

接口层比较好理解,它是面向WEB网络的接口,使用Http格式去调用

@RestController@RequestMapping("/driver/imageCourse")public class TImageCourseController extends BaseController {        @Autowired    private ITImageCourseService tImageCourseService;    @Autowired    private TImageCourseMapper tImageCourseMapper;        // 具体接口...}

Service 业务层

在实际应用中,更复杂的逻辑应该写在 Service 业务层方法中,在业务方法中再调用数据层方法,实现从 接口层-业务层-数据层 的链路调用关系,提高代码的可读性

public interface ITImageCourseService extends IService<TImageCourse> {}

业务层实现

@Servicepublic class TImageCourseServiceImpl extends ServiceImpl<TImageCourseMapper, TImageCourse> implements ITImageCourseService {    @Autowired    private TImageCourseMapper tImageCourseMapper;}

ServiceImpl 类实现了 IService 接口中的方法;ServiceImpl 中的方法,本质上是对 BaseMapper 方法的封装,同时也增加了一些 BaseMapper 类中没有的特性,例如常用的 list()count() 方法

// Service方法调用了Mapper方法 只是将insert()返回转换成了布尔值@Overridepublic boolean save(T entity) {    return retBool(baseMapper.insert(entity));}

Mapper 数据层

继承 BaseMapper 接口后,无需编写 mapper.xml 文件,即可获得CRUD功能;例如,insert()deleteById()updateById()selectById() 等方法

如果手动编写数据层的sql,BaseMapper实现者即对应xml中的sql方法

public interface TImageCourseMapper extends BaseMapper<TImageCourse> {}

**mapper.xml **

xml内容例子,该例子自定义了一个根据id的查询方法,无视了删除标志

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.ruoyi.mapper.TRuralInfoMapper">    <resultMap type="TRuralInfo" id="RuralInfoResult">        <id     property="id"             column="id"           />        <result property="cityName"       column="city_name"    />        <result property="countyName"     column="county_name"  />        <result property="townName"       column="town_name"    />        <result property="villageName"    column="village_name" />        <result property="checkCode"      column="check_code"   />        <result property="parentLevel"    column="parent_level" />        <result property="parentId"       column="parent_id"    />        <result property="delFlag"        column="del_flag"     />        <result property="createBy"       column="create_by"    />        <result property="createTime"     column="create_time"  />        <result property="updateBy"       column="update_by"    />        <result property="updateTime"     column="update_time"  />    </resultMap>    <sql id="selectRuralInfoVo">        select t_rural_info.id, city_name, county_name, town_name, village_name, check_code, parent_level, parent_id,        t_rural_info.del_flag, t_rural_info.create_by, t_rural_info.create_time, t_rural_info.update_by, t_rural_info.update_time        from t_rural_info    </sql>    <select id="getRuralInfoById" parameterType="Long" resultMap="RuralInfoResult">        <include refid="selectRuralInfoVo"/>        where id = #{id}    </select></mapper>

增删改查


新增(C)

使用 mapper 对象的 insert() 方法新增一条记录,成果后会将数据库的id返回给实体

@PostMappingpublic ajaxResult add(@RequestBody TImageCourse tImageCourse){        ...    return toAjax(tImageCourseMapper.insert(tImageCourse));}

saveBatch

service 类中提供了 saveBatch() 方法,可实现批量插入,该方法是支持事务

saveOrUpdate

service 类中提供了 saveOrUpdate() 方法,如果id为空则调用 save() 方法保存,反之则调用 updateById() 方法更新

查询(R)

查询多数要借助条件构造器使用才有意义,实现更灵活的查询;

查询实体

常用的方法有 .getOne()getById() ;

.getOne() 接收一个条件构造器作为参数

getById() 根据id进行查询实体

查询集合

常用的查询方法包括 .list()

.list() 方法也可以接收一个条件构造器作为参数

构造器的使用

条件构造器包含 QueryWrapperLambdaQueryWrapper 两个类。

LambdaQueryWrapper 为函数式编程的书写习惯,与 QueryWrapper 表达的意义相同,优点是简化了代码。

此处以 LambdaQueryWrapper 的使用为例,常用的三种方法:

// 1、直接用new创建// 创建对象的方式会更加灵活,可配合 if()...else 达到更灵活的sql拼接LambdaQueryWrapper<TCenterPoint> wrapper = new LambdaQueryWrapper<>();wrapper.eq(TCenterPoint::getPoint, 10.0);// 2、静态方法创建 Wrappers.<>lambdaQuery()// 构造器方法多为链式编程 可连写Wrappers.<TCenterPoint>lambdaQuery().eq(TCenterPoint::getPoint, 10.0)// 3、静态方法创建 Wrappers.query() // query可接受对象 字段不为null则自动拼接.eq()方法Wrappers.query(tUserDetail)

构造器方法


eq(boolean condition, R column, Object val)
eq相等=
ne不等于!=
gt大于>
ge大于等于>=
lt小于<
le小于等于<=
betweenBETWEEN 值1 AND 值2
likeLIKE ‘%值%'
notLikeNOT LIKE ‘%值%'
likeLeftLIKE ‘%值' ;likeRight同理
isNull字段 IS NULL;
orderByAsc排序:ORDER BY 字段, … ASC;orderByDesc同理

在sql中使用and和or,逻辑只需写在where中即可,在ORM框架中较为不好理解,总之,其结果是实现一个查询条件和多个条件并列的关系

OR

or(Consumer<Param> consumer)or(boolean condition, Consumer<Param> consumer)

OR 嵌套,例如

// or (name = '李白' and status <> '活着')or(i -> i.eq("name", "李白").ne("status", "活着"))

AND

and(Consumer<Param> consumer)and(boolean condition, Consumer<Param> consumer)

AND 嵌套,例如

// and (name = '李白' and status <> '活着')and(i -> i.eq("name", "李白").ne("status", "活着"))

修改(U)

使用 mapper 对象的 updateById() 方法更新实体,只有字段内容不为空,才会触发字段内容的修改

@PutMappingpublic AjaxResult edit(@RequestBody TImageCourse tImageCourse){    return toAjax(tImageCourseMapper.updateById(tImageCourse));}

删除(D)

删除常用的方法是根据id进行删除,使用 mapper 对象的 deleteById ,框架也支持批量删除的操作 deleteBatchIds

@DeleteMapping("/{ids}")public AjaxResult remove(@PathVariable Long[] ids) {    return toAjax(tImageCourseMapper.deleteBatchIds(Arrays.asList(ids)));}

以上是“Mybatis Plus使用条件构造器增删改查功能的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!

--结束END--

本文标题: Mybatis Plus使用条件构造器增删改查功能的示例分析

本文链接: https://www.lsjlt.com/news/276413.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作