最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot JPA?CriteriaBuilder?如何獲取指定字段

 更新時間:2021年12月07日 08:51:29   作者:sunlight  
這篇文章主要介紹了springboot JPA CriteriaBuilder如何獲取指定字段,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

springboot JPA CriteriaBuilder獲取指定字段

廢話不說直接貼代碼

public class ActivityVO implements Serializable {
 private static final long serialVersionUID = 1L;  
 private int id;
 private String name;
 private String thumb; 
 public ActivityVO(int id, String name, String thumb) {
  this.id    = id;
  this.name  = name;
  this.thumb = thumb;
 }
 
 public ActivityVO(){ 
 }
 
 public int getId() {
  return id;
 }
 
 public void setId(int id) {
  this.id = id;
 }
 
 public String getName() {
  return name;
 }
 
 public void setName(String name) {
  this.name = name;
 }
 
 public String getThumb() {
  return thumb;
 }
 
 public void setThumb(String thumb) {
  this.thumb = thumb;
 }  
}
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction; 
import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Objects; 
 
/**
 * The persistent class for the activity database table.
 *
 */
@Entity
@Table(name="activity")
//@JsonIgnoreProperties({"user"})
//@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
//@NamedQuery(name = "Task.findByTaskName", query = "select t from Task t where t.taskName = ?1")
public class ActivityEntity implements Serializable {
	private static final long serialVersionUID = 1L; 
	public ActivityEntity(int id, String name, String thumb) {
		this.id                  = id;
		this.name                = name;
	}
 
	private int        id;
	private Integer    userId;
	private String     name;
	private String     thumb;
	private byte       type;
	private byte       mode;
	private String     notes;
	private String     content;
	private BigDecimal deposit;
	private byte       status;
	private int        productNum;
	private Integer    participantNum;
	private Integer    wxId;
	private Integer    mpId;
	private String     mpRelpyKeyword;
	private BigInteger startTime;
	private byte       lotteryType;
	private BigInteger lotteryTime;
	private int        maxParticipantNum;
	private byte       enableBlacklist;
    private byte enableSharing;
	private byte isVisible;
	private BigInteger createTime;
	private BigInteger updateTime; 
	public ActivityEntity() {
	}
 
	@Id
	@Column(name = "id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public int getId() {
		return id;
	}
 
	public void setId(int id) {
		this.id = id;
	}
 
	@Basic
	@Column(name = "user_id")
	public Integer getUserId() {
		return userId;
	}
 
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
 
	@Basic
	@Column(name = "name")
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	@Basic
	@Column(name = "thumb")
	public String getThumb() {
		return thumb;
	}
 
	public void setThumb(String thumb) {
		this.thumb = thumb;
	}
 
	@Basic
	@Column(name = "type")
	public byte getType() {
		return type;
	}
 
	public void setType(byte type) {
		this.type = type;
	}
 
	@Basic
	@Column(name = "mode")
	public byte getMode() {
		return mode;
	}
 
	public void setMode(byte mode) {
		this.mode = mode;
	}
 
	@Basic
	@Column(name = "notes")
	public String getNotes() {
		return notes;
	}
 
	public void setNotes(String notes) {
		this.notes = notes;
	}
 
	@Basic
	@Column(name = "content")
	public String getContent() {
		return content;
	}
 
	public void setContent(String content) {
		this.content = content;
	}
 
	@Basic
	@Column(name = "deposit")
	public BigDecimal getDeposit() {
		return deposit;
	}
 
	public void setDeposit(BigDecimal deposit) {
		this.deposit = deposit;
	}
 
	@Basic
	@Column(name = "status")
	public byte getStatus() {
		return status;
	}
 
	public void setStatus(byte status) {
		this.status = status;
	}
 
	@Basic
	@Column(name = "product_num")
	public int getProductNum() {
		return productNum;
	}
 
	public void setProductNum(int productNum) {
		this.productNum = productNum;
	}
 
	@Basic
	@Column(name = "participant_num")
	public Integer getParticipantNum() {
		return participantNum;
	}
 
	public void setParticipantNum(Integer participantNum) {
		this.participantNum = participantNum;
	}
 
	@Basic
	@Column(name = "wx_id")
	public Integer getWxId() {
		return wxId;
	}
 
	public void setWxId(Integer wxId) {
		this.wxId = wxId;
	}
 
	@Basic
	@Column(name = "mp_id")
	public Integer getMpId() {
		return mpId;
	}
 
	public void setMpId(Integer mpId) {
		this.mpId = mpId;
	}
 
