Before the DAO design model, it was generally a process like this:
① Designing entity Objects First
Student objects:
Package Com.itheima.domain;import Java.io.serializable;import Java.util.date;public class Student implements Serializable {Private Integer id;//can be nullprivate String name;private Date birthday;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;} @Overridepublic String toString () {return "Student [id=" + ID + ", name=" + name + ", birthday=" + Birthday + "]";}}
Customer object:
Package Com.itheima.domain;import Java.io.serializable;public class Customer implements Serializable {private Integer Id;private string Name;private string gender;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getgender () {return gender;} public void Setgender (String gender) {This.gender = gender;} @Overridepublic String toString () {return "Customer [id=" + ID + ", name=" + name + ", gender=" + gender+ "]";}}
② Designing the DAO Layer
Student DAO
Customer DAO
③dao implementation
Student DAO Implementation
Customer DAO implementations similar to
Can be found, so that DAO more up, there are a lot of duplicate code, mainly focus on adding and deleting 4 methods above.
Improved
One. In the DAO package, add a DAO interface
Package Com.itheima.dao;import Java.io.serializable;public interface dao<t> {void Add (T t); void update (T t);/** * Root The primary key queries an object * @param ID * @return */t findOne (Serializable ID);/** * Deletes an object based on the primary key * @param ID */void del (Serializable ID);}
Two. Create a simple Basedao class (here is the focus) under the Daoimpl package to implement the DAO interface
Package Com.itheima.dao.impl;import Java.io.serializable;import Org.hibernate.session;import Org.hibernate.transaction;import Com.itheima.dao.dao;import Com.itheima.util.sessionfactoryutil;public Abstract Class Basedao<t> implements dao<t> {Private class clazz;//remember the entity type passed over outside public Basedao (class Clazz) { This.clazz = Clazz;} public void Add (T t) {Session session = Null;try{session = Sessionfactoryutil.getsession (); Transaction tx = Session.begintransaction (); Session.save (t); Tx.commit ();} catch (Exception e) {throw new RuntimeException (e);} Finally{session.close ();}} Public T FindOne (Serializable ID) {Session session = Null;try{session = Sessionfactoryutil.getsession (); return (T) Session.get (clazz, id);} catch (Exception e) {throw new RuntimeException (e);} Finally{session.close ();}} public void update (T t) {Session session = Null;try{session = Sessionfactoryutil.getsession (); Transaction tx = Session.begintransaction (); session.update (t); Tx.commit ();} catch (Exception e) {throw new Runtimeexception (e);} Finally{session.close ();}} public void del (Serializable ID) {Session session = Null;try{session = Sessionfactoryutil.getsession (); Transaction tx = Session.begintransaction (); t bean = (t) session.get (clazz, id); Session.delete (bean); Tx.commit ();} catch (Exception e) {throw new RuntimeException (e);} Finally{session.close ();}}}
Three. Modify the specific implementation class
Modify the customer DAO implementation class
Package Com.itheima.dao.impl;import Java.util.list;import Com.itheima.dao.customerdao;import Com.itheima.domain.customer;public class Customerdaoimpl extends basedao<customer> implements Customerdao {Publ IC Customerdaoimpl (Class clazz) {super (customer.class);} Public list<customer> findpagecustomer (int startIndex, int size) {return null;}}
Modify Student DAO Implementation class
Package Com.itheima.dao.impl;import Java.util.list;import Com.itheima.dao.studentdao;import Com.itheima.domain.student;public class Studentdaoimpl extends basedao<student> implements Studentdao {//Inherit base Dao, there is no construction method, need to construct method, tell the parent class, check is that class, above the customer like public Studentdaoimpl (class Clazz) {super (student.class);} Public list<student> FindAll () {return null;}}
Four. Service Test
Student.cfg.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
Cusstomer.cfg.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
Give the configuration file to hibernate management
Hibernate file hibernate.cfg.xml
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd ">
<--The main thing is this--><!--tell the map file--><mapping resource= "Com/itheima/domain/student.hbm.xml"/>< Mapping resource= "Com/itheima/domain/customer.hbm.xml"/></session-factory>DAO test
Package Com.itheima.test;import Java.util.date;import Com.itheima.dao.customerdao;import Com.itheima.dao.studentdao;import Com.itheima.dao.impl.customerdaoimpl;import Com.itheima.dao.impl.studentdaoimpl;import Com.itheima.domain.customer;import Com.itheima.domain.Student;public Class Daotest {public static void main (string[] args) {Customerdao Cdao = new Customerdaoimpl (customer.class); Studentdao SDao = new Studentdaoimpl (student.class); Customer C = new Customer (), C.setname ("Liu Yachong"), C.setgender ("Pending"); Cdao.add (c); Student s = new Student (); S.setname ("Lvyun Snow"); S.setbirthday (new Date ()); Sdao.add (s);}}
Results:
Customer table
Student table
This is the DAO design model
The dashed line is the implementation interface, the solid is the inheriting class
There is an abstract on the Basedao, note:
Why is there no abstract method in a class, but also to define the declaration as an abstract class?
The reason is simple: just do not let the direct use, why not let?
Basedao does not allow direct use, it is used for inheritance. In fact, HttpServlet is typical, HttpServlet is abstract class, there is a non-abstract method doxxx (Forget what name)
After the inheritance, satisfied with the method of direct use, not satisfied with the method of directly covering off the good.
DAO model design (based on DAO and hebernate framework)