接口mapper需要继承BaseMapper<要操作的类>外加@Mapper

mport org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface AdminUserMapper extendsBaseMapper<AdminUserEntity> {
AdminUserEntity:
@TableName("openapi_admin_user")
@Data
public class AdminUserEntity extends BaseEntity {
@TableId("user_id")
private Long userId;
private String userName;
private String userPassword;
private String userType;
private String status;
private String telephone;
private String isInitPwd;
private Long renterId; /**
* 登录用户 及新增用户的创建人
*/
private String createBy;

BaseEntity:

@Data
@ApiModel("基础实体类")
public abstract class BaseEntity { @ApiModelProperty(value = "创建人")
private String createBy; @ApiModelProperty(value = "更新人")
private String updateBy; @ApiModelProperty(value = "更新时间")
@JsonSerialize(using = JsonDateSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date dateUpdated; @ApiModelProperty(value = "创建时间")
@JsonSerialize(using = JsonDateSerializer.class)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date dateCreated; }

1:如果有涉及到输入框的查询时,分页

Controller:

@ApiOperation("查询角色列表")
@PostMapping("/list")
public ResponseDto<Page<RoleResponse>> listRoles(@RequestBody @Valid PageWrapper<RoleRequest> pageRequest) {
log.info("/roles/list start");
log.info("【/roles/list】调用roleService层入参: " + pageRequest);
Page page = new Page(pageRequest.getCurrent(), pageRequest.getSize());
if(pageRequest.getDataBody() == null) {
throw new ServiceException(ResponseCode.ValidationError.getCode(), "缺少参数dataBody");
}
Page<RoleResponse> list = roleService.selectRoleListByCondition(page, pageRequest.getDataBody());
log.info("/roles/list end");
return ResponseUtil.success(list); }

PageWrapper:

 */
@Data
public class PageWrapper<T> {
@NumberFormat
private int current = 1;
private int size = 10;
private int total;
private int pages; @Valid
private T dataBody;
}

Repository:(无排序)

*/
public Page<RoleResponse> selectRoleListByCondition(Page pageRequest, RoleRequest request) {
List<RoleResponse> responseList = new ArrayList<>(0); Pagination page = getPageParam(pageRequest); // condition
RoleEntity param = new RoleEntity();
EntityWrapper<RoleEntity> wrapper = new EntityWrapper<>(param);
wrapper.like(StringUtils.isNotBlank(request.getRoleName()), "aa.role_name", request.getRoleName());
wrapper.ge(request.getStart() != null, "aa.date_created", request.getStart() + " 00:00:00");
/* if (StringUtils.isNotEmpty(request.getEnd())) {
wrapper.le("aa.date_updated", request.getEnd() + " 23:59:59");
}*/
wrapper.le(request.getEnd() != null,"aa.date_created", request.getEnd() + " 23:59:59");
wrapper.eq(request.getRoleId() != null, "aa.role_id", request.getRoleId());
log.info("selectRoleListByCondition:: {}", wrapper.getSqlSegment()); List<RoleResponse> entities = null;
try {
// 查询角色列表
entities = roleMapper.selectRolesByPage(page, wrapper);
if (CollectionUtils.isEmpty(entities)) {
log.warn("No roles were found.");
return buildResponse(page, responseList);
}
private Page<RoleResponse> buildResponse(Pagination page, List<RoleResponse> responses) {
Page<RoleResponse> result = new Page<>(page.getCurrent(), page.getSize());
result.setRecords(responses);
result.setTotal(page.getTotal());
return result;
}
 

Repository(排序)

public List<UserResponse> listUsers(UserRequest request) {
AdminUserEntity userEntity = new AdminUserEntity();
EntityWrapper<AdminUserEntity> ew = new EntityWrapper<>(userEntity);
ew.eq("status", CommonFlagEnum.Yes.getCode()).eq(StringUtils.isNotBlank(request.getCompanyName()),
"company_name", request.getCompanyName()).eq(StringUtils.isNotBlank(request.getUserType()),
"user_type", request.getUserType()).eq(StringUtils.isNotBlank(request.getUserName()),
"um_id", request.getUserName()).like(StringUtils.isNotBlank(request.getUserName()),
"user_name", request.getUserName());
//排序列
List<String> orderColumns = new ArrayList<>();
orderColumns.add("date_created"); ew.orderDesc(orderColumns);
log.info("sql: {}, {}", ew.getSqlSelect(), ew.getSqlSegment()); try {
List<AdminUserEntity> entities = userMapper.selectList(ew);

Mapper:

List<RoleResponse> selectRolesByPage(Pagination pagination, @Param("ew") Wrapper<RoleEntity> wrapper);

xml:

<select id="selectRolesByPage" resultType="com.paic.ocss.gateway.model.dto.role.RoleResponse">
SELECT
aa.role_id,
aa.role_name,
aa.STATUS,
aa.date_created,
aa.create_by,
aa.date_updated,
aa.update_by,
aa.role_desc,
bb.resource_ids
FROM
openapi_admin_role aa
LEFT JOIN ( SELECT GROUP_CONCAT( r.resource_id ) resource_ids, r.role_id FROM
openapi_admin_role_resource_mapping r GROUP BY r.role_id ) bb ON aa.role_id = bb.role_id
<where>
${ew.sqlSegment}
</where>
order by date_created desc </select>

2:如果没有涉及到输入框的查询时,分页,而是简单的增,删,改,查:

public void modify(UserModify userRequest) {

    AdminUserEntity userEntity = new AdminUserEntity();
userEntity.setUserId(userRequest.getUserId());
if (StringUtils.isNotBlank(userRequest.getTelephone())) {
userEntity.setTelephone(userRequest.getTelephone());
}
if (StringUtils.isNotBlank(userRequest.getUserName())) {
userEntity.setUserName(userRequest.getUserName());
}
if (StringUtils.isNotBlank(userRequest.getStatus())) {
userEntity.setStatus(userRequest.getStatus());
}
if (userRequest.getRenterId() != null) {
userEntity.setRenterId(userRequest.getRenterId());
} String loginAccount = SecurityUtils.getSubject().getPrincipal().toString();
userEntity.setUpdateBy(loginAccount); if (CommonFlagEnum.Yes.getCode().equals(userRequest.getIsInitPwd())) {
//存储到db的密码
userEntity.setUserPassword(SHA256Util.getSHA256StrJava(DEFAULT_PASSWORD));
} else if(userRequest.getUserPassword() != null){
userEntity.setUserPassword(SHA256Util.getSHA256StrJava(userRequest.getUserPassword()));
}
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
//userEntity.setDateUpdated(format.format(new Date()));
userEntity.setDateUpdated(new Date()); try {
int count = userMapper.updateById(userEntity);

Mybatis-plus的使用的更多相关文章

  1. 【分享】标准springMVC+mybatis项目maven搭建最精简教程

    文章由来:公司有个实习同学需要做毕业设计,不会搭建环境,我就代劳了,顺便分享给刚入门的小伙伴,我是自学的JAVA,所以我懂的.... (大图直接观看显示很模糊,请在图片上点击右键然后在新窗口打开看) ...

  2. Java MyBatis 插入数据库返回主键

    最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...

  3. [原创]mybatis中整合ehcache缓存框架的使用

    mybatis整合ehcache缓存框架的使用 mybaits的二级缓存是mapper范围级别,除了在SqlMapConfig.xml设置二级缓存的总开关,还要在具体的mapper.xml中开启二级缓 ...

  4. 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程

    本文将讲解SSM框架的基本搭建集成,并有一个简单demo案例 说明:1.本文暂未使用maven集成,jar包需要手动导入. 2.本文为基础教程,大神切勿见笑. 3.如果对您学习有帮助,欢迎各种转载,注 ...

  5. mybatis plugins实现项目【全局】读写分离

    在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...

  6. MyBatis基础入门--知识点总结

    对原生态jdbc程序的问题总结 下面是一个传统的jdbc连接oracle数据库的标准代码: public static void main(String[] args) throws Exceptio ...

  7. Mybatis XML配置

    Mybatis常用带有禁用缓存的XML配置 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ...

  8. MyBatis源码分析(一)开篇

    源码学习的好处不用多说,Mybatis源码量少.逻辑简单,将写个系列文章来学习. SqlSession Mybatis的使用入口位于org.apache.ibatis.session包中的SqlSes ...

  9. (整理)MyBatis入门教程(一)

    本文转载: http://www.cnblogs.com/hellokitty1/p/5216025.html#3591383 本人文笔不行,根据上面博客内容引导,自己整理了一些东西 首先给大家推荐几 ...

  10. MyBatis6:MyBatis集成Spring事物管理(下篇)

    前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...

随机推荐

  1. 网页学习:day1

    初始准备: Write some function Write a titie Write a article Write some button Button function写法: functio ...

  2. python执行unittest界面设置

    执行单元测试时,系统会自动添加unittest in...的执行服务器. 执行时unittest in...的执行服务器在界面右上方可以看到,且执行结果为左侧框和右侧统计结果. 如果没有,会导致测试结 ...

  3. [小米OJ] 7. 第一个缺失正数

    思路: 参考这个思路 即:将每个数字放在对应的第几个位置上,比如1放在第1个位置上,2放在第2个位置上. 注意几个点:将每个数放在它正确的位置,前提是该数是正数,并且该数小于序列长度,并且交换的两个数 ...

  4. python函数闭包-装饰器-03

    可调用对象 callable()  # 可调用的(这个东西加括号可以执行特定的功能,类和函数) 可调用对象即  callable(对象)  返回为  True  的对象 x = 1 print(cal ...

  5. 学习16内容# 1.自定义模块 # 2.time # 3.datetime # 4.random

    模块的定义与分类 模块是什么? ​ 这几天,我们进入模块的学习.在学习模块之前,我们首先要知道,什么是模块? ​ 一个函数封装一个功能,你使用的软件可能就是由n多个函数组成的(先不考虑面向对象).比如 ...

  6. Linux基础之定时任务

    30.1)什么是定时任务 定时任务命令是cond,crond就是计划任务,类似于我们平时生活中的闹钟,定点执行. 30.2)为什么要用crond 计划任务主要是做一些周期性的任务,比如凌晨3点定时备份 ...

  7. django第四次(转自刘江)

    我们都知道对于ManyToMany字段,Django采用的是第三张中间表的方式.通过这第三张表,来关联ManyToMany的双方.下面我们根据一个具体的例子,详细解说中间表的使用. 一.默认中间表 首 ...

  8. 【Spring】The matching wildcard is strict……

    applicationContext.xml 文件抛出了这个异常信息. 解决方法: 需要在 namespace 后加上对应的 schemaLocation,如下所示: <?xml version ...

  9. PID算法通俗理解,平衡车,倒立摆,适合不理解PID算法的人来看!

    先插句广告,本人QQ522414928,不熟悉PID算法的可以一起交流学习,随时在线(PID资料再我的另一篇博客里) 倒立摆资料连接↓ https://www.cnblogs.com/LiuXinyu ...

  10. JDK的命令行工具系列 (二) javap、jinfo、jmap

    javap: 反编译工具, 可用来查看java编译器生成的字节码 参数摘要: -help 帮助 -l 输出行和变量的表 -public 只输出public方法和域 -protected 只输出publ ...