Dao design model and dao Model
I. Directory Engineering Structure
Ii. Model Design
Iii. Implementation of core code
1. entity class
package com.lyh.domain;public class Book { private String id; private String name; private float price; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; }}package com.lyh.domain;public class Customer { private Integer id; private String name; 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; }}
2. Dao design
Top-level Dao. javapackage com. lyh. dao; import java. io. serializable; public interface Dao <T> {void add (T t); void update (T t); void delete (Serializable id); T findOne (Serializable id );} // BookDao. javapackage com. lyh. dao; import com. lyh. domain. book; public interface BookDao extends Dao <Book >{} // CustomerDao. java package com. lyh. dao; import java. util. list; import com. lyh. domain. customer; public interface CustomerDao extends Dao <Customer >{list <Customer> findCustomers (int startIndex, int offset );}
3. Specific Dao implementation class
Dao implementation class BaseDao. javapackage com. itheima. dao. impl; import java. io. serializable; import java. lang. reflect. parameterizedType; import java. lang. reflect. type; import org. hibernate. session; import org. hibernate. transaction; import com. itheima. dao. dao; import com. itheima. util. hibernateUtil; public abstract class BaseDao <T> implements Dao <T> {private Class clazz; public BaseDao () {// you can specify the type of javabean to which the subclass operates. Assign Type type = this To clazz. getClass (). getGenericSuperclass (); // new BookDaoImpl () Get BaseDao <Book> parameterized generic class ParameterizedType ptype = (ParameterizedType) type; // convert to BaseDao <Book> Type types [] = ptype. getActualTypeArguments (); clazz = (Class) types [0]; System. out. println (clazz) ;}@ Override public void add (T t) {Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); session. save (t); tx. commit (); session. close () ;}@ Override public void update (T t) {Session session = HibernateUtil. getSession (); Transaction tx = session. beginTransaction (); session. update (t); tx. commit (); session. close () ;}@ Override public void delete (Serializable id) {Session session = HibernateUtil. getSession (); T bean = findOne (id); Transaction tx = session. beginTransaction (); session. delete (bean); tx. commit (); session. close () ;}@ Override public T findOne (Serializable id) {Session session = HibernateUtil. getSession (); return (T) session. get (clazz, id) ;}// BookDaoImpl package com. lyh. dao. impl; import com. lyh. dao. bookDao; import com. lyh. domain. book; public class BookDaoImpl extends BaseDao <Book> implements BookDao {}// CustomerDaoImpl. javapackage com. lyh. dao. impl; import java. util. list; import com. lyh. dao. customerDao; import com. lyh. domain. customer; public class CustomerDaoImpl extends BaseDao <Customer> implements CustomerDao {// public CustomerDaoImpl () {// super (Customer. class); //} public List <Customer> findCustomers (int startIndex, int offset) {return null ;}}