hibernate的查询方式

HQL 查询

如下需要创建对应的vo 对象对象名.hbm.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="cn.blue.entity.CCfgCatalog"
table="C_CFG_CATALOG">
<id name="id" type="java.lang.String">
<column name="id" length="20" />
<generator class="assigned" />
</id>
<property name="catalogid" type="java.lang.String">
<column name="catalogid" length="20" />
</property>
</class>
</hibernate-mapping>

方式一

1
2
3
4
5
6
7
String sql = "select TYPECODE, TYPE from c_cfg_type where id = '" + typeId + "'";
List<Object[]> list = cInsProjectDao.executeNativeSql(sql);
CCfgType cCfgType = new CCfgType();
if (!list.isEmpty()) {
cCfgType.setTypecode(list.get(0)[0].toString());
cCfgType.setType(list.get(0)[1].toString());
}

方式二

1
2
3
4
5
6
7
8
9
10
String hql = "from CCfgType where id = ?";
try {
List<CCfgType> cCfgTypeList = cInsProjectDao.find(hql, typeId);
if (!cCfgTypeList.isEmpty()) {
return cCfgTypeList.get(0);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;

示例

  1. 查询所有示例
1
2
3
4
5
6
7
8
// from 后跟的是要查询的对象,而不是表
String hql = "from User";
Query query = session.createQuery(hql);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
  1. 带 where 的查询示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
String hql = "from User where userName = 'James'";
Query query = session.createQuery(hql);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
/*
在HQL中where语句中使用的是持久化对象的属性名,如上面示例中的userName。当然在HQL中也可以使用别名
*/
String hql = "from User as u where u.userName = 'James'";
/*
过滤条件
在where语句中还可以使用各种过滤条件,如:=、<>、<、>、>=、<=、between、not between、
in、not in、is、like、and、or等
*/
  1. HQL 占位符
1
2
3
4
5
6
7
8
String hql = "from User where userName = ?";
Query query = session.createQuery(hql);
// 索引从0开始
query.setString(0, "James");
List<User> userList = query.list();
for(User user : userList){
System.out.println(user.getUserName());
}
  1. HQL 引用占位符
1
2
3
4
5
6
7
String hql = "from User where userName = :name";
Query query = session.createQuery(hql);
query.setParameter("name", "James");
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}

QBC(Query By Criteria)查询

  • Criteria 对象提供了一种面向对象的方式查询数据库。Criteria 对象需要使用 Session 对象来获得。
  • 一个 Criteria 对象表示对一个持久化类的查询。
  1. 查询所有
1
2
3
4
5
Criteria c = session.createCriteria(User.class);
List<User> userList = c.list();
for(User user:userList){
System.out.println(user.getUserName());
}
  1. 带条件查询
1
2
3
4
5
6
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.eq("userName", "James"));
List<User> userList = c.list();
for(User user : userList){
System.out.println(user.getUserName());
}

原生 SQL 查询

  1. 示例

    原生会返回对象的父类Object

    1
    2
    3
    4
    5
    6
    7
    String sql = "select id,username,userpwd from user";
    List list = session.createSQLQuery(sql).list();
    for(Object item : list){
    Object[] rows = (Object[]) item;
    System.out.println("id:" + rows[0] + "username:"
    + rows[1] + "userpwd:" + rows[2]);
    }
  2. addEntity()示例

    addEntity()可以告诉Hibernate你想要封装成对象的类型,然后自动为你封装

1
2
3
4
5
6
String sql = "select id,username,userpwd from t_user";
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
List<User> list = query.list();
for(User user : list){
System.out.println(user.getUserName());
}

3. uniqueResult示例

可以直接查询返回对象结果集

1
2
3
4
String sql = "select id,username,userpwd from t_user where id = 2";
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
User user = (User) query.uniqueResult();
System.out.println(user.getUserName());
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  1. © 2020 Liu Yang    湘ICP备20003709号

请我喝杯咖啡吧~

支付宝
微信