hibernate dao层应用之HibernateDaoSupport二次封装

阅读更多
/**
*

方法描述:sql语句查询 返回List


*

方法备注: Class 只能是自定义类


* @param calzz
* @param sql
* @return
*

创建人:王川


*

创建时间:Jul 9, 2015 4:44:16 PM


*
*/
public List findSql(Class calzz ,String sql){
Query query = this.getSession().createSQLQuery(sql);
return query.setResultTransformer(new AliasToBeanResultTransformer_CBRC(calzz)).list();
}

/**
*

方法描述:sql语句查询 返回Object


*

方法备注: Class 只能是自定义类


* @param calzz
* @param sql
* @return
*

创建人:王川


*

创建时间:Jul 9, 2015 4:44:16 PM


*
*/
public Object findSqlUnique(Class calzz ,String sql){
Query query = this.getSession().createSQLQuery(sql);
return query.setResultTransformer(new AliasToBeanResultTransformer_CBRC(calzz)).uniqueResult();
}


/**
*

方法描述:HQL分页查询


*

方法备注:


* @param _page
* @param hql
*

创建人:王川


*

创建时间:Jul 10, 2015 9:51:29 AM


*
*/
/*-------------------------------------------------------------------*/
public void findPage(Page _page,String hql) throws BaseException{
findPage(_page, hql, null);
}

public void findPage(Page _page, String hql, List _params) throws BaseException{
findPage(_page, hql, _params, null);
}

public void findPage(Page _page, String hql, List _params, List _paramsNM) throws BaseException{
int totalRecord = findPageTotal(hql, _params, _paramsNM);
_page.setTotalRecord(totalRecord);
Query query = getSession().createQuery(hql);
setQueryParams(query, _params, _paramsNM);
query.setFirstResult(_page.getStartIndex());
query.setMaxResults(_page.getPageSize());
_page.setDataList(query.list());
}
/*-------------------------------------------------------------------*/

/**
*

方法描述: HQL分页统计


*

方法备注:


* @param hql
* @return
* @throws BaseException
*

创建人:王川


*

创建时间:Jul 10, 2015 11:15:27 AM


*
*/
/*-------------------------------------------------------------------*/
public int findPageTotal(String hql) throws BaseException{
return findPageTotal(hql,null);
}

public int findPageTotal(String hql, List _params) throws BaseException {
return findPageTotal(hql, _params, null);
}

public int findPageTotal(String hql, List _params, List _paramsNM) throws BaseException{
Query query = getSession().createQuery(getPageTotalStr(hql));
setQueryParams(query, _params, _paramsNM);
List result = query.list();
if ((result != null) && (!(result.isEmpty())))
return Integer.parseInt(result.get(0).toString());
return 0;
}
/*-------------------------------------------------------------------*/

/**
*

方法描述:SQL分页查询


*

方法备注:


* @param _page
* @param hql
*

创建人:王川


*

创建时间:Jul 10, 2015 9:51:29 AM


*
*/
/*-------------------------------------------------------------------*/
public void findPageSQL(Class calzz, Page _page,String sql) throws BaseException{
findPageSQL(calzz, _page, sql, null);
}

public void findPageSQL(Class calzz, Page _page, String sql, List _params) throws BaseException{
findPageSQL(calzz, _page, sql, _params, null);
}

public void findPageSQL(Class calzz, Page _page, String sql, List _params, List _paramsNM) throws BaseException{
sql = makeTempSql(sql);
int totalRecord = findPageTotalSQL(sql, _params, _paramsNM);
_page.setTotalRecord(totalRecord);
SQLQuery query = getSession().createSQLQuery(sql);
setQueryParams(query, _params, _paramsNM);
query.setFirstResult(_page.getStartIndex());
query.setMaxResults(_page.getPageSize());
query.setResultTransformer(new AliasToBeanResultTransformer_CBRC(calzz));
_page.setDataList(query.list());
}
/*-------------------------------------------------------------------*/

/**
*

方法描述: SQL分页统计


*

方法备注:


* @param hql
* @return
* @throws BaseException
*

创建人:王川


*

创建时间:Jul 10, 2015 11:15:27 AM


*
*/
/*-------------------------------------------------------------------*/
public int findPageTotalSQL(String sql) throws BaseException{
return findPageTotalSQL(sql,null);
}

public int findPageTotalSQL(String sql, List _params) throws BaseException {
return findPageTotalSQL(sql, _params, null);
}

public int findPageTotalSQL(String sql, List _params, List _paramsNM) throws BaseException{
SQLQuery query = getSession().createSQLQuery(getPageTotalStr(sql));
setQueryParams(query, _params, _paramsNM);
List result = query.list();
if ((result != null) && (!(result.isEmpty())))
return Integer.parseInt(result.get(0).toString());
return 0;
}
/*-------------------------------------------------------------------*/

protected String makeTempSql(String sql){
return "select t_.* from ("+sql+") t_ ";
}

/**
*

方法描述:设置查询参数


*

方法备注:


* @param query
* @param _params
* @param _paramsNM
*

创建人:王川


*

创建时间:Jul 10, 2015 2:13:21 PM


*
*/
protected void setQueryParams(Query query,List _params, List _paramsNM){
if(null == _params){
}
else if(null == _paramsNM){
for (int i = 0; i < _params.size(); ++i) {
query.setParameter(i, _params.get(i));
}
}
else{
for (int i = 0; i < _params.size(); ++i) {
query.setParameter(_paramsNM.get(i), _params.get(i));
}
}
}

/**
*

方法描述:获取总条数sql查询语句


*

方法备注:


* @param sql
* @return
* @throws BaseException
*

创建人:王川


*

创建时间:Jul 10, 2015 2:13:57 PM


*
*/
protected String getPageTotalStr(String sql) throws BaseException{
int start = sql.toUpperCase().indexOf("FROM");
    if (start == -1)
    throw new BaseException("sql sentence is Error,have no FROM key!");
    String from = sql.substring(start, sql.length());
    return "select count(*) " + from;
}

/**
*

方法描述:sql语句执行 update delete insert


*

方法备注:


* @param sql
*

创建人:王川


*

创建时间:Jul 10, 2015 2:22:53 PM


*
*/
/*-------------------------------------------------------------------*/
public void executeSql(String sql) {
executeSql(sql, null);
}

public void executeSql(String sql, List _params) {
executeSql(sql, _params, null);
}

public void executeSql(String sql, List _params, List _paramsNM) {
SQLQuery query = getSession().createSQLQuery(sql);
setQueryParams(query, _params, _paramsNM);
query.executeUpdate();
}
/*-------------------------------------------------------------------*/

你可能感兴趣的:(hibernate,dao)