/**
* <p>方法描述:sql语句查询 返回List<Class> </p>
* <p>方法备注: Class 只能是自定义类 </p>
* @param calzz
* @param sql
* @return
* <p>创建人:王川</p>
* <p>创建时间:Jul 9, 2015 4:44:16 PM</p>
*
*/
public List findSql(Class calzz ,String sql){
Query query = this.getSession().createSQLQuery(sql);
return query.setResultTransformer(new AliasToBeanResultTransformer_CBRC(calzz)).list();
}
/**
* <p>方法描述:sql语句查询 返回Object<Class> </p>
* <p>方法备注: Class 只能是自定义类 </p>
* @param calzz
* @param sql
* @return
* <p>创建人:王川</p>
* <p>创建时间:Jul 9, 2015 4:44:16 PM</p>
*
*/
public Object findSqlUnique(Class calzz ,String sql){
Query query = this.getSession().createSQLQuery(sql);
return query.setResultTransformer(new AliasToBeanResultTransformer_CBRC(calzz)).uniqueResult();
}
/**
* <p>方法描述:HQL分页查询 </p>
* <p>方法备注: </p>
* @param _page
* @param hql
* <p>创建人:王川</p>
* <p>创建时间:Jul 10, 2015 9:51:29 AM</p>
*
*/
/*-------------------------------------------------------------------*/
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<Object> _params, List<String> _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());
}
/*-------------------------------------------------------------------*/
/**
* <p>方法描述: HQL分页统计</p>
* <p>方法备注: </p>
* @param hql
* @return
* @throws BaseException
* <p>创建人:王川</p>
* <p>创建时间:Jul 10, 2015 11:15:27 AM</p>
*
*/
/*-------------------------------------------------------------------*/
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<Object> _params, List<String> _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;
}
/*-------------------------------------------------------------------*/
/**
* <p>方法描述:SQL分页查询 </p>
* <p>方法备注: </p>
* @param _page
* @param hql
* <p>创建人:王川</p>
* <p>创建时间:Jul 10, 2015 9:51:29 AM</p>
*
*/
/*-------------------------------------------------------------------*/
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<Object> _params, List<String> _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());
}
/*-------------------------------------------------------------------*/
/**
* <p>方法描述: SQL分页统计</p>
* <p>方法备注: </p>
* @param hql
* @return
* @throws BaseException
* <p>创建人:王川</p>
* <p>创建时间:Jul 10, 2015 11:15:27 AM</p>
*
*/
/*-------------------------------------------------------------------*/
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<Object> _params, List<String> _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_ ";
}
/**
* <p>方法描述:设置查询参数 </p>
* <p>方法备注: </p>
* @param query
* @param _params
* @param _paramsNM
* <p>创建人:王川</p>
* <p>创建时间:Jul 10, 2015 2:13:21 PM</p>
*
*/
protected void setQueryParams(Query query,List<Object> _params, List<String> _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));
}
}
}
/**
* <p>方法描述:获取总条数sql查询语句 </p>
* <p>方法备注: </p>
* @param sql
* @return
* @throws BaseException
* <p>创建人:王川</p>
* <p>创建时间:Jul 10, 2015 2:13:57 PM</p>
*
*/
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;
}
/**
* <p>方法描述:sql语句执行 update delete insert </p>
* <p>方法备注: </p>
* @param sql
* <p>创建人:王川</p>
* <p>创建时间:Jul 10, 2015 2:22:53 PM</p>
*
*/
/*-------------------------------------------------------------------*/
public void executeSql(String sql) {
executeSql(sql, null);
}
public void executeSql(String sql, List<Object> _params) {
executeSql(sql, _params, null);
}
public void executeSql(String sql, List<Object> _params, List<String> _paramsNM) {
SQLQuery query = getSession().createSQLQuery(sql);
setQueryParams(query, _params, _paramsNM);
query.executeUpdate();
}
/*-------------------------------------------------------------------*/