	@Basic
	@Column(name = "mp_relpy_keyword")
	public String getMpRelpyKeyword() {
		return mpRelpyKeyword;
	}
 
	public void setMpRelpyKeyword(String mpRelpyKeyword) {
		this.mpRelpyKeyword = mpRelpyKeyword;
	}
 
	@Basic
	@Column(name = "start_time")
	public BigInteger getStartTime() {
		return startTime;
	}
	public void setStartTime(BigInteger startTime) {
		this.startTime = startTime;
	}
 
	@Basic
	@Column(name = "lottery_type")
	public byte getLotteryType() {
		return lotteryType;
	}
 
	public void setLotteryType(byte lotteryType) {
		this.lotteryType = lotteryType;
	}
 
	@Basic
	@Column(name = "lottery_time")
	public BigInteger getLotteryTime() {
		return lotteryTime;
	}
 
	public void setLotteryTime(BigInteger lotteryTime) {
		this.lotteryTime = lotteryTime;
	}
 
	@Basic
	@Column(name = "max_participant_num")
	public int getMaxParticipantNum() {
		return maxParticipantNum;
	} 
 
	public void setMaxParticipantNum(int maxParticipantNum) {
		this.maxParticipantNum = maxParticipantNum;
	}
 
	@Basic
	@Column(name = "enable_blacklist")
	public byte getEnableBlacklist() {
		return enableBlacklist;
	}
 
	public void setEnableBlacklist(byte enableBlacklist) {
		this.enableBlacklist = enableBlacklist;
	}
 
	@Basic
	@Column(name = "enable_sharing")
	public byte getEnableSharing() {
		return enableSharing;
	}
 
	public void setEnableSharing(byte enableSharing) {
		this.enableSharing = enableSharing;
	}
 
	@Basic
	@Column(name = "is_visible")
	public byte getIsVisible() {
		return isVisible;
	}
 
	public void setIsVisible(byte isVisible) {
		this.isVisible = isVisible;
	}
	@Basic
	@Column(name = "create_time")
	public BigInteger getCreateTime() {
		return createTime;
	}
	public void setCreateTime(BigInteger createTime) {
		this.createTime = createTime;
	}
 
	@Basic
	@Column(name = "update_time")
	public BigInteger getUpdateTime() {
		return updateTime;
	}
 
	public void setUpdateTime(BigInteger updateTime) {
		this.updateTime = updateTime;
	}
 
	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		ActivityEntity that = (ActivityEntity) o;
		return id == that.id &&
				type == that.type &&
				mode == that.mode &&
				status == that.status &&
				productNum == that.productNum &&
				lotteryType == that.lotteryType &&
				maxParticipantNum == that.maxParticipantNum &&
				enableBlacklist == that.enableBlacklist &&
				Objects.equals(userId, that.userId) &&
				Objects.equals(name, that.name) &&
				Objects.equals(thumb, that.thumb) &&
				Objects.equals(notes, that.notes) &&
				Objects.equals(content, that.content) &&
				Objects.equals(deposit, that.deposit) &&
				Objects.equals(participantNum, that.participantNum) &&
				Objects.equals(wxId, that.wxId) &&
				Objects.equals(mpId, that.mpId) &&
				Objects.equals(mpRelpyKeyword, that.mpRelpyKeyword) &&
				Objects.equals(startTime, that.startTime) &&
				Objects.equals(lotteryTime, that.lotteryTime) &&
				Objects.equals(createTime, that.createTime) &&
				Objects.equals(updateTime, that.updateTime) &&
				Objects.equals(user, that.user);
	}
 
	@Override
	public int hashCode() {
		return Objects.hash(id, userId, name, thumb, type, mode, notes, content, deposit, status, productNum, participantNum, wxId, mpId, mpRelpyKeyword, startTime, lotteryType, lotteryTime, maxParticipantNum, enableBlacklist, createTime, updateTime, user);
	}
 
	@Override
	public String toString() {
		return "ActivityEntity{" +
				"id=" + id +
				", userId=" + userId +
				", name='" + name + '\'' +
				", thumb='" + thumb + '\'' +
				", type=" + type +
				", mode=" + mode +
				", notes='" + notes + '\'' +
				", content='" + content + '\'' +
				", deposit=" + deposit +
				", status=" + status +
				", productNum=" + productNum +
				", participantNum=" + participantNum +
				", wxId=" + wxId +
				", mpId=" + mpId +
				", mpRelpyKeyword='" + mpRelpyKeyword + '\'' +
				", startTime=" + startTime +
				", lotteryType=" + lotteryType +
				", lotteryTime=" + lotteryTime +
				", maxParticipantNum=" + maxParticipantNum +
				", enableBlacklist=" + enableBlacklist +
				", createTime=" + createTime +
				", updateTime=" + updateTime +
				", user=" + user +
				'}';
	}
}
 @PersistenceContext//@Autowired和@PersistenceContext注解任取一
    EntityManager entityManager;
    public List<ActivityVO> findAllByPage(Pageable pageable) throws Exception {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Tuple> criteriaQuery = criteriaBuilder.createTupleQuery();
        Root<ActivityEntity> root = criteriaQuery.from(ActivityEntity.class);
        criteriaQuery.multiselect( root.get("id"), root.get("name"),root.get("thumb") );
 
        Predicate restrictions = criteriaBuilder.conjunction();
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("id"), 1));
        criteriaQuery.where(restrictions);
        criteriaQuery.orderBy(criteriaBuilder.desc(root.get("id")));
 
        TypedQuery<Tuple> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); 
        List<Tuple> tuples = query.getResultList();
 
        ActivityVO adminWrapper = null;
        List<ActivityVO> lists = new ArrayList<ActivityVO>();
        for (Tuple t : tuples) {
            adminWrapper = new ActivityVO();
            adminWrapper.setId((Integer) t.get(0));
            adminWrapper.setName((String)t.get(1));
            adminWrapper.setThumb((String)t.get(2));
            lists.add(adminWrapper);
        }
        return lists;  
    }  
}

