spring boot jpa 自定义返回实体

spring boot jpa 自定义返回实体

Entity-A定义

import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

@Data
@Entity
@Table(name = "USER_INFO")
public class UserInfoDo implements Serializable {
    private static final long serialVersionUID = 2723403345407921497L;
    
    @Id
    @Column(name = "USERNAME", nullable = false, length = 20)
    private String userName;
    
    @Column(name = "USESTATUS")
    private Long useStatus;
}

Entity-B定义

import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

@Data
@Entity
@Table(name = "USER_AUTH")
public class UserAuthDo implements Serializable {
    private static final long serialVersionUID = 34523452345234L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "UserAuthSeq")
    @SequenceGenerator(name = "UserAuthSeq", sequenceName = "USER_AUTH_SEQ", allocationSize = 1)
    @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
    private Long id;

    @Column(name = "USERNAME", nullable = false, length = 20)
    private String userName;

    @Column(name = "USERTYPE", length = 10)
    private String userType;

    @Column(name = "IP", length = 500)
    private String ip;
    
    @Column(name = "CREATETIME")
    private Date createTime;

    
}

ABBean自定义返回实体

import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Date;

@Data
@AllArgsConstructor
public class ABBean {
    private String userName;
    private Long userInfoUseStatus;
    private Long ID;
    private String userType;
    private Date createTime;
    private String ip;
}

Repository定义

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface UserRepository extends JpaRepository<UserInfoDo, String>, JpaSpecificationExecutor<UserInfoDo> {
		/*1.自定义实体必须写清楚包
		*2.查询返回顺序必须与实体属性定义顺序一致
		*3.自定义实体属性类型必须与Entity一致
		*/
    @Query("select new  com.ABBean(a.userName ,a.useStatus , b.id, b.userType,b.createTime ,b.ip " +
            "from UserInfoDo a " +
            "LEFT JOIN UserAuthDo b on a.userName = b.userName " +
            "where (:userName is null or a.userName like :userName) " +
            "and (:userInfoUseStatus is null or a.useStatus = :userInfoUseStatus) " +
            "and (:createTime is null or b.createTime= :createTime) ")
    Page<ABBean> QueryUserAuth(@Param("userName ") String userName,                     
                                                  @Param("userInfoUseStatus") Long userInfoUseStatus,
                                                  @Param("createTime") Date createTime,
                                                  Pageable page);
}

Service 定义

 public Page<ABBean> queryUserInfo(QueryUserInfoRqDto rq) {
 			//排序属性 与 查询语句对应
        Order createTimeSort = new Order(Sort.Direction.DESC, "b.createTime");
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(createTimeSort);
        Sort sort = Sort.by(orders);
        Pageable pageable = PageRequest.of(Integer.valueOf(rq.getPage()) - 1, Integer.valueOf(rq.getLimit()), sort);
        String userName=null;
        Long userInfoUseStatus=null;
        if (!StringUtils.isEmpty(rq.getUserName())) {
            userName= "%" + rq.getUserName() + "%";
        }
        if (!StringUtils.isEmpty(rq.getUserInfoUseStatus())) {
            userInfoUseStatus=Long.valueOf(rq.getUserInfoUseStatus());
        }
        return userRepository.QueryUserAuth(userName,
                userInfoUseStatus,
                rq.getCreateTime(),
                pageable);
    }
上一篇:Java:将xml文件里的内容,提取成entity,转存数据库中


下一篇:empty()方法