hql语句进行单多表查询和模糊查询

来源:互联网 发布:工厂模式 php 编辑:程序博客网 时间:2024/06/10 20:29

     1.查询一个表的字段集合,返回值类型List<Enity> 或者set<Entiy>等   查出所有的列的值

       from Enity  where xxx=?       where 以后的可不加


  2.查询指定列对象的集合 返回值类型List<Enity> 或者set<Entiy>等    只有指定列有值  如User表中username 和 address 字段

       select new User(username,address) from User where xxx=?   where 以后的可不加   

注意:使用newUser(username,address)  必须在User实体中加入对应两个属性的构造方法,否则会出现异常。Excetion:Unable to locate appropriate constructor on class

   

  3.多表and查询

          select  new User(u.username,u.address) from Customer c,User u where c.xxx=? and u.xxx=?;个别属性

          selrct new User from Customer c,User u where c.xxx=? and u.xxx=?;  全部属性


  4.多表and和or结合查询

select new User from Customer c ,User u where (c.xxx=? and u.xxx=?) or (c.xxx=? and c.xxx=? and c.xxx=u.xxx) 多混合使用

    select new User(u.user,u.address) from User u ,Customer c where (c.xxx=? and u.xxx=?) or c.xxx=?;等 

  5.like

  String hql = "select new Customer(customerName) from Customer c where customerName like'%"+customerName+"%'";

  like 一般用于模糊查询代表像什么,后面的%代表任意字符,如果只需要匹配后面的只需在后面加上%就可以了。注意加上单引号。

  

0 0
原创粉丝点击