Java inheritance + Interface + generic unsolvable combination

Source: Internet
Author: User

Directly add the code without nonsense:

 

Engineering Structure:

 

Entity User:

package com.smonk.user.entity;public class User {//....}

Ibasedao:

Package COM. smonk. common. base; import Java. io. serializable; import Java. util. list; public interface ibasedao <t, Id extends serializable> {public abstract list <t> findall ();/*** find all, and pagination * @ Param page number of pages to be returned * @ Param pagesize no record count * @ return */public abstract list <t> findall (INT page, int pagesize ); public abstract void save (T entity); public abstract void Delete (T entity);/*** similar to findbyproperty, when properyname = value, delete the corresponding record */public abstract void deletebyproperty (string propertyname, object value); public abstract list <t> findbyexample (T example ); /***** search for * @ Param propertyname attribute name * @ Param Value Attribute Value * @ return */public abstract list <t> findbyproperty (string propertyname, object value ); /*** query multiple attributes * @ Param propertynames attribute name array * @ Param values attribute value array * @ return */public abstract list <t> findbypropertys (string [] propertynames, object [] values);/*** searches for multiple attributes and returns them by page, the attribute name array and the attribute value array must correspond to the ** @ Param propertynames attribute name array * @ Param values attribute value array * @ Param page number * @ Param pagesize number of entries per page * @ return */public list <t> findbypropertys (string [] propertynames, object [] values, int page, int pagesize);/*** query through attributes and pagination, the sequence of the attribute name array and attribute value array must correspond to * @ Param propertynames attribute name * @ Param values attribute value * @ Param page number * @ Param pagesize number of entries per page * @ return */Public list <t> findbyproperty (string propertyname, object value, int page, int pagesize);/*** count the total number of all records * @ return total number */Public int countall (); /*** count the total number of records in the database when propertyname = value * @ Param propertyname * @ Param value * @ return */Public int countbyproperty (string propertyname, object value ); /*** count the total number of records when multiple propertynames = value in the database * @ Param propertynames * @ Param values * @ return */Public int countbypropertys (string [] propertynames, object [] values); public abstract void saveorupdate (T entity); public abstract t findbyid (ID); public abstract void Update (T entity ); /*** obtain the persistence object type * @ return */public abstract class <t> getpersistentclass (); /*** query and sort by a certain attribute * @ Param property sort by Order * @ Param issequence whether to order in sequence */public list <t> findandorderbyproperty (INT firstresult, int fetchsize, string propertyname, Boolean issequence ); /*** query and sort by a certain attribute * @ Param property sort by Order * @ Param issequence whether to order in sequence */public list <t> findallandorderbyproperty (string propertyname, boolean issequence );}

 

Basehibernatedao:

Package COM. smonk. common. base; import Java. io. serializable; import Java. lang. reflect. parameterizedtype; import Java. util. list; import Org. hibernate. query; import Org. springframework. orm. hibernate3.support. hibernatedaosupport; public abstract class basehibernatedao <t, Id extends serializable> extendshibernatedaosupport implements ibasedao <t, Id> {private class <t> persistentclass; @ suppresswarnings ("uncheck Ed ") Public basehibernatedao () {// gets the persistence object type this. persistentclass = (class <t>) (parameterizedtype) getclass (). getgenericsuperclass ()). getactualtypearguments () [0];} public class <t> getpersistentclass () {return persistentclass ;} /*** search for * @ Param ID * @ return */@ suppresswarnings ("unchecked") Public t findbyid (ID) {return (t) This. gethibernatetemplate (). get (getpersistentclass (), ID);} public void Save (T entity) {This. gethibernatetemplate (). save (entity);}/*** Delete */Public void Delete (T entity) {This. gethibernatetemplate (). delete (entity);}/*** Delete through attribute */Public void deletebyproperty (string propertyname, object Value) {string querystring = "delete from" + getpersistentclass (). getname () + "as model where model. "+ propertyname +" =? "; Query = This. getsession (). createquery (querystring); query. setparameter (0, value1_query.exe cuteupdate ();}/*** saveorupdate */Public void saveorupdate (T entity) {This. gethibernatetemplate (). saveorupdate (entity);}/*** update */Public void Update (T entity) {This. gethibernatetemplate (). update (entity );} /*** query all records by page * @ Param page number of pages to be returned * @ Param pagesize no record count * @ return */public list <t> findall (in T page, int pagesize) {string querystring = "from" + getpersistentclass (). getname (); query = This. getsession (). createquery (querystring); int firstresult = (page-1) * pagesize; query. setfirstresult (firstresult); query. setmaxresults (pagesize); Return query. list ();}/*** count the total number of all records * @ return total number */Public int countall () {string querystring = "select count (*) from "+ getpersistentclass (). getname (); Qu Ery query = This. getsession (). createquery (querystring); List list = query. list (); long result = (long) list. get (0); return result. intvalue ();}/*** find by example * @ Param entity * @ return */@ suppresswarnings ("unchecked") public list <t> findbyexample (T entity) {return this. gethibernatetemplate (). findbyexample (entity);} @ suppresswarnings ("unchecked") public list <t> findall () {return this. gethibernatetem Plate (). find ("from" + getpersistentclass (). getname ();}/*** search for the * @ Param propertyname attribute name * @ Param Value Attribute Value * @ return */@ suppresswarnings ("unchecked") through Properties ") public list <t> findbyproperty (string propertyname, object Value) {string querystring = "from" + getpersistentclass (). getname () + "as model where model. "+ propertyname +" =? "; Return this. gethibernatetemplate (). find (querystring, value );} /*** query through multiple attribute combinations * @ Param propertynames attribute name array * @ Param values returns matching results for propertynames values */public list <t> findbypropertys (string [] propertynames, object [] values) {stringbuffer strbuffer = new stringbuffer (); strbuffer. append ("from" + getpersistentclass (). getname (); strbuffer. append ("as model where"); For (INT I = 0; I <Property Names. length; I ++) {if (I! = 0) strbuffer. append ("and"); strbuffer. append ("model. "); strbuffer. append (propertynames [I]); strbuffer. append ("="); strbuffer. append ("? ");} String querystring = strbuffer. tostring (); return this. gethibernatetemplate (). find (querystring, values );} /*** query and pagination through properties * @ Param propertyname attribute name * @ Param Value Attribute Value * @ Param page number * @ Param pagesize number of entries per page */public list <t> findbyproperty (string propertyname, object value, int page, int pagesize) {return this. findbypropertys (New String [] {propertyname}, new object [] {value}, page, pagesize) ;} /*** Query through multiple attribute combinations * @ Param propertynames attribute name array * @ Param values corresponds to the value of propertynames * @ Param page number * @ Param pagesize return return matched result */public list <t> findbypropertys (string [] propertynames, object [] values, int page, int pagesize) {stringbuffer strbuffer = new stringbuffer (); strbuffer. append ("from" + getpersistentclass (). getname (); strbuffer. append ("as model where"); For (INT I = 0; I <propertynames. length; I ++) {if (I! = 0) strbuffer. append ("and"); strbuffer. append ("model. "); strbuffer. append (propertynames [I]); strbuffer. append ("="); strbuffer. append ("? ");} String querystring = strbuffer. tostring (); int firstresult = (page-1) * pagesize; query = This. getsession (). createquery (querystring); query. setfirstresult (firstresult); query. setmaxresults (pagesize); For (INT I = 0; I <values. length; I ++) {query. setparameter (I, values [I]);} return query. list ();}/*** count by attribute * @ Param propertyname attribute name * @ Param Value Attribute Value */Public int countbyproperty (string Propertyname, object Value) {string [] propertynames = new string [] {propertyname}; object [] values = new object [] {value}; return this. countbypropertys (propertynames, values );} /*** count by multiple attributes * @ Param propertynames attribute name array * @ Param values returns */Public int countbypropertys (string [] propertynames, object [] values) {stringbuffer strbuffer = new stringbuffer (); strbuffer. append ("select Co Unt (*) from "+ getpersistentclass (). getname (); strbuffer. append ("as model where"); For (INT I = 0; I <propertynames. length; I ++) {if (I! = 0) strbuffer. append ("and"); strbuffer. append ("model. "); strbuffer. append (propertynames [I]); strbuffer. append ("="); strbuffer. append ("? ");} String querystring = strbuffer. tostring (); query = This. getsession (). createquery (querystring); For (INT I = 0; I <values. length; I ++) {query. setparameter (I, values [I]);} List list = query. list (); long result = (long) list. get (0); return result. intvalue ();}/*** search for T and sort it by a certain attribute * @ Param property sort order * @ Param issequence indicates whether the sequence is ordered, false is in reverse order */public list <t> findandorderbyproperty (INT firstresult, int fetchsize, string propertyname, Boolean issequence) {string querystring = "from" + getpersistentclass (). getname () + "as model order by model. "+ propertyname; If (issequence = false) {querystring = querystring +" DESC ";} query queryobject = getsession (). createquery (querystring); queryobject. setfirstresult (firstresult); queryobject. setmaxresults (fetchsize); Return queryobject. list ();} /*** search for all and sort by a certain attribute * @ Param propertyname sort by attribute name * @ Param issequence Order */public list <t> findallandorderbyproperty (string propertyname, boolean issequence) {string querystring = "from" + getpersistentclass (). getname () + "as model order by model. "+ propertyname; If (issequence = false) {querystring = querystring +" DESC ";} query queryobject = getsession (). createquery (querystring); Return queryobject. list ();}}

Iuserdao:

Package COM. smonk. user. dao; import COM. smonk. common. base. ibasedao; import COM. smonk. user. entity. user; public interface iuserdao extends ibasedao <user, integer> {/*** search for a user using the user name * @ Param username * @ return Tuser user object, if the user name does not exist, return NULL */public user findbyusername (string username );}

Userhibernatedao:

Package COM. smonk. user. dao; import Java. util. list; import COM. smonk. common. base. basehibernatedao; import COM. smonk. user. entity. user; public class userhibernatedao extends basehibernatedao <user, integer> implementsiuserdao {// property constantspublic static final string user_name = "username "; /*** search for the user by name * @ return user */public user findbyusername (string username) {list <user> userlist = super. findby Property (user_name, username); If (userlist. Size ()! = 0) {return userlist. get (0) ;}else {return NULL ;}} public static void main (string [] ARGs) {INTEGER id = 10; iuserdao Dao = new userhibernatedao (); Dao. findbyid (ID );}}

See how many functions userdao has with just a few sentences !!! :

 

Dear user, how much code does this architecture save? How many person-days are saved? How boring and annoying does it save ?...

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.