Dao design model and dao Model

Source: Internet
Author: User
Tags findone

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 ;}}

 

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.