mybatisPlus动态sql语句 ${ew.sqlSegment}
这里主要是介绍通过MyBatis Plus使用${ew.sqlSegment}进行条件分页查询示例等,方便以后查阅!!!
一、简介
${ew.customSqlSegment}是MyBatis Plus提供的动态SQL语句拼接功能。
1、在使用MyBatis Plus进行数据库操作时,可以通过Wrapper对象来构建查询条件。Wrapper对象可以通过链式调用的方式动态添加查询条件,包括等于、大于、小于等各种条件。而${ew.customSqlSegment}就是Wrapper对象中自定义的SQL片段,可以灵活地根据业务需求进行动态拼接。
2、例如,在上述代码中,${ew.customSqlSegment}可以用于拼接额外的查询条件或者排序规则。具体的拼接逻辑可以通过在方法的参数中传入Wrapper对象来实现。
3、${ew.customSqlSegment}和${ew.sqlSegment}是什么?
${ew.customSqlSegment}在使用时,相当与 where + queryWrapper内的条件 ${ew.sqlSegment}相当于queryWrapper内的条件
二、分页条件查询
dao层
mapper-java文件
package com.wl.cloud.monitor.dao;
import com.wl.cloud.monitor.domain.StudentTest;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.wl.cloud.monitor.support.vo.StudentTestVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* test信息 Mapper接口
*
* @author wanglin
* @since 2024-03-21
*/
@Mapper
public interface StudentTestMapper extends BaseMapper
/**
* 查询test信息
*
* @return
*/
List
/**
* 分页
*
* @param page
* @param ew
* @return
*/
IPage
/**
* 列表
*
* @param ew
* @return
*/
List
/**
* 查询详情
*
* @param id
* @return
*/
StudentTestVO getById(@Param("id") String id);
}
dao-xml
mapper-xml文件
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
id, create_by, create_time, update_by, update_time, deleted, age, description, name, sex
select
from gl_student_test
order by create_time desc limit 10;
select
from id, create_by, create_time, update_by, update_time, deleted, age, description, name, sex
deleted = 0 and
${ew.sqlSegment}
select
from id, create_by, create_time, update_by, update_time, deleted, age, description, name, sex
deleted = 0 and
${ew.sqlSegment}
select
from gl_student_test
deleted = 0 and
id = #{id}
server层
StudentTestServiceImpl
package com.wl.cloud.monitor.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.wl.cloud.monitor.dao.StudentTestMapper;
import com.wl.cloud.monitor.domain.StudentTest;
import com.wl.cloud.monitor.service.StudentTestService;
import com.wl.cloud.monitor.support.dto.StudentTestDTO;
import com.wl.cloud.monitor.support.dto.query.StudentTestQueryDTO;
import com.wl.cloud.monitor.support.vo.StudentTestVO;
import com.wl.cloud.core.dto.DataStoreDTO;
import com.wl.cloud.core.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* test信息 服务实现类
*
* @author wanglin
* @since 2024-03-21
*/
@Service
public class StudentTestServiceImpl implements StudentTestService {
@Autowired
private StudentTestMapper studentTestMapper;
@Transactional(readOnly = true)
@Override
public DataStoreDTO
QueryWrapper
Page
IPage
return new DataStoreDTO(result.getTotal(), result.getRecords());
}
@Transactional(readOnly = true)
@Override
public List
QueryWrapper
PageUtils.transferSort(queryWrapper, sort);
return this.studentTestMapper.getList(queryWrapper);
}
@Transactional(rollbackFor = Exception.class)
@Override
public void save(StudentTestDTO dto) {
// TODO 唯一性字段校验
dto.setId(null);
studentTestMapper.insert(this.transferEntity(null, dto));
}
@Transactional(rollbackFor = Exception.class)
@Override
public void update(StudentTestDTO dto) {
Assert.hasText(dto.getId(), "id不能为空");
// TODO 唯一性字段校验
StudentTest entity = studentTestMapper.selectById(dto.getId());
Assert.notNull(entity, "找不到id为 " + dto.getId() + " 的记录");
studentTestMapper.updateById(this.transferEntity(entity, dto));
}
@Transactional(rollbackFor = Exception.class)
@Override
public void delete(Set
if (CollectionUtils.isNotEmpty(ids)) {
studentTestMapper.deleteBatchIds(ids);
}
}
@Transactional(readOnly = true)
@Override
public StudentTestVO get(String id) {
Assert.hasText(id, "id不能为空");
StudentTest entity = studentTestMapper.selectById(id);
Assert.notNull(entity, "找不到id为 " + id + " 的记录");
return this.transferVo(entity);
}
private QueryWrapper
QueryWrapper
if (Objects.nonNull(queryDto)) {
queryWrapper.lambda().eq(StringUtils.isNotBlank(queryDto.getId()), StudentTest::getId, queryDto.getId());
}
return queryWrapper;
}
private StudentTest transferEntity(StudentTest entity, StudentTestDTO dto) {
if (Objects.isNull(entity)) {
entity = new StudentTest();
}
BeanUtils.copyProperties(dto, entity);
return entity;
}
private List
if (CollectionUtils.isEmpty(entities)) {
return Lists.newArrayList();
}
List
StudentTestVO vo = new StudentTestVO();
BeanUtils.copyProperties(entity, vo);
return vo;
}).collect(Collectors.toList());
return voList;
}
private StudentTestVO transferVo(StudentTest entity) {
if (Objects.isNull(entity)) {
return null;
}
StudentTestVO vo = new StudentTestVO();
BeanUtils.copyProperties(entity, vo);
return vo;
}
}
关注林哥,持续更新哦!!!★,°:.☆( ̄▽ ̄)/$:.°★ 。
参考阅读
发表评论