In the beginning with Hibernate4 wrote a Basedao implementation please advise

Source: Internet
Author: User
Import java.io.Serializable;
Import java.util.List;
Import Java.util.Map;
Import Javax.annotation.Resource;
Import org.hibernate.HibernateException;
Import Org.hibernate.Query;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import Org.hibernate.transform.Transformers;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.stereotype.Repository; /** * * @author Kingschan * date:2014-1-9 * @param <T> * * @Repository ("Basedao") public class Basedao {Publi
	c Logger log = Loggerfactory.getlogger (Basedao.class);
	
	Private Sessionfactory sessionfactory;
	Public Sessionfactory Getsessionfactory () {return sessionfactory;  } @Resource (name= "sessionfactory") public void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory
		= Sessionfactory;
	Log.info ("injected sessionfactory");
		} public Session getsession () {session session = NULL;
			try {session = Sessionfactory.getcurrentsession (); Log.info ("Only in the matterManagement, the session can be obtained through getcurrentsession (), and if this information is displayed, the transaction management work is in effect.
		");  	
			} catch (Hibernateexception ex) {session = Sessionfactory.opensession ();
			Log.info ("*************************************************************");
			Log.info ("* *"); Log.info ("* Obtained by opensession ().  Transaction management does not work and needs to be checked for configuration.
			*");
			Log.info ("* *");
		Log.info ("*************************************************************");
	} return session;
		}/** * Save an object * @param obj */public void Save (object obj) {Session s = getsession ();
	S.save (obj); }/** * Returns a List based on HQL if there are parameters just hql used to represent conditions * @param hql * @param args is assigned from 1 * @return */Public LIST&LT;?&G T
		queryForList (String hql,object...args) {list<?> lis =null;
		Session s = getsession ();
		Query q = s.createquery (HQL); if (null!=args&&args.length>0) {for (int i = 0; i < ARGS.LEngth;
			i++) {Q.setparameter (I, args[i]);		
		}} lis = Q.list ();
	return LIS; /** * Paged Query * @param hql * @param pageindex * @param limit * @param args * @return * * Public list<?& Gt
		Pagination (String hql,int pageindex,int limit,object...args) {list<?> lis =null;
		Session s = getsession ();
		Query q = s.createquery (HQL);
			if (null!=args&&args.length>0) {for (int i = 0; i < args.length; i++) {Q.setparameter (I, args[i]);
		}} q.setfirstresult ((pageindex-1) *limit);
		Q.setmaxresults ((pageindex-1) *limit+limit);
		
		lis = Q.list ();
	return LIS;
	}/** * Modifies an entity * @param obj */public void update (Object obj) {getsession (). Update (obj);
	}/** * Delete an entity * @param obj */public void Delete (Object obj) {getsession (). Delete (obj); 
		/** * Returns an entity based on the primary key * @param clazz * @param ID * @return */public Object get (class<?> clazz,object ID) {
Return GetSession (). Get (Clazz, (Serializable) ID);	}/** * Executes HQL * @param hql * @param obj */public int executehql (String hql,object...args) {Query q = getses
		Sion (). CreateQuery (HQL);
			if (null!=args&&args.length>0) {for (int i = 0; i < args.length; i++) {Q.setparameter (I, args[i]);
	}} return Q.executeupdate (); }/** * Executes SQL * @param hql * @param obj */public int executesql (String sql,object...args) {Query q = getsess
		Ion (). createsqlquery (SQL);
			if (null!=args&&args.length>0) {for (int i = 0; i < args.length; i++) {Q.setparameter (I, args[i]);
	}} return Q.executeupdate (); 
		}/** * HQL singleton value * @param hql * @param args * @return */public Object uniquequery (String Hql,object...args) {
		Query q = getsession (). CreateQuery (HQL);
			if (null!=args&&args.length>0) {for (int i = 0; i < args.length; i++) {Q.setparameter (I, args[i]);
	}} return Q.uniqueresult ();
}/*** * SQL Single value check * @param SQL * @param args	 * @return */public Object uniquequerybysql (String sql,object...args) {Query q = getsession (). createsqlquery (SQL); if (null!=args&&args.length>0) {for (int i = 0; i < args.length; i++) {Q.setparameter (i, args[i]
			);
	}} return Q.uniqueresult (); /** * Local named query returns list<map> * @return * @throws Exception */@SuppressWarnings ("unchecked") public list<
		?> getnamequery (String Key,object...args) throws exception{list<map<string, object>> lis = null;
		Session s = getsession (); 
		Query q = s.getnamedquery (key);
			if (null!=args&&args.length>0) {for (int i = 0; i < args.length; i++) {Q.setparameter (I, args[i]);
		}} q.setresulttransformer (Transformers.alias_to_entity_map);
		lis = Q.list ();
	return LIS; }/** * <font color= ' red ' > Execute SQL statement return list<map<string,object>></font> * @param SQL * @param ob jects * @return * * Public list<?> executesqlquery (String sQL, object...objects) {list<?> lis =null;
		Session s = getsession ();
		Query q = s.createsqlquery (SQL); if (null!=objects&&objects.length>0) {for (int i = 0; i < objects.length; i++) {Q.setparameter (i, O
			Bjects[i]);
		}} q.setresulttransformer (Transformers.alias_to_entity_map);
	return lis = Q.list ();
 }
}

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.