SpringBoot JPA 单/多表联合查询时遇见的!坑!

1,hibernte criteria制作投影映射

                         ProjectionList proList = Projections.projectionList();//设置投影集合 
			//SELECT * 
			//DISTINCT
			proList.add(Projections.distinct(Projections.property("userId"))); 
			//User
			proList.add( Projections.groupProperty( "userId") ,"userId");////GROUP BY
			proList.add( Projections.property( "loginName") ,"loginName" );
			proList.add( Projections.property( "accountMonkey"),"accountMonkey" );
			proList.add( Projections.property( "costMonkey") ,"costMonkey" );
			//ConsumRecord
			proList.add( Projections.property( "consumtMonkey"),"consumtMonkey" );
			//LevelAward
			 proList.add( Projections.property( "rewardAmount"),"rewardAmount" );
			//ShareBonusAward
			proList.add( Projections.property( "averageAmount"),"averageAmount" );
			//RewardRecord
			proList.add( Projections.property( "reMmemberReward"),"reMmemberReward" ); 
			criteria.setProjection(proList); 

 

2,hibernte标准创建连接

                           Criteria criteria =  session.createCriteria(User.class,"users");
			//LEFT JOIN
			criteria.createAlias("ConsumRecord", "consumRecord");
			 criteria.createAlias("LevelAward", "levelAward");
			criteria.createAlias("ShareBonusAward", "shareBonusAward");
			criteria.createAlias("RewardRecord", "rewardRecord"); 
			
			criteria.setFetchMode("ConsumRecord", FetchMode.JOIN);
			 criteria.setFetchMode("LevelAward", FetchMode.JOIN);
			criteria.setFetchMode("ShareBonusAward", FetchMode.JOIN);
			criteria.setFetchMode("RewardRecord", FetchMode.JOIN); 

 

3,hibernte标准添加哪里,groupby

            //WHERE
			criteria.add(Restrictions.eq("consumRecord.createTime", "CURDATE()"));
	        //ORDER BY
			criteria.addOrder(Order.desc("users.status"));

4,hibernte标准一个复杂的查询例子(包含常用的所有查询例法)

 参考网站:https://www.cnblogs.com/g-smile/p/9177841.html    ----------------------------感谢老师!

5,hibernte标准关联查询(内连接与左连接)

参考网站:https:      //blog.csdn.net/snowday88/article/details/6712169 ----------------------------感谢老师!

6,Hibernate标准Projections(投影,统计,不重复结果)

参考网站:https:    //blog.csdn.net/yalove/article/details/6644228 ----------------------------感谢老师!

                    https://blog.csdn.net/weixin_37524974/article/details/62044655     ----------------------------感谢老师!!!

7,hibernate - Criteria查询

参考网站:https:        //www.cnblogs.com/liuconglin/p/5721863.html ----------------------------感谢老师!

                     https://www.cnblogs.com/liuconglin/p/5721863.html     ----------------------------感谢老师!!!

                     https://www.cnblogs.com/liuconglin/p/5721863.html     ----------------------------感谢老师!!!

                      https://www.cnblogs.com/Sonet-life/p/4685479.html    ----------------------------感谢老师! !

1,标准查询,需要获取非关联字段信息时,必须创建关联表criteria.createAlias(“productType”,“ptype”);

2,Criteria查询,主表和关联表有相同数据库字段,且需要使用原始SQL语句时,必须指定别名,否则将无法找到指定列,而报“未明确指定列”的错误

8,数据库异常整理:org.hibernate.QueryException:无法解析属性:“xxx”   巨大一个坑!

参考网站:https:      //blog.csdn.net/chuck_kui/article/details/54783004 ----------------------------感谢老师!

9,Spring boot之使用JPA对数据进行复杂条件的查询  ---------------------------- 谓词

@Override
	public List finSalesInfoByConditions (String productName,String productStyle,String userName,String startTime, String endTime) {
		Specification spec = new Specification() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery query,
					CriteriaBuilder criteriaBuilder) {
				List predicates = new ArrayList();
				if ( !"".equals(productName) && productName != null && !productName.isEmpty() ) {
                    //添加 like 查询语句
					predicates.add(criteriaBuilder.like(root.get("productName"),"%" + productName + "%"));
				}
				if ( !"".equals(productStyle) && productStyle != null & !productStyle.isEmpty()) {
                    //添加 and 并且 查询语句
					predicates.add( 
criteriaBuilder.equal(root.get("productStyle").as(String.class), productStyle));
				}
				if ( !"".equals(userName) && userName != null & !userName.isEmpty() ) {
					predicates.add( criteriaBuilder.like(root.get("userName"), "%" + userName + "%"));
				}
                //根据时间大小 分别添加 查询条件
				if ( (startTime != null && !"".equals(startTime)) ) {
					 predicates.add( criteriaBuilder.greaterThan(root.get("storeStart").as(String.class), startTime) );
				}
				if ( (endTime != null && !"".equals(endTime))  ) {
					 predicates.add(  criteriaBuilder.lessThan(root.get("storeStart").as(String.class), endTime) );
				}
                //根据条件 添加 OR 查询语句
				if ( !"".equals(storeId) && storeId != null & !storeId.isEmpty()) {
					Predicate p1 = criteriaBuilder.and(criteriaBuilder.equal(root.get("storeId") , storeId));
					Predicate p2 = criteriaBuilder.and( criteriaBuilder.equal(root.get("superStoreId"), storeId));
					predicates.add(  criteriaBuilder.or(p1,p2)  );
				}
				return  criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
			}
		};
		List list = salesRepository.findAll(spec);
		return list;
	}

10,  Spring boot之使用JPA对数据进行时间筛选---------------------- 

predicates.add( criteriaBuilder.greaterThan(root.get("createTime").as(String.class), startTime));
predicates.add( criteriaBuilder.lessThan(root.get("createTime").as(String.class), endTime));

11,  Spring boot之使用JPA对数据进行像模糊查询筛选---------------------- 

if ( !"".equals(productName) && productName != null && !productName.isEmpty() ) {
	predicates.add(criteriaBuilder.like(root.get("productName"),"%" + productName + "%"));
}

来都来了,代码看都看了,那就留个言呗,可以互动下!


转载声明:本文为博主原创文章,未经博主允许不得转载。

你可能感兴趣的:(----------后台,#,SpringBoot,体系)