Home > Database > Mysql Tutorial > 第21天之dao模型

第21天之dao模型

WBOY
Release: 2016-06-07 15:56:18
Original
978 people have browsed it

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);} Serializable id参数使用了 多态思想 ,因为integer、String等等都实现了Serializab

package com.jxnu.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);

}
Copy after login

Serializable id参数使用了多态思想,因为integer、String等等都实现了Serializable接口,我们数据库存储的id类型是不定的。

package com.jxnu.dao;

import java.util.List;

import com.itheima.domain.User;

public interface UserDao extends Dao<User> {

	List<User> findAll();
}
Copy after login
package com.jxnu.dao;

import java.util.List;

import com.itheima.domain.Customer;

public interface CustomerDao extends Dao<Customer> {
		List<Customer> findRecords(int startIndex,int pageSize);
}
Copy after login

借助了hibernate中的一个jar包hibernate3.jar

package com.jxnu.dao.impl;

import java.io.Serializable;

import org.hibernate.Session;

import com.itheima.dao.Dao;

public class BaseDao<T> implements Dao<T> {

	private Session session = null;
	private Class clazz;
	public BaseDao(Class clazz)
	{
		this.clazz=clazz;
	}
	public void add(T t) {
		// TODO Auto-generated method stub
		session.save(t);
	}

	public void update(T t) {
		// TODO Auto-generated method stub
		session.update(t);
	}

	public void delete(Serializable id) {
		// TODO Auto-generated method stub
		
	}

	public T findOne(Serializable id) {
		// TODO Auto-generated method stub
		return (T) session.get(clazz, id);
	}

	
}
Copy after login
package com.jxnu.dao.impl;

import java.util.List;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
	public UserDaoImpl(){
		super(User.class);
	}
	@Override
	public List<User> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

}
Copy after login
package com.jxnu.dao.impl;

import java.util.List;

import com.itheima.dao.CustomerDao;
import com.itheima.domain.Customer;

public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao {
	
	public CustomerDaoImpl(){
		super(Customer.class);
	}
	public List<Customer> findRecords(int startIndex, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

}
Copy after login
以上就是dao的模板,简化了很多代码,很精简,不是代码多就好,而是要好,要清晰。
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template