Java JPA CriteriaBuilder使用

一個復(fù)雜的查詢例子

包含常用的所有查詢方法

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();     //查詢結(jié)果所需要的類型(Entity相對應(yīng))
        CriteriaQuery<Entity> criteriaQuery = criteriaBuilder.createQuery(Entity.class);     //查詢所需要的主體類(Entity0相對應(yīng))
        Root<Entity0> root = criteriaQuery.from(Entity0.class);     //查詢結(jié)果-select(此處查詢所有符合條件的主體類)
        criteriaQuery.select(root);     //過濾條件用Predicate方法拼接
        Predicate restrictions = criteriaBuilder.conjunction();     //過濾條件——equal(當(dāng)Entity0關(guān)聯(lián)member類時,Entity0:member=m:1)
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
        //過濾條件——like
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.like(root.<String>get("str"), "%"+str+"%"));
        //用戶名查詢(member里面的username匹配) ———— 多層查詢 ———— 子查詢的一種:適用于m:1或1:1(即多對一或一對一)
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.like(root.get("member").<String>get("username"), "%"+username+"%"));
        //子查詢(規(guī)范寫法,先判斷查詢內(nèi)容是否存在)(適用于1:m)(即一對多)
        if (searchType != null || searchValue != null || hasExpired != null || status != null || type != null || isPendingReceive != null || isPendingRefunds != null || isAllocatedStock != null || businessType != null) {
            //建立子查詢        Subquery<Order> orderSubquery = criteriaQuery.subquery(Order.class);
            Root<Order> orderSubqueryRoot = orderSubquery.from(Order.class);
            orderSubquery.select(orderSubqueryRoot);       //子查詢和父查詢相關(guān)聯(lián)
            Predicate orderRestrictions = criteriaBuilder.equal(orderSubqueryRoot.<MergeOrder>get("mergeOrder"), root);
            //子查詢過濾條件拼接
            if (searchType != null && searchValue != null) {if ("phone".equals(searchType)) {
                    orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.like(orderSubqueryRoot.<String>get("phone"), "%"+searchValue+"%"));
                }
            }if (type != null) {
                CriteriaBuilder.In<Order.Type> in = criteriaBuilder.in(orderSubqueryRoot.<Order.Type>get("type"));
                in.value(type);
                orderRestrictions = criteriaBuilder.and(orderRestrictions, in);
            }
            //and、or以及判斷是否為null,比較(>)的使用(比較可以用于日期比較)
            if (hasExpired != null) {
                orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.or(orderSubqueryRoot.get("expire").isNull(), criteriaBuilder.greaterThan(orderSubqueryRoot.<Date>get("expire"), new Date())));
            }
            // not的使用方法(不符合上述過濾條件),notEqual的使用,<(小于)的使用
            if (isPendingReceive != null) {
                restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("paymentMethodType"), PaymentMethod.Type.cashOnDelivery));
                Predicate predicate = criteriaBuilder.and(criteriaBuilder.or(orderSubqueryRoot.get("expire").isNull()
                        , criteriaBuilder.greaterThan(orderSubqueryRoot.<Date>get("expire"), new Date()))
                        , criteriaBuilder.notEqual(orderSubqueryRoot.get("status"), Order.Status.completed)
                        , criteriaBuilder.lessThan(orderSubqueryRoot.<BigDecimal>get("amountPaid"), orderSubqueryRoot.<BigDecimal>get("amount")));
                if (isPendingReceive) {
                    orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.not(predicate));
                }
            }// 多層查詢使用if (businessType != null) {
                orderRestrictions = criteriaBuilder.and(orderRestrictions, criteriaBuilder.equal(orderSubqueryRoot.get("store").get("business").get("businessType"), businessType));
            }       // 拼接過濾條件
            orderSubquery.where(orderRestrictions);
            // 和總條件拼接(exists的使用)
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(orderSubquery));
        }
        criteriaQuery.where(restrictions);     TypedQuery<Entity> query = entityManager.createQuery(criteriaQuery);     Entity singleResult = query.getSingleResult();

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java使用RestTemplate封裝post請求方式

    java使用RestTemplate封裝post請求方式

    這篇文章主要介紹了java使用RestTemplate封裝post請求方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中easypoi的使用之導(dǎo)入校驗

    Java中easypoi的使用之導(dǎo)入校驗

    因工作需要,使用easypoi導(dǎo)入表格,并進行校驗,將表格中有問題的地方,給出提示信息,以表格形式返回,下面這篇文章主要給大家介紹了關(guān)于Java中easypoi的使用之導(dǎo)入校驗的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • Java中?!=null?的判斷

    Java中?!=null?的判斷

    空指針異常是出現(xiàn)頻率比較高的bug,在出現(xiàn)空指針時,很多小伙伴都是習(xí)慣性地加一個?!=null?的判斷,本文就來介紹一下如何使用,給感興趣的可以了解下
    2025-02-02
  • java同步開篇入門簡單介紹

    java同步開篇入門簡單介紹

    java中的CountDownLatch、Semaphore、CyclicBarrier這些類又不屬于鎖,它們和鎖又有很多共同點,都是為了協(xié)同多線程的執(zhí)行,都是一種同步器,所以這里就借用同步來取名字了,也就是“同步系列”的來源。下面小編來簡單介紹下
    2019-05-05
  • 解讀為何java中的boolean類型是32位的

    解讀為何java中的boolean類型是32位的

    這篇文章主要介紹了為何java中的boolean類型是32位的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Nacos啟動出現(xiàn)failed to req API:/nacos/v1/ns/instance after all servers問題

    Nacos啟動出現(xiàn)failed to req API:/nacos/v1/ns/insta

    這篇文章主要介紹了Nacos啟動出現(xiàn)failed to req API:/nacos/v1/ns/instance after all servers問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java實現(xiàn)FTP文件上傳與文件下載

    java實現(xiàn)FTP文件上傳與文件下載

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)FTP文件上傳與文件下載的相關(guān)資料,通過以下兩種方式實現(xiàn),通過JDK自帶的API實現(xiàn),二是通過Apache提供的API是實現(xiàn),感興趣的小伙伴們可以參考一下
    2016-04-04
  • 利用Java截取String字符串的幾種常用方法

    利用Java截取String字符串的幾種常用方法

    在開發(fā)應(yīng)用中經(jīng)常會使用到j(luò)ava string,這篇文章主要給大家介紹了關(guān)于利用Java截取String字符串的幾種常用方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Java面向?qū)ο蠡A(chǔ)之多態(tài)性,抽象類和接口

    Java面向?qū)ο蠡A(chǔ)之多態(tài)性,抽象類和接口

    這篇文章主要介紹了Java面向?qū)ο蠡A(chǔ):多態(tài)性,抽象類和接口,文中代碼可以幫助各位更好的理解學(xué)習(xí),有需求的小伙伴可以參考下
    2020-05-05
  • springboot響應(yīng)json?null值過濾方式

    springboot響應(yīng)json?null值過濾方式

    這篇文章主要介紹了springboot響應(yīng)json?null值過濾方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論

收藏| 定结县| 遵化市| 盐山县| 板桥市| 广西| 新闻| 泗阳县| 东港市| 樟树市| 衡东县| 彰武县| 齐齐哈尔市| 濮阳市| 永清县| 广德县| 河间市| 探索| 深州市| 长宁区| 安徽省| 高清| 沽源县| 朝阳区| 富蕴县| 油尖旺区| 南涧| 岱山县| 平阴县| 肇庆市| 琼中| 罗甸县| 弥渡县| 武威市| 关岭| 丹江口市| 江孜县| 滦南县| 济宁市| 临洮县| 丹寨县|