21st-day dao Model

Source: Internet
Author: User
Packagecom. jxnu. dao; importjava. io. serializable; publicinterfaceDaoT {voidadd (Tt); voidupdate (Tt); voiddelete (Serializableid); TfindOne (Serializableid);} The Serializableid parameter uses the polymorphism idea, because integer, String, and so on all implement Serializab

Package com. jxnu. dao; import java. io. serializable; public interface DaoT {void add (T t); void update (T t); void delete (Serializable id); T findOne (Serializable id );} the Serializable id parameter uses polymorphism because integer and String all implement Serializab.

package com.jxnu.dao;import java.io.Serializable;public interface Dao
 
   {void add(T t);void update(T t);void delete(Serializable id);T findOne(Serializable id);}
 

The Serializable id parameter is used.PolymorphismBecause integer, String, and so on all implement the Serializable interface, the id type stored in our database is not fixed.

package com.jxnu.dao;import java.util.List;import com.itheima.domain.User;public interface UserDao extends Dao
 
   {List
  
    findAll();}
  
 
package com.jxnu.dao;import java.util.List;import com.itheima.domain.Customer;public interface CustomerDao extends Dao
 
   {List
  
    findRecords(int startIndex,int pageSize);}
  
 

Using a jar package in hibernate, hibernate3.jar

package com.jxnu.dao.impl;import java.io.Serializable;import org.hibernate.Session;import com.itheima.dao.Dao;public class BaseDao
 
   implements Dao
  
    {private Session session = null;private Class clazz;public BaseDao(Class clazz){this.clazz=clazz;}public void add(T t) {// TODO Auto-generated method stubsession.save(t);}public void update(T t) {// TODO Auto-generated method stubsession.update(t);}public void delete(Serializable id) {// TODO Auto-generated method stub}public T findOne(Serializable id) {// TODO Auto-generated method stubreturn (T) session.get(clazz, id);}}
  
 
package com.jxnu.dao.impl;import java.util.List;import com.itheima.dao.UserDao;import com.itheima.domain.User;public class UserDaoImpl extends BaseDao
 
   implements UserDao {public UserDaoImpl(){super(User.class);}@Overridepublic List
  
    findAll() {// TODO Auto-generated method stubreturn null;}}
  
 
package com.jxnu.dao.impl;import java.util.List;import com.itheima.dao.CustomerDao;import com.itheima.domain.Customer;public class CustomerDaoImpl extends BaseDao
 
   implements CustomerDao {public CustomerDaoImpl(){super(Customer.class);}public List
  
    findRecords(int startIndex, int pageSize) {// TODO Auto-generated method stubreturn null;}}
  
 
The above is the dao template, which simplifies a lot of code and is very streamlined. It is better not to have more code, but to be clear.

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.