HIBERNATE项目运用

=====================项目运用======================
多条件分页查询的dao写法:条件是不确定的,所以hql要拼出来。

public List<User> getUserByPage(User user, PageModel pageModel){
StringBuilder hql = new StringBuilder();
hql.append("select u from User as u where 1=1");
List<Object> params = new ArrayList<>();

if(user != null){
    if(StringUtils.hasText(user.getName())){
        hql.append(" and name like ? ");
        params.add("%"+user.getName()+"%");
    }
    if(StringUtils.hasText(user.getPhone())){
        hql.append(" and phone like ?");
        params.add("%"+user.getPhone()+"%");
    }
    if(user.getJob()!=null && StringUtils.hasText(user.getJob().getCode())){
        hql.append(" and job.code = ?");
        params.add(user.getJob().getCode());
    }
}
hql.append(" order by createDate asc");
return this.findByPage(hql.toString(), pageModel, params);

}

使用第三种方式要在 userDao实现类里加deleteUser方法:
public void deleteUser(String[] userIds){
StringBuilder hql = new StringBuilder();
hql.append("delete from User where userId in(");
for(int i = 0;i<userIds.length;i++){
hql.append( i == 0 ? "?" : ",?");
}
hql.append(")");
this.bulkUpdate(hql.toString(), userIds);
}

--dao实现类增加方法
public void checkUser(String[] userIds,Short status){
StringBuilder hql = new StringBuilder();
hql.append("update User set status = ?,checker = ?,checkDate = ? where userId in(");
List<Object> params = new ArrayList<>();
params.add(status);
params.add(WebConstant.getSessionUser());
params.add(new Date());
for(int i = 0;i<userIds.length;i++){
hql.append( i == 0 ? "?" : ",?");
params.add(userIds[i]);
}
hql.append(")");
this.bulkUpdate(hql.toString(), params.toArray());
}
//批量删除角色
public void deleteRole(String[] ids){
StringBuilder hql = new StringBuilder();
hql.append("delete from Role where id in(");
Long[] params = new Long[ids.length];//id是long类型要注意
for(int i=0;i<ids.length;i++){
hql.append(i==0? "?":",?");
params[i] = Long.valueOf(ids[i]);
}
hql.append(")");
this.bulkUpdate(hql.toString(),params);
}

//异步加载树的hqlpublic List<Object[]> getModuleByCodeAndName() {String hql = "select code,name from Module order by code asc";return find(hql);}

相关文章
相关标签/搜索