Hibernate使用Criteria查询部分字段


String alias = "user_"; //查詢時的table別名   
DetachedCriteria dc = DetachedCriteria.forClass(User.class,alias);  
ProjectionList pList = Projections.projectionList();  
pList.add(Projections.property(alias + "." + "id").as("id"));  
pList.add(Projections.property(alias + "." + "name").as("name"));  
pList.add(Projections.property(alias + "." + "age").as("age"));  
pList.add(Projections.property(alias + "." + "sex").as("sex"));  
dc.setProjection(pList);  
dc.setResultTransformer(Transformers.aliasToBean(User.class));  
resultList = memberService.findByDetached(dc).size();  
 
String alias = "user_"; //查詢時的table別名
DetachedCriteria dc = DetachedCriteria.forClass(User.class,alias);
ProjectionList pList = Projections.projectionList();
pList.add(Projections.property(alias + "." + "id").as("id"));
pList.add(Projections.property(alias + "." + "name").as("name"));
pList.add(Projections.property(alias + "." + "age").as("age"));
pList.add(Projections.property(alias + "." + "sex").as("sex"));
dc.setProjection(pList);
dc.setResultTransformer(Transformers.aliasToBean(User.class));

resultList = memberService.findByDetached(dc).size();

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页