常用的HQL写法大全

来源:互联网 发布:mac优酷怎么开弹幕 编辑:程序博客网 时间:2024/05/16 13:03

1.一些常用的HQL写法

2.常用几种数据库,取前10条记录的sql语句写法

 access:select top (10) * from table1 where 1=1

  db2:select column from table where 1=1 fetch first 10 rows only
  mysql:select * from table1 where 1=1 limit 10
  sql server:
   读取前10条:select top (10) * from table1 where 1=1
   读取后10条:select top (10) * from table1 order by id desc
  oracle:select * from table1 where rownum<=10  

 

3.日期排序并取前五条的HQL语句写法

MySQL写法:里敲:select * from blog order by blogid desc limit 10     //查出最后10条数据(如果是HQL语句则会查出所有行数据)

HQL语句的完美写法
    /**
     * 通过用户id和分类id(可选)分页查询收藏(APP使用)
     * @param fromId    用户id
     * @param typeId    类型id
     * @param pageNow    当前页码
     * @param size        每页大小
     */
    @SuppressWarnings("unchecked")
    public List<HYCollect> listByFromId(int fromId, int typeId, int pageNow, int size){
        String hql = "select u from HYCollect u where u.fromId = "+fromId;
        if(typeId!=0){ hql += " and u.typeId = "+typeId;}
        hql += " order by u.id desc";
        Query query = this.hibernateTemplate.getSessionFactory().openSession().createQuery(hql);
        query.setFirstResult((pageNow-1)*size);
        query.setMaxResults(size);
        return query.list();
    }
4.HQL如何写查询语句,查询符合某条件的从第10条开始到第20条的结果:
    select top 10 * from userImage where id not in(select top 10 id from userImage)

 

0 0