DAO架构的小总结
整理一下在悠悠校园中的DAO层的设计思路。
项目的目录的结构
首先是BaseDao
public interface BaseDao<T> {
T get(Class<T> entityClazz, Serializable id);
Serializable save(T entity);
void update(T entity);
void delete(T entity);
void delete(Class<T> entityClazz, Serializable id);
List<T> findAll(Class<T> entityClazz);
}
接着是BaseDaoImpl,这里采用的是hibernate作为数据库访问的方法
package com.yoyo.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import com.yoyo.cons.CommonConstant;
import com.yoyo.dao.inter.BaseDao;
public class BaseDaoImpl<T> implements BaseDao<T> {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
@SuppressWarnings("unchecked")
public T get(Class<T> entityClazz, Serializable id) {
return (T) getSessionFactory().getCurrentSession().get(entityClazz, id);
}
public Serializable save(T entity) {
return getSessionFactory().getCurrentSession().save(entity);
}
public void update(T entity) {
getSessionFactory().getCurrentSession().saveOrUpdate(entity);
}
public void delete(T entity) {
getSessionFactory().getCurrentSession().delete(entity);
}
public void delete(Class<T> entityClazz, Serializable id) {
delete(get(entityClazz, id));
}
public List<T> findAll(Class<T> entityClazz) {
return find("from " + entityClazz.getSimpleName());
}
@SuppressWarnings("unchecked")
protected List<T> find(String hql) {
return (List<T>) getSessionFactory().getCurrentSession()
.createQuery(hql).list();
}
@SuppressWarnings("unchecked")
protected List<T> find(String hql, Object... params) {
Query query = getSessionFactory().getCurrentSession().createQuery(hql);
for (int i = 0, len = params.length; i < len; i++) {
query.setParameter(i + "", params[i]);
}
return (List<T>) query.list();
}
protected List<T> findByPage(String hql, int pageNo,Object... params){
Query query = getSessionFactory().getCurrentSession().createQuery(hql);
for (int i = 0, len = params.length; i < len; i++) {
query.setParameter(i + "", params[i]);
}
int pageSize = new CommonConstant().PAGE_SIZE;
query.setFirstResult(pageNo * pageSize);
query.setMaxResults(pageSize);
return (List<T>) query.list();
}
}
BaseDomain的设计
package com.yoyo.domain;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
public class BaseDomain implements Serializable{
public String toString(){
return ToStringBuilder.reflectionToString(this);
}
}
下面以MainUser作为案例给出相应的实现类
MainUserDao
package com.yoyo.dao.inter;
import java.util.List;
import java.util.Set;
import com.yoyo.domain.Comment;
import com.yoyo.domain.MainUser;
public interface MainUserDao {
public List<MainUser> findAll();
public MainUser getMainUserById(int i);
public MainUser getMainUserByName(String name);
public void UpdateMainUser(MainUser mainUser);
public void DeleteMainUser(MainUser mainUser);
public void AddMainUser(MainUser mainUser);
public Set<Comment> getAllComments(MainUser mainUser);
}
MainUserDaoImpl
package com.yoyo.dao.impl;
import java.util.List;
import java.util.Set;
import com.yoyo.dao.inter.MainUserDao;
import com.yoyo.domain.Comment;
import com.yoyo.domain.MainUser;
public class MainUserDaoImpl extends BaseDaoImpl<MainUser> implements MainUserDao{
private final String GET_MAINUSER_BY_ID = "from MainUser u where u.uid = ?0";
private final String GET_MAINUSER_BY_NAME = "from MainUser u where u.username = ?0";
public List<MainUser> findAll(){
List<MainUser> list = find("from MainUser");
if(list.size() == 0)
System.out.println("Emptuy List");
return list;
}
public MainUser getMainUserById(int i){
List<MainUser> list = find(GET_MAINUSER_BY_ID, i);
if(list.isEmpty()){
System.out.println("isEmpty");
}
return list.get(0);
}
public MainUser getMainUserByName(String name){
List<MainUser> list = find(GET_MAINUSER_BY_NAME, name);
if(list.isEmpty()){
System.out.println("IsEmpty");
}
return list.get(0);
}
public void UpdateMainUser(MainUser mainUser){
update(mainUser);
}
public void DeleteMainUser(MainUser mainUser){
delete(mainUser);
}
public void AddMainUser(MainUser mainUser){
save(mainUser);
}
public Set<Comment> getAllComments(MainUser mainUser){
Set<Comment> list = mainUser.getCommentList();
return list;
}
}
MainUser
package com.yoyo.domain;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import com.sun.org.apache.bcel.internal.generic.NEW;
@Entity
@Table(name="tao_mainusers")
public class MainUser extends BaseDomain{
private int uid;
private String userimage;
private String username;
private String qq;
private String phoneNum;
private String phonePublic;
private int score;
private String sign;
private int sex;
private String home_city;
private String home_province;
private Date birthday;
private String school;
private int schoolTime;
private int collectionPublic;
private Date signinDate;
private String homeArea;
private Set<Comment> commentList;
private Set<LotteryResult> lotteryResults;
@Id
@GeneratedValue
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUserimage() {
return userimage;
}
public void setUserimage(String userimage) {
this.userimage = userimage;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
@Column(name="phonenum")
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
@Column(name="phonepublic")
public String getPhonePublic() {
return phonePublic;
}
public void setPhonePublic(String phonePublic) {
this.phonePublic = phonePublic;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String getSign() {
return sign;
}
public void setSign(String sign) {
this.sign = sign;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getHome_city() {
return home_city;
}
public void setHome_city(String home_city) {
this.home_city = home_city;
}
public String getHome_province() {
return home_province;
}
public void setHome_province(String home_province) {
this.home_province = home_province;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
@Column(name = "schooltime")
public int getSchoolTime() {
return schoolTime;
}
public void setSchoolTime(int schoolTime) {
this.schoolTime = schoolTime;
}
@Column(name = "collectionpublic")
public int getCollectionPublic() {
return collectionPublic;
}
public void setCollectionPublic(int collectionPublic) {
this.collectionPublic = collectionPublic;
}
@Column(name = "signin_date")
public Date getSigninDate() {
return signinDate;
}
public void setSigninDate(Date signinDate) {
this.signinDate = signinDate;
}
@Column(name = "home_area")
public String getHomeArea() {
return homeArea;
}
public void setHomeArea(String homeArea) {
this.homeArea = homeArea;
}
@OneToMany(mappedBy = "mainUser",targetEntity=Comment.class, cascade = CascadeType.ALL, fetch=FetchType.EAGER)
public Set<Comment> getCommentList() {
return commentList;
}
public void setCommentList(Set<Comment> commentList) {
this.commentList = commentList;
}
@OneToMany(mappedBy="mainUser",targetEntity=LotteryResult.class,cascade = CascadeType.ALL,fetch=FetchType.EAGER)
public Set<LotteryResult> getLotteryResults(){
return lotteryResults;
}
public void setLotteryResults(Set<LotteryResult> lotteryResults){
this.lotteryResults = lotteryResults;
}
}
引用和版权说明
欢迎大家分享、转发。在联系原作者并标明出处及原链接,保留作者署名,非商业应用,相同方式共享,本文欢迎转载。非经授权许可,禁止转载。本文采用 CC BY-NC-SA 4.0授权。