MybatisPlus使用条件构造器增删改查功能的实现方法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MybatisPlus使⽤条件构造器增删改查功能的实现⽅法java后端层级结构
Controller 接⼝层
接⼝层⽐较好理解,它是⾯向web⽹络的接⼝,使⽤http格式去调⽤
/**
* 图⽂课程管理Controller
*/
@RestController
@RequestMapping("/driver/imageCourse")
public class TImageCourseController extends BaseController {
@Autowired
private ITImageCourseService tImageCourseService;
@Autowired
private TImageCourseMapper tImageCourseMapper;
// 具体接⼝...
}
Service 业务层
在实际应⽤中,更复杂的逻辑应该写在Service业务层⽅法中,在业务⽅法中再调⽤数据层⽅法,实现从接⼝层-业务层-数据层的链路调⽤关系,提⾼代码的可读性
/**
* 图⽂课程管理Service接⼝
*/
public interface ITImageCourseService extends IService<TImageCourse> {
}
业务层实现
/**
* 图⽂课程管理Service业务层处理
*/
@Service
public class TImageCourseServiceImpl extends ServiceImpl<TImageCourseMapper, TImageCourse> implements ITImageCourseService {
@Autowired
private TImageCourseMapper tImageCourseMapper;
}
ServiceImpl类实现了IService接⼝中的⽅法;ServiceImpl中的⽅法,本质上是对BaseMapper⽅法的封装,同时也增加了⼀些BaseMapper类中没有的特性,例如常⽤的list()、count()⽅法
// Service⽅法调⽤了Mapper⽅法只是将insert()返回转换成了布尔值
@Override
public boolean save(T entity) {
return retBool(baseMapper.insert(entity));
}
Mapper 数据层
继承BaseMapper接⼝后,⽆需编写 mapper.xml ⽂件,即可获得CRUD功能;例如,insert()、deleteById()、updateById()、selectById()等⽅法
如果⼿动编写数据层的sql,BaseMapper实现者即对应xml中的sql⽅法
/**
* 图⽂课程管理Mapper接⼝
*/
public interface TImageCourseMapper extends BaseMapper<TImageCourse> {
}
**mapper.xml **
xml内容例⼦,该例⼦⾃定义了⼀个根据id的查询⽅法,⽆视了删除标志
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/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返回给实体
/**
* 新增图⽂课程管理
*/
@PostMapping
public 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()⽅法也可以接收⼀个条件构造器作为参数
构造器的使⽤
条件构造器包含QueryWrapper和LambdaQueryWrapper两个类。
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)
构造器⽅法
/**
* 源码
* @param condition 执⾏条件可省略
* @param column 字段
* @param val 值
*/
eq(boolean condition, R column, Object val)
eq相等=
ne不等于!=
gt⼤于>
ge⼤于等于>=
lt⼩于<
le⼩于等于<=
between BETWEEN 值1 AND 值2
like LIKE ‘%值%'
notLike NOT LIKE ‘%值%'
likeLeft LIKE ‘%值' ;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()⽅法更新实体,只有字段内容不为空,才会触发字段内容的修改
/**
* 修改图⽂课程管理
*/
@PutMapping
public 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使⽤条件构造器增删改查的⽂章就介绍到这了,更多相关Mybatis Plus条件构造器增删改查内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。