select 语句
1.以Object[]形式返回选择的属性
select子句中未指定返回数据类型,默认为Object[]
HQL的select查询语句中,如果指定了多个查询字段,则返回的是一个Object[]数组;
如果只指定了一个查询字段,则返回的是一个Object对象。
public void testSelect(){
String hql="select s.name,s.tel,s.address from Seller s";
Query query=session.createQuery(hql);
List<Object[]> lists=query.list();
for (Object[] list : lists) {
System.out.println("name:"+list[0]);
System.out.println("tel:"+list[1]);
System.out.println("address"+list[2]);
}
}
2.以list方式返回查询结果
String hql="select new list(s.name,s.tel,s.address) from Seller s";
Query query=session.createQuery(hql);
List<List> lists=query.list();
for (List list : lists) {
System.out.println("-------------------------");
System.out.println("name:"+list.get(0));
System.out.println("tel:"+list.get(1));
System.out.println("address:"+list.get(2));
}
3.通过map返回查询结果
new Map<Key,Value> key为索引值(是字符串类型)
String hql = "select new map(s.name,s.tel,s.address)from Seller s";
Query query = session.createQuery(hql);
List<Map> maps = query.list();
for(Map map :maps){
System.out.println("name"+map.get("0"));
System.out.println("name"+map.get("1"));
System.out.println("name"+map.get("2"));
}
或以别名的方式
String hql = "select new map(s.name as name ,s.tel as tel,s.address as addr)from Seller s";
Query query = session.createQuery(hql);
List<Map> maps = query.list();
for(Map map :maps){
System.out.println("name"+map.get("name"));
System.out.println("name"+map.get("tel"));
System.out.println("name"+map.get("addr"));
}
5.通过自定义类型返回查询结果
//1.先在类中定义构造方法
public Sellers(String name,String address){
this.name=name; this.address=address;
}
//2.通过自定义类型返回查询结果
String hql="select new Seller(s.name,s.tel,s.address)from Seller s";
Query query=session.createQuery(hql);
List<Seller> lists=query.list();
for (Seller seller : lists) {
System.out.println("-----------------------------");
System.out.println("name"+seller.getName());
System.out.println("tel"+seller.getTel());
System.out.println("address"+seller.getAddress());
}
6.distinct关键字
distinct关键字 消除查询过程中重复的元素
String hql="select distinct c.sex from Customer c";
where语句
1.比较运算
public void testWhere1(){
String hql="from Commodity where price>400";
Query query = session.createQuery(hql);
List<Commodity> commodities=query.list();
for (Commodity commodity : commodities) {
System.out.println("name:"+commodity.getName());
System.out.println("price:"+commodity.getPrice());
}
}
2.null值判断运算
is null 和 = null在HQL中是相同的
is not null 和 <>null 也是相同语义
【但在SQL中不能使用=null,以及<>null】
3.范围运算
//寻找价格(不)是4000或5000的商品
String hql="from Commodity c where c.price (not) in (5000,4000)";
//寻找价格(不在)在200到4000的商品
String hql1="from Commodity c where c.price (not) between 200 and 4000";
4.字符串模式匹配
1.like 关键字
2.通配符:% 匹配任意个字符,_ 匹配一个字符
String hql = "from Customer c where c.name like '张_'";//姓张的两个字的人
String hql = "from Customer c where c.address like '%北京%'";//地址里有北京
5.逻辑运算
String hql="from Commodity c where c.price between 100 and 4000 and c.category like '%电脑%'";
String hql1="from Commodity c where c.price between 100 and 4000 or c.category like '%电脑%'";