通过id查询用户,但是只返回指定的字段
来源:互联网 发布:中国移动宽带拨号软件 编辑:程序博客网 时间:2024/06/06 00:21
使用hibernate和spring MVC
通过id查询到一个用户,但是只返回指定的字段
方式一: 拼接hql
/*** * 通过数据库ID查询用户,但是只返回指定的字段 * @param id * @param propertyNames : 指定的多个成员变量 * @return */ public Object[] getPropertiesById(int id,String[] propertyNames){ if(ValueWidget.isNullOrEmpty(propertyNames)){ return null; } String hql="select "+propertyNames[0]; for (int i = 1; i < propertyNames.length; i++) { String string = propertyNames[i]; hql+=","+string; } String parameterId="id22"; hql+=" from "+clz.getCanonicalName()+" c where c.id=:"+parameterId; Query q= this.sessionFactory.getCurrentSession().createQuery(hql); Object result=q.setInteger(parameterId, id).uniqueResult(); Object[]objs=null; if(result instanceof Object[]){ objs=(Object[])result; }else{//当只返回一个成员变量的时候 objs=new Object[]{result}; } return objs; } /*** * 通过id,查询到一条记录,但是只返回指定的两个字段 * @param id * @param propertyName1 : 类的成员变量 * @param propertyName2 : 类的成员变量 * @return */ public Object[] getPropertiesById(int id,String propertyName1,String propertyName2){ String hql="select "+propertyName1; if(!ValueWidget.isNullOrEmpty(propertyName2)){ hql+=","+propertyName2; } String parameterId="id22"; hql+=" from "+clz.getCanonicalName()+" c where c.id=:"+parameterId; Query q= this.sessionFactory.getCurrentSession().createQuery(hql); Object result=q.setInteger(parameterId, id).uniqueResult(); Object[]objs=null; if(result instanceof Object[]){ objs=(Object[])result; }else{//当只返回一个成员变量的时候 objs=new Object[]{result}; } return objs; }
方式二:使用投影
/*** * 通过数据库ID查询用户,但是只返回指定的字段 * @param id * @param propertyNames : 指定的多个成员变量 * @return */ public Object[] getPropertiesById2(int id,String[] propertyNames){ Criteria c=this.sessionFactory.getCurrentSession().createCriteria(clz); ProjectionList projectionList=Projections.projectionList(); if(!ValueWidget.isNullOrEmpty(propertyNames)){ for (int i = 0; i < propertyNames.length; i++) { String string = propertyNames[i]; projectionList.add(Projections.property(string)); } } Object result=c.add(Restrictions.idEq(id)).setProjection(projectionList).uniqueResult(); Object[]objs=null; if(result instanceof Object[]){ objs=(Object[])result; }else{//当只返回一个成员变量的时候 objs=new Object[]{result}; } return objs; }/*** * 通过id,查询到一条记录,但是只返回指定的两个字段 * @param id * @param propertyName1 : 类的成员变量 * @param propertyName2 : 类的成员变量 * @return */ public Object[] getPropertiesById2(int id,String propertyName1,String propertyName2){ Criteria c=this.sessionFactory.getCurrentSession().createCriteria(clz); ProjectionList projectionList=Projections.projectionList() .add(Projections.property(propertyName1)); if(!ValueWidget.isNullOrEmpty(propertyName2)){ projectionList.add(Projections.property(propertyName2)); } Object result=c.add(Restrictions.idEq(id)).setProjection(projectionList).uniqueResult(); Object[]objs=null; if(result instanceof Object[]){ objs=(Object[])result; }else{//当只返回一个成员变量的时候 objs=new Object[]{result}; } return objs; }
使用场景:知道id,但是没必要获取整条记录,只需要获取其中的两三个字段而已.
0 0
- 通过id查询用户,但是只返回指定的字段
- mongoTemplate通过Query条件指定查询条件和返回字段
- 关于hql语句只返回指定字段的一种写法
- 连接查询(多个表),通过where指定连接字段,从而只查询连个表中都有的数据
- Hibernate查询指定字段并返回对象的结果集
- mongodb中查询返回指定字段
- 【MongoDB】MongoDB只返回指定的字段的值 和 设定超时项
- Hibernate高效查询,只查询部分/指定字段
- Hibernate高效查询,只查询部分/指定字段
- Hibernate高效查询,只查询部分/指定字段
- 数据库只查询user表的id和name字段,并对name进行去重
- 关于用Hibernate执行sql查询,字符字段类型只返回第一个字节的问题
- Oracle 通过语句返回指定的字段且是不同值的数目
- hibernate查询数据表char类型字段只返回一个字符
- hibernate查询数据表char类型字段只返回一个字符
- hibernate查询数据表char类型字段只返回一个字符
- Hibernate查询数据表char类型字段只返回一个字符
- hibernate查询数据表char类型字段只返回一个字符 .
- list容器合并链表
- 我的第一个博客
- Training:Hash及应用
- 反射的应用
- javascript基础学习之-事件冒泡
- 通过id查询用户,但是只返回指定的字段
- POJ 3469 Dual Core CPU 最小割
- PopoverController for iPhone
- 第十四周项目3-OOP版电子词典
- uva 264 Count on Cantor
- TCP/IP协议族-----6、IP分组的交付和转发
- 测试数据真假难辨-IT精英自我思考
- CentOS安装zip unzip命令
- Android EditText控件切换可编辑状态