The design of Basedao and its implementation class based on Hibernate

Source: Internet
Author: User

I used to design the DAO interface and its implementation, this way you do not have to write so many duplicate code. But because there is no understanding of reflection, it cannot be written unless it relies on other components of hibernate. However, with reflection, we can implement the functionality we want to do with generics.

The first is the interface:


Package Com.sms.dao.base;import Java.util.list;public interface basedao<t> {public void Add (T entity) throws exception;public void Delete (T entity) throws exception;public void Update (T entity) throws Exception;public T FindByID (in Teger ID) throws exception;/* * Get a list of pagesize from startIndex start size */public list<t> getpagelist (int startIndex, int page Size) throws exception;/* * Get total */public long Getamount ();}



Then the implementation class:

Package Com.sms.dao.base.impl;import Java.lang.reflect.parameterizedtype;import Java.util.list;import Javax.annotation.resource;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Com.sms.dao.base.basedao;public class Basedaoimpl<t> implements basedao<t> {private class<t> Entityclass;private String hql; @Resourceprivate sessionfactory sessionfactory;public Session getsession () {return Sessionfactory.getcurrentsession ();} @SuppressWarnings ("unchecked") public Basedaoimpl () {//Gets the class object of the generic passed class by Reflection This.entityclass = (class<t>) (( Parameterizedtype) This.getclass (). Getgenericsuperclass ()). Getactualtypearguments () [0];THIS.HQL = "from" + This.entityClass.getName ();} @Overridepublic void Add (Object entity) {this.getsession (). Save (entity); @Overridepublic void Delete (Object entity) {this.getsession (). Delete (entity);} @Overridepublic void Update (Object entity) {this.getsession (). Update (entity);} @Overridepublic T FindByID (Integer id) {@SuppressWarnings ("uncheckEd ") T result = (T) this.getsession (). get (Entityclass,id); return result;} @Overridepublic list<t> getpagelist (int startIndex, int pageSize) {//TODO auto-generated method Stub@suppresswarnings ("unchecked") list<t> List = This.getsession (). CreateQuery (HQL). Setfirstresult ( StartIndex). Setmaxresults (pageSize). List (); System.out.println (HQL); return list;} @Overridepublic long Getamount () {String sql = "SELECT COUNT (*) from" + this.entityClass.getName (); Long Count = (long) th Is.getsession (). CreateQuery (SQL). Uniqueresult (); return count;}}


Universal interface is completed, we use, as long as the inheritance of Basedaoimp can achieve the most basic additions and deletions to check.


For example, the grade in the Student management system:

The interfaces are:

Package Com.sms.dao;import Com.sms.dao.base.basedao;import Com.sms.entity.gradeentity;public interface GradeDao Extends basedao<gradeentity>{}



Implementation class:

Package Com.sms.dao.impl;import Org.springframework.stereotype.component;import Com.sms.dao.gradedao;import Com.sms.dao.base.impl.basedaoimpl;import com.sms.entity.GradeEntity; @Componentpublic class Gradedaoimpl extends Basedaoimpl<gradeentity> implements gradedao{}




This way, Gradedaoimpl can achieve the most basic function of adding and removing and changing the search.








The design of Basedao and its implementation class based on Hibernate

Related Article

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.