实体公共类
package com.demo.commons.bases;
import com.demo.ServletInitializer;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
@Getter
@Setter
@Entity
@JsonInclude
@ApiModel(value = "Base公共实体类")
public class BaseEntity extends ServletInitializer {
private static final long serialVersionUID = 1L;
@Id
@ApiModelProperty(value = "主键ID")
@GeneratedValue(generator = "JDBC")
private Long objectId;
@ApiModelProperty(value = "创建人")
private String createBy;
@ApiModelProperty(value = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
@ApiModelProperty(value = "更新人")
private String updateBy;
@ApiModelProperty("更新时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
}
dao 公共类
package com.demo.commons.bases;
import org.apache.ibatis.annotations.Param;
import tk.mybatis.mapper.common.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T>, IdsMapper<T>, ConditionMapper<T>, ExampleMapper<T> {
// 特别注意,该接口不能被扫描到,否则会出错
// 最后在启动类中通过MapperScan注解指定扫描的mapper路径:
/**
* 页面列表.模糊查询
*/
List<T> getList(Map<String, Object> map);
/**
* 获取表信息
*/
List<LinkedHashMap<String, String>> getTableInfo(@Param("tableName") String tableName);
}
service接口公共类
package com.demo.commons.bases;
import org.apache.ibatis.session.RowBounds;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public interface BaseService<T> {
/**
* 1: 保存一个实体,null的属性也会保存,不会使用数据库默认值
*/
int save(T entity);
/**
* 2: 保存一个实体,null的属性不会保存,会使用数据库默认值
*/
int saveNotNull(T entity);
/**
* 3: 插入数据,限制为实体包含`id`属性并且必须为自增列,实体配置的主键策略无效
*/
int saveGeneratedKeys(T entity);
/**
* 4: 批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含`id`属性并且必须为自增列
*/
int saveList(List<T> list);
/**
* 5: 单个实体对象删除
*/
void deleteByObject(T entity);
/**
* 6: 根据主键字段进行删除,方法参数必须包含完整的主键属性
*/
void deleteById(Long key);
/**
* 7: 根据主键@Id进行删除,多个Id以逗号,分割
*/
void deleteByIds(String ids);
/**
* 8: 根据Example条件删除数据,返回删除的条数
*/
int deleteByExample(Example example);
/**
* 9: 根据Condition条件删除数据,返回删除的条数
*/
int deleteByCondition(Condition condition);
/**
* 10: 据主键更新实体全部字段,null值会被更新
*/
int updateById(T entity);
/**
* 11: 根据主键更新属性不为null的值
*/
void updateNotNullById(T entity);
/**
* 12: 根据Condition条件更新实体entity包含的全部属性,null值会被更新,返回更新的条数
*/
int updateByCondition(T entity, Condition condition);
/**
* 13: 根据Example条件更新实体entity包含的全部属性,null值会被更新,返回更新的条数
*/
int updateByExample(T entity, Example example);
/**
* 14: 根据Condition条件更新实体entity包含的全部属性,null值会被更新,返回更新的条数
*/
int updateByConditionSelective(T entity, Condition condition);
/**
* 15: 根据Example条件更新实体`record`包含的不是null的属性值,返回更新的条数
*/
int updateByExampleSelective(T entity, Example example);
/**
* 16: 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
*/
T getByObject(T entity);
/**
* 17: 根据Example条件进行查询,若有多条数据则抛出异常
*/
T getOneByExample(Example example);
/**
* 18: 根据实体中的属性进行条件查询,
*/
List<T> getByExample(Example example);
/**
* 19: 根据主键@Id进行查询,多个Id以逗号,分割
*/
List<T> getByIds(String ids);
/**
* 20: 根据实体中的属性值进行查询,查询条件使用等号
*/
List<T> getByObjects(T entity);
/**
* 21: 根据Condition条件进行查询
*/
List<T> getByCondition(Condition condition);
/**
* 21: 查询全部结果
*/
List<T> getAll();
/**
* 23: 根据实体中的属性查询总数,查询条件使用等号
*/
int getCount(T entity);
/**
* 24: 根据Example条件进行查询总数
*/
int getCountByExample(Example example);
/**
* 25: 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
*/
T getById(Long key);
/**
* 26: 根据条件分页查询
*/
List<T> getByPages(Example example, RowBounds rowBounds);
/**
* 27: 根据主键字段查询总数,方法参数必须包含完整的主键属性,查询条件使用等号
*/
boolean isUnique(Long key);
/**
* 28: 根据零件明细查询基础数据
*/
List<LinkedHashMap<String, String>> getTableInfo(String tableName);
/**
* 30: 页面列表.模糊查询
*/
List<T> getList(Map<String, Object> map);
/*
* 其他...
*/
}
service实现公共类
package com.baorun.dms.commons.bases;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public abstract class BaseImpl<T> implements BaseService<T> {
@Autowired
protected BaseMapper<T> mapper;
/**
* 1: 保存一个实体,null的属性也会保存,不会使用数据库默认值
*/
@Override
public int save(T entity) {
return mapper.insert(entity);
}
/**
* 2: 保存一个实体,null的属性不会保存,会使用数据库默认值
*/
@Override
public int saveNotNull(T entity) {
return mapper.insertSelective(entity);
}
/**
* 3: 插入数据,限制为实体包含`id`属性并且必须为自增列,实体配置的主键策略无效
*/
@Override
public int saveGeneratedKeys(T entity) {
return mapper.insertUseGeneratedKeys(entity);
}
/**
* 4: 批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含`id`属性并且必须为自增列
*/
@Override
public int saveList(List<T> list) {
return mapper.insertList(list);
}
/**
* 5: 根据主键字段进行删除,方法参数必须包含完整的主键属性
*/
@Override
public void deleteByObject(T entity) {
mapper.delete(entity);
}
/**
* 6: 根据主键字段进行删除,方法参数必须包含完整的主键属性
*/
@Override
public void deleteById(Long key) {
mapper.deleteByPrimaryKey(key);
}
/**
* 7: 根据主键@Id进行删除,多个Id以逗号,分割
*/
@Override
public void deleteByIds(String ids) {
mapper.deleteByIds(ids);
}
/**
* 8: 根据Example条件删除数据,返回删除的条数
*/
@Override
public int deleteByExample(Example example) {
return mapper.deleteByExample(example);
}
/**
* 9: 根据Condition条件删除数据,返回删除的条数
*/
@Override
public int deleteByCondition(Condition condition) {
return mapper.deleteByCondition(condition);
}
/**
* 10: 据主键更新实体全部字段,null值会被更新
*/
@Override
public int updateById(T entity) {
return mapper.updateByPrimaryKey(entity);
}
/**
* 11: 根据主键更新属性不为null的值
*/
@Override
public void updateNotNullById(T entity) {
mapper.updateByPrimaryKeySelective(entity);
}
/**
* 12: 根据Condition条件更新实体entity包含的全部属性,null值会被更新,返回更新的条数
*/
@Override
public int updateByCondition(T entity, Condition condition) {
return mapper.updateByCondition(entity, condition);
}
/**
* 13: 根据Example条件更新实体entity包含的全部属性,null值会被更新,返回更新的条数
*/
@Override
public int updateByExample(T entity, Example example) {
return mapper.updateByExample(entity, example);
}
/**
* 14: 根据Condition条件更新实体entity包含的全部属性,null值会被更新,返回更新的条数
*/
@Override
public int updateByConditionSelective(T entity, Condition condition) {
return mapper.updateByConditionSelective(entity, condition);
}
/**
* 15: 根据Example条件更新实体`record`包含的不是null的属性值,返回更新的条数
*/
@Override
public int updateByExampleSelective(T entity, Example example) {
return mapper.updateByExampleSelective(entity, example);
}
/**
* 16: 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
*/
@Override
public T getByObject(T entity) {
return mapper.selectOne(entity);
}
/**
* 17: 根据Example条件进行查询,若有多条数据则抛出异常
*/
@Override
public T getOneByExample(Example example) {
return mapper.selectOneByExample(example);
}
/**
* 18: 根据实体中的属性进行条件查询,
*/
@Override
public List<T> getByExample(Example example) {
return mapper.selectByExample(example);
}
/**
* 19: 根据主键@Id进行查询,多个Id以逗号,分割
*/
@Override
public List<T> getByIds(String ids) {
return mapper.selectByIds(ids);
}
/**
* 20: 根据实体中的属性值进行查询,查询条件使用等号
*/
@Override
public List<T> getByObjects(T entity) {
return mapper.select(entity);
}
/**
* 21: 根据Condition条件进行查询
*/
@Override
public List<T> getByCondition(Condition condition) {
return mapper.selectByCondition(condition);
}
/**
* 22: 查询全部结果
*/
@Override
public List<T> getAll() {
return mapper.selectAll();
}
/**
* 23: 根据实体中的属性查询总数,查询条件使用等号
*/
@Override
public int getCount(T entity) {
return mapper.selectCount(entity);
}
/**
* 24: 根据Example条件进行查询总数
*/
@Override
public int getCountByExample(Example example) {
return mapper.selectCountByExample(example);
}
/**
* 25: 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
*/
@Override
public T getById(Long objectId) {
return mapper.selectByPrimaryKey(objectId);
}
/**
* 26: 根据条件分页查询
*/
@Override
public List<T> getByPages(Example example, RowBounds rowBounds) {
return mapper.selectByExampleAndRowBounds(example, rowBounds);
}
/**
* 27: 根据主键字段查询总数,方法参数必须包含完整的主键属性,查询条件使用等号
*/
@Override
public boolean isUnique(Long objectId) {
return mapper.existsWithPrimaryKey(objectId);
}
/**
* 28: 查询列表数据
*/
@Override
public List<LinkedHashMap<String, String>> getTableInfo(String tableName) {
return null;
}
/**
* 29: 页面列表.模糊查询
*/
@Override
public List<T> getList(Map<String, Object> map) {
return null;
}
}
Maven依赖
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>4.2.1</version>
</dependency>