在spring boot中使用mybatis过程中,发现有个实体的时间字段未在mapper方法执行完的封装结果中进行封装,但是却有值返回。
如下展示问题:
实体如下:
package com.sxd.swapping.domain; import com.sxd.swapping.base.BaseBean; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.StringUtils; import org.springframework.data.jpa.domain.Specification; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.io.Serializable; import java.util.*; @Entity @Table @Getter @Setter public class HuaYangArea extends BaseBean implements Serializable { private static final long serialVersionUID = -1851783771574739215L; @Column(nullable = false) private String areaName; @Column(nullable = false) private Long areaPerson; public static Specification<HuaYangArea> where(HuaYangArea huaYangArea){ return new Specification<HuaYangArea>() { @Override public Predicate toPredicate(Root<HuaYangArea> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { //创建查询列表 List<Predicate> predicates = new ArrayList<>(); //字段areaName是否查询 String areaName = huaYangArea.getAreaName(); if (StringUtils.isNotBlank(areaName)){ predicates.add(criteriaBuilder.like(root.get("areaName"),"%"+areaName+"%")); } //字段areaPerson是否查询 Long areaPerson = huaYangArea.getAreaPerson(); if (areaPerson != null) { predicates.add(criteriaBuilder.equal(root.get("areaPerson"),areaPerson)); } return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction(); } }; } }
package com.sxd.swapping.base; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import javax.persistence.*; import java.util.*; import java.util.stream.Collectors; /** * 基础bean */ @MappedSuperclass public class BaseBean { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;// 主键 自增 @Column(nullable = false, updatable = false) private Date createDate = new Date();// 创建时间 private Date updateDate = new Date();// 修改时间 private String updateId; // 修改人 @Column(nullable = false) private String createId; // 创建人 @Column(nullable = false) private String uid; //业务主键 @Transient private Integer pageNum = 0; //分页 页号 @Transient private Integer pageSize = 10; //分页 页量 public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Date getUpdateDate() { return updateDate; } public void setUpdateDate(Date updateDate) { this.updateDate = updateDate; } public String getUpdateId() { return updateId; } public void setUpdateId(String updateId) { this.updateId = updateId; } public String getCreateId() { return createId; } public void setCreateId(String createId) { this.createId = createId; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public Integer getPageNum() { return pageNum; } public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public void initEntity(){ this.createDate = new Date(); this.createId = UUID.randomUUID().toString();//如果有当前登陆人,则初始化为当前登陆人 this.uid = UUID.randomUUID().toString(); } public void updateEntity(){ this.updateDate = new Date(); this.updateId = UUID.randomUUID().toString();//如果有当前登陆人,则赋值为当前登陆人 } /** * 分页 工具方法 * @return */ public PageRequest page(){ return PageRequest.of(pageNum,this.pageSize); } /** * 分页 排序工具方法 * * 中文字段排序 需要在查询出来后处理 sort无法解决中文排序的问题 * @param map * @param obj * @return * @throws Exception */ public PageRequest page(Map<String,Sort.Direction> map,Object obj) throws Exception{ //反射获取实体所有属性 List<String> properties = Arrays.stream(obj.getClass().getDeclaredFields()).map(i->i.getName()).collect(Collectors.toList()); Set<String> keySet = map.keySet(); Sort sort = null; if (properties.containsAll(keySet)){ for (String str:keySet){ if (sort == null){ sort = Sort.by(map.get(str),str); }else{ sort = sort.and(Sort.by(map.get(str),str)); } } }else{ throw new Exception("排序字段非本实体字段"); } return PageRequest.of(this.pageNum,this.pageSize,sort); } }
Mapper.java如下:
public interface HuaYangAreaMapper { /** * 根据业务主键查询 * @param uid * @return */ @Select("SELECT * FROM hua_yang_area where uid = #{uid}") @Results({ @Result(property = "areaName",column = "area_name",javaType = String.class), @Result(property = "areaPerson",column = "area_person",javaType = Long.class), @Result(property = "createId",column = "create_id",javaType = String.class) }) HuaYangArea findOne(String uid);
调用这个方法,返回结果如下:
如上所示,因为createDate和updateDate都没有在结果集进行封装,所以按道理来说应该是获取不到值的嘛,但是这里却能获取到值。
,展示结果集封装
追溯原因:
最后找到根源,原来是因为在实体类中,部分不按逻辑出牌的字段 进行了new操作
所以,如果想要mybatis查询的结果集完全按照自己封装的效果来进行返回,就可以将这里的初始化给去掉!!!!