init
This commit is contained in:
227
src/main/java/com/newyetai/tkmybatis/mybatis/MyMapper.java
Normal file
227
src/main/java/com/newyetai/tkmybatis/mybatis/MyMapper.java
Normal file
@@ -0,0 +1,227 @@
|
||||
package com.newyetai.tkmybatis.mybatis;
|
||||
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.newyetai.common.entity.BaseEntity;
|
||||
import com.newyetai.common.util.UserContextHolder;
|
||||
|
||||
/**
|
||||
*
|
||||
* 定义自己的通用mapper
|
||||
*
|
||||
* @author guhong
|
||||
* @date 2019/8/19
|
||||
*/
|
||||
public interface MyMapper<T extends BaseEntity> extends /*Mapper<T>, ExistsMapper<T>,*/BaseMapper<T> {
|
||||
|
||||
/**
|
||||
* 设置新增属性值
|
||||
* @param entity
|
||||
* @param operateUserId
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default void setCreateProperty(T entity, Long operateUserId) {
|
||||
entity.setCreateBy(operateUserId);
|
||||
entity.setCreateTime(new Date());
|
||||
entity.setUpdateBy(null);
|
||||
entity.setUpdateTime(null);
|
||||
entity.setDeleted(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置修改属性值
|
||||
* @param entity
|
||||
* @param operateUserId
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default void setUpdateProperty(T entity, Long operateUserId) {
|
||||
entity.setUpdateBy(operateUserId);
|
||||
entity.setUpdateTime(new Date());
|
||||
entity.setProxyBy(entity.getProxyBy());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置删除属性值
|
||||
* @param entity
|
||||
* @param operateUserId
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default void setDeleteProperty(T entity, Long operateUserId) {
|
||||
setUpdateProperty(entity, operateUserId);
|
||||
entity.setDeleted(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 新增
|
||||
* @param entity
|
||||
* @param operateUserId 操作人
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default void saveEntity(T entity, Long operateUserId) {
|
||||
setCreateProperty(entity, operateUserId);
|
||||
insert(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键更新实体,null也会被更新
|
||||
* @param entity
|
||||
* @param operateUserId 操作人
|
||||
* @return
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default int updateEntity(T entity, Long operateUserId) {
|
||||
setUpdateProperty(entity, operateUserId);
|
||||
return alwaysUpdateSomeColumnById(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键更新实体,null不会被更新
|
||||
* @param entity
|
||||
* @return
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default int updateEntitySelective(T entity) {
|
||||
return updateEntitySelective(entity, UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键更新实体,null不会被更新
|
||||
* @param entity
|
||||
* @return
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default int updateEntitySelective(T entity, Long operateUserId) {
|
||||
setUpdateProperty(entity, operateUserId);
|
||||
// 创建人/时间不能修改
|
||||
entity.setCreateBy(null);
|
||||
entity.setCreateTime(null);
|
||||
return updateById(entity);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 逻辑删除实体(实则根据主键更新实体,null不会更新)
|
||||
* @param entity
|
||||
* @param operateUserId
|
||||
* @return
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default int deleteEntity(T entity, Long operateUserId) {
|
||||
setDeleteProperty(entity, operateUserId);
|
||||
return updateById(entity);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 批量保存
|
||||
* @param entitys
|
||||
* @param operateUserId
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default void saveEntites(Collection<T> entitys, Long operateUserId) {
|
||||
entitys.forEach(entity -> saveEntity(entity, operateUserId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量保存
|
||||
* @param entitys
|
||||
* @param operateUserId
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default void saveEntites(Collection<T> entitys) {
|
||||
this.saveEntites(entitys, UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量修改(根据主键更新实体,null也会被更新)
|
||||
* @param entitys
|
||||
* @param operateUserId
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default void updateEntites(Collection<T> entitys, Long operateUserId) {
|
||||
entitys.forEach(entity -> updateEntity(entity, operateUserId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除(实则根据主键更新实体,null不会更新)
|
||||
* @param entitys
|
||||
* @param operateUserId
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default void deleteEntites(Collection<T> entitys, Long operateUserId) {
|
||||
entitys.forEach(entity -> deleteEntity(entity, operateUserId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量修改(根据主键更新实体,null不会被更新)
|
||||
* @param entity
|
||||
* @return
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default void updateEntitysSelective(Collection<T> entitys, Long operateUserId) {
|
||||
entitys.forEach(entity -> updateEntitySelective(entity, operateUserId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id获取实体
|
||||
* @param id
|
||||
* @return
|
||||
* @author sr on 2019-11-05
|
||||
*/
|
||||
default T getById(Long id) {
|
||||
T entity = selectById(id);
|
||||
return (entity == null || entity.getDeleted()) ? null : entity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断是否存在相同属性其他实体(已设置deleted=0)
|
||||
* @param record
|
||||
* @return
|
||||
* @author sr on 2019-12-09
|
||||
*/
|
||||
boolean existsEntity(@Param(Constants.ENTITY)T record);
|
||||
|
||||
|
||||
/**
|
||||
* 新增(context取用户信息)
|
||||
* @param entity
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default void saveEntity(T entity) {
|
||||
saveEntity(entity, UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键更新实体,null也会被更新(context取用户信息)
|
||||
* @param entity
|
||||
* @return
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default int updateEntity(T entity) {
|
||||
return updateEntity(entity, UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 逻辑删除实体(实则根据主键更新实体,null不会更新,context取用户信息)
|
||||
* @param entity
|
||||
* @return
|
||||
* @author sr on 2019-10-12
|
||||
*/
|
||||
default int deleteEntity(T entity) {
|
||||
return deleteEntity(entity, UserContextHolder.getLoginUserId());
|
||||
}
|
||||
|
||||
int alwaysUpdateSomeColumnById(@Param(Constants.ENTITY)T entity);
|
||||
|
||||
}
|
||||
78
src/main/java/com/newyetai/tkmybatis/pojo/Page.java
Normal file
78
src/main/java/com/newyetai/tkmybatis/pojo/Page.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package com.newyetai.tkmybatis.pojo;
|
||||
|
||||
import com.newyetai.common.dto.IPage;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @author guhong
|
||||
* @date 2019/8/19
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class Page<T> implements Serializable, IPage<T> {
|
||||
|
||||
/** **/
|
||||
private static final long serialVersionUID = -729078966440672920L;
|
||||
|
||||
/** 第几页 **/
|
||||
private int pageNo;
|
||||
/** 页宽 **/
|
||||
private int pageSize;
|
||||
/** 一共的页数 **/
|
||||
private int pageCount;
|
||||
/** 记录条数 **/
|
||||
private int totalCount;
|
||||
/** 查询出来的对象实体 **/
|
||||
private List<T> entityList;
|
||||
|
||||
public Page() {
|
||||
pageSize = 20;
|
||||
pageNo = 1;
|
||||
}
|
||||
|
||||
public Page(int pageNo, int pageSize) {
|
||||
super();
|
||||
this.pageNo = pageNo < 1 ? 1 : pageNo;
|
||||
this.pageSize = pageSize < 1 ? 20 : pageSize;
|
||||
}
|
||||
|
||||
public void setTotalCount(int totalCount) {
|
||||
pageCount = (int) Math.ceil((double) totalCount / pageSize);
|
||||
this.totalCount = totalCount;
|
||||
}
|
||||
|
||||
public int getFirstEntityIndex() {
|
||||
return (pageNo - 1) * pageSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPageNo() {
|
||||
return pageNo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPageResult(int totalCount, List<T> results) {
|
||||
this.entityList = results;
|
||||
this.setTotalCount(totalCount);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Boolean getNeedPage() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user