Hibernate query SQL query, query results with new object to accept the way, HQL query

Source: Internet
Author: User



Package Com.ucap.netcheck.dao.impl;

Import java.util.ArrayList;
Import java.util.List;

Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Repository;
Import org.springframework.transaction.annotation.Transactional;

Import Com.ucap.netcheck.combination.beans.MainPageResultCombinationBean;
Import Com.ucap.netcheck.common.Page;
Import Com.ucap.netcheck.common.dao.BaseDAO;
Import Com.ucap.netcheck.dao.ISingleRejectDao;
Import Com.ucap.netcheck.entity.MainPageScanFail;
Import Com.ucap.netcheck.entity.MainPageScanResult;
Import com.ucap.netcheck.exception.DAOException;

/**
* @Title: Checkmainpageresultdaoimpl.java
* @Package Com.ucap.netcheck.dao.impl
* @Description:
* @author
* @date 2015-4-8 5:04:34
* @version V1.0
*/
@Repository
@Transactional
public class Singlerejectdaoimpl extends Basedao implements Isinglerejectdao {

@Autowired
Private Sessionfactory sessionfactory;

@SuppressWarnings ("Unchecked")
Public Mainpagescanresult querymainpagescanresultunique (Object ... params) {
try {
String sql = "from Mainpagescanresult m"
+ "WHERE m.errorpercent > 5.0" + "and M.inneruuid =?"
+ "and M.taskid =?";
Session session = Sessionfactory.getcurrentsession ();

list<mainpagescanresult> list = session.createquery (SQL)
. Setparameter (0, (String) params[0])
. Setparameter (1, (Integer) params[1])
. List ();

if (!list.isempty ()) {
Return List.get (0);
}
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}

/**
* Querymainpagescanfailbyid (This method to achieve the results of the search page scan failed)
*
* @Title: Querymainpagescanfailbyid
* @Description:
* @param @param PageNo
* @param @param Pagerow
* @param @param ID
* @param @return Settings file
* @return Page return type
* @throws
*/
Public Page Querymainpagescanfailbyid (int pageno, int pagerow,integer id) {
try {
list<object> params = new arraylist<object> ();

String hql = "from mainpagescanfail m WHERE m.mainpagescanid =?";

Params.add (ID);

Page page = This.querybypage (hql, PageNo, Pagerow, params);
return page;
} catch (Exception e) {
throw new Daoexception ("Paging Query all->error", e);
}
}

@SuppressWarnings ("Unchecked")
Public list<mainpagescanfail> Querymainpagescanfaillistbyid (Integer ID) {
try {
String hql = "from mainpagescanfail m WHERE m.mainpagescanid =:mainpagescanid";

list<mainpagescanfail> list = Sessionfactory.getcurrentsession ()
. CreateQuery (HQL)
. Setparameter ("Mainpagescanid", id)
. List ();

return list;
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}

 /**
  * Querymainpagescaneresultstatistics (this method allows you to count the information for this table)
  *
  * @Title: Querymainpagescaneresultstatistics
  * @Description: This method implements the statistics for this table
  * @param @param objs
  * @param @return settings file
  * @return page<mainpagescanresult> return type
  * @throws
  */
  @SuppressWarnings ("unchecked")
 public Mainpageresultcombinationbean Querymainpagescaneresultstatistics (
   object ... objs) {
  try {
    string hql = "Select New Com.ucap.netcheck.combination.beans.MainPageResultCombinationBean ("
      + "Min" (m.taskstarttime), Max (M.taskendtime), Count (M.inneruuid), Max (M.totalscannum), Max ( M.failnum)) "
     +" from "
     + MainPageScanResult.class.getSimpleName ()
     + "m WHERE m.inneruuid =?" + "and M.taskid = ? ";

list<mainpageresultcombinationbean> list = Sessionfactory
. Getcurrentsession (). CreateQuery (HQL)
. Setparameter (0, (String) objs[0]). Setparameter (1, objs[1])
. List ();
Return (!list.isempty ()? (Mainpageresultcombinationbean) List
. Get (0): null);
} catch (Exception e) {
throw new Daoexception ("Paging Query all->error", e);
}
}

@SuppressWarnings ({"Rawtypes", "Unchecked"})
@Override
Public Page querymainpageupdateresultbypage (int pageno, int pagerow,
Object ... objs) {
try {
StringBuilder sql = new StringBuilder (
"Select" +
"Cm.id as ID," +
"CM." task_id as TaskId, "+
"Cm.inneruuid as Inneruuid," +
"Cm.checksystaskid as Checksystaskid," +
"CM." URL as URL, "+
"Cm.first_channel as Firstchannel," +
"Cm.second_channel as Secondchannel," +
"Cm.chanel_name as Chanelname," +
"CM." Title as title, "+
"CM." Img_url as Imgurl, "+
"Cm.lastmodifytime as LastModifyTime," +
"CM." Show_time as ShowTime, "+
"Cm.scantime as Scantime," +
"Cm.isreject as Isreject" +
"From Cpu_mp as cm" +
"WHERE Cm.inneruuid =?" +
"and cm." task_id =? " +
"And Cm.isreject = 1"
);

Query query = sessionfactory
. Getcurrentsession ()
. Createsqlquery (Sql.tostring ())
. Setparameter (0, (String) objs[0])
. Setparameter (1, (Integer) objs[1]);

Page page = new page ();
Page.setpagenum (PageNo);
Page.setnumperpage (Pagerow);
Page.setallrows (Query.list (). Size ());

int allpages = Page.getallrows ()/page.getnumperpage ();
if (page.getallrows ()% page.getnumperpage () = = 0) {
Page.setallpages (Allpages); Total pages
} else {
Page.setallpages (allpages + 1); Total pages
}

Query.setfirstresult ((pageNo-1) * pagerow);
Query.setmaxresults (Pagerow);
Collection
list<object[]> list = Query.list ();
Page.setrecordlist (list);

return page;
} catch (Exception e) {
throw new Daoexception ("Paging Query all->error", e);
}
}

@SuppressWarnings ({"Unchecked"})
@Override
Public list<object[]> querymainpageupdateresultbycondition (Object ... objs) {
try {
StringBuilder sql = new StringBuilder (
"Select" +
"Cm.id as ID," +
"CM." task_id as TaskId, "+
"Cm.inneruuid as Inneruuid," +
"Cm.checksystaskid as Checksystaskid," +
"CM." URL as URL, "+
"Cm.first_channel as Firstchannel," +
"Cm.second_channel as Secondchannel," +
"Cm.chanel_name as Chanelname," +
"CM." Title as title, "+
"CM." Img_url as Imgurl, "+
"Cm.lastmodifytime as LastModifyTime," +
"CM." Show_time as ShowTime, "+
"Cm.scantime as Scantime," +
"Cm.isreject as Isreject" +
"From Cpu_mp as cm" +
"WHERE Cm.inneruuid =?" +
"and cm." task_id =? " +
"And Cm.isreject = 1"
);

Query query = sessionfactory
. Getcurrentsession ()
. Createsqlquery (Sql.tostring ())
. Setparameter (0, (String) objs[0])
. Setparameter (1, (Integer) objs[1]);

Collection
list<object[]> list = Query.list ();
return list;
} catch (Exception e) {
E.printstacktrace ();
}
return null;
}

}

Hibernate query SQL query, query results with new object to accept the way, HQL query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.