项目中使用的是Hibernate框架,对于表查询一直只针对一张表,原码:
// 搜索员工
public List searchUseraccount(String username, String deptid, String cid,
String groupid) throws Exception {
List accountList = new ArrayList();
try {
session = HibernateSessionFactory.getSession();
t = session.beginTransaction();
StringBuffer hql = new StringBuffer();
hql.append("from Account where cid ='" + cid
+ "' and username like '%" + username + "%'");
if (null!=deptid&&!"0".equals(deptid)) {
hql.append(" and deptid='" + deptid + "'");
}
if (null!=groupid&&!"0".equals(groupid)) {
hql.append(" and gid='" + groupid + "'");
}
Query query = session.createQuery(hql.toString());
accountList = query.list();
t.commit();
} catch (Exception e) {
System.out.println("searchUseraccount fail!");
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return accountList;
}
但需求变化--->表结构变化,查询的结果必须通过两张表,通过搜索(http://zjsoft.iteye.com/blog/152328)得到了解决办法,写法如下:
// 搜索员工
public List searchUseraccount(String username, String deptid, String cid,
String groupid) throws Exception {
List accountList = new ArrayList();
try {
session = HibernateSessionFactory.getSession();
t = session.beginTransaction();
StringBuffer hql = new StringBuffer();
hql.append("select * from account where cid ='" + cid
+ "' and username like '%" + username + "%'");
if (null != deptid && !"0".equals(deptid) && !"".equals(deptid)) {
hql.append(" and deptid='" + deptid + "'");
}
if (null != groupid && !"0".equals(groupid) && !"".equals(groupid)) {
hql.append(" and (aid in (select aid from Agmapping where "
+" gid = '" + groupid + "')))");
}
Query query = session.createSQLQuery(hql.toString())
.addEntity(Account.class);
accountList = query.list();
t.commit();
} catch (Exception e) {
System.out.println("searchUseraccount fail!");
e.printStackTrace();
} finally {
HibernateSessionFactory.closeSession();
}
return accountList;
}
不同于我以前的写法在于红色区域,createSQLQuery方法的参数是纯sql语句,关于createSQLQuery的用法参见:http://jihongbin12329.iteye.com/blog/88678
分享到:
相关推荐
hibernate 多表查询 hibernate 多表不同于一般的sql嵌套查询,嵌套查询是不支持的,所以必须要配置好才能实现多表!只要配置好了,还是特别好用的
hibernate实现动态表查询的多种解决方案.自行整理的动太实现方案,总有一个适合你.
使用Struts2+Hibernate实现对部门表、员工表两张表的增删改查。
hibernate实现动态SQL查询,通过XML配置SQL,FREEMARKER 解析,生成要执行的SQL
利用MYSQL数据库提供的样例数据库world建立的查询小例子。已经建立好了数据库对应的...本项目主要是为了练习使用Hibernate查询数据库。如果有什么问题可以发站内信或者邮件asddf14@hotmail.com。我会尽量解答一下~~~
实际开发中碰到一个使用hibernate递归查询的问题。我解决了发出来给大家一起分享
Hibernate 实现原理 模拟,通过模拟hibernate的实现原理来加深对hibernate实现原理的理解和使用。
ExtJs4.2+Mysql+Struts2+Hibernate3实现分页查询 1.libs目录缺少hibernate核心jar包 2.libs目录缺少struts jar 3.WebRoot目录缺少ExtJs4.2核心类库 以上信息我都在项目里面注明了,因为这些内容的文件太大了,CSDN不...
hibernate 分页查询的实现 hibernate 内置的有分页功能 有三个参数 thisnumber一个是当前页数 sumcount是一页显示多少条数据 sql是用来查询的sql语句 public List getPageList(int thisNumber, int sumCount, ...
User实体(id, name, age, sex, ctime),使用hibernate实现User的增删改查。要求: 1、包的层次: com.xxx.entity, com.xxx.dao, com.xxx.dao.impl.hql, com.xxx.dao.impl.qbc, com.xxx.dao.impl.sql, ...
亲手编写的BBS小程序,管理员操作还没实现,但是用户的一般的发表文章,发表评论,登陆什么的都已实现,数据库就工程包里,运行前请把数据库导入SQL server中使用,如果有什么问题欢迎大家和我讨论,也欢迎大家来我...
改AHibernate 实现数据库 自动新增表参数,在要新增的参数上加上version数据库版本号
专用于HIBERNATE中两个对象间的外键关联查询
使用hibernate实现的分页效果 orm 映射 session 等技能点 带有注释和案例演示 表生成语句
后台的查询功能单独用类StudentDao封装(与Action类脱离),StudentDao使用Spring创建,StudentDao中支持参数flag,0时直接使用静态数据查询,1时使用hibernate查询数据库,flag值采用Spring方式赋值(在xml中配置)
7:Hibernate查询缓存Query Cache及实现
STRUTS2+HIBERNATE分页 实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码
分页查询就是把数据库中某张表的记录数进行分页查询,在做分页查询时会有一个Page类,下面是一个Page类,我对其做了详细的注解:。。。。。。。。。。。。
hibernate struts2 MyEclipse一个实现查询的小例子,很好用,值得初学者学习一下
六种方式实现hibernate查询,六种方式实现hibernate查询,六种方式实现hibernate查询