JavaWeb项目开发案例精粹-第4章博客网站系统-003Dao层

时间:2022-04-24 11:50:25

1.

 package com.sanqing.dao;

 import java.util.List;

 import com.sanqing.fenye.Page;
import com.sanqing.po.Article; public interface ArticleDAO {
//定义一个add方法,用来保存博客文章
public void add(Article article); //定义一个queryUserAll,用来取出用户的所有文章
public List<Article> queryUserAll(String username); //获得用户文章总记录数
public int queryUserAllCount(String username); //按分页信息查询用户的记录
public List<Article> queryByPage(String username,Page page); //获得所有文章的总记录数
public int queryAllCount(); //按分页信息查询记录
public List<Article> queryAllByPage(Page page); //按ID查询文章
public Article queryById(int id);
}

2.

 package com.sanqing.dao;

 import java.sql.SQLException;
import java.util.List; import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.sanqing.fenye.Page;
import com.sanqing.po.Article; public class ArticleDAOImpl extends HibernateDaoSupport implements ArticleDAO { //实现add方法
public void add(Article article) {
this.getHibernateTemplate().saveOrUpdate(article);
} public List<Article> queryUserAll(String username) {
List find = this.getHibernateTemplate().find("select art from Article art where art.username = ?", username);
List<Article> list = find;
return list;
} public int queryUserAllCount(String username) {
List find = this.getHibernateTemplate().find("select count(*) from Article art where art.username = ?",username);
return ((Long)find.get(0)).intValue();
} public List<Article> queryByPage(final String username, final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("select art from Article art where art.username = ?");
//设置参数
query.setParameter(0, username);
//设置每页显示多少个,设置多大结果。
query.setMaxResults(page.getEveryPage());
//设置起点
query.setFirstResult(page.getBeginIndex());
return query.list();
} });
} public List<Article> queryAllByPage(final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("select art from Article art order by art.date desc");
//设置每页显示多少个,设置多大结果。
query.setMaxResults(page.getEveryPage());
//设置起点
query.setFirstResult(page.getBeginIndex());
return query.list();
} });
} public int queryAllCount() {
List find = this.getHibernateTemplate().find("select count(*) from Article art");
return ((Long)find.get(0)).intValue();
} public Article queryById(int id) {
List find = this.getHibernateTemplate().find("select art from Article art where art.id = ?", id);
return (Article) find.get(0);
} }

3.

 package com.sanqing.dao;

 import com.sanqing.po.BlogInfo;

 public interface BlogInfoDAO {
//设置个性化内容
public void save(BlogInfo info);
//获得
public BlogInfo get(String username);
}

4.

 package com.sanqing.dao;

 import java.util.List;

 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 import com.sanqing.po.BlogInfo;

 public class BlogInfoDAOImpl extends HibernateDaoSupport implements BlogInfoDAO{

     public BlogInfo get(String username) {
List list = this.getHibernateTemplate().find("select bloginfo from BlogInfo bloginfo where bloginfo.username=?", username);
if(list.size() == 0) {
return null;
} else {
return (BlogInfo) list.get(0);
}
} public void save(BlogInfo info) {
this.getHibernateTemplate().saveOrUpdate(info);
} }

5.

 package com.sanqing.dao;

 import java.util.List;

 import com.sanqing.fenye.Page;
import com.sanqing.po.Critique; public interface CritiqueDAO {
//添加评论
public void addCritique(Critique critique); //获得指定文章的评论数
public int queryCritiqueCount(int AId); //根据Page来查询指定文章的评论
public List<Critique> queryByPage(int AId,Page page);
}

6.

 package com.sanqing.dao;

 import java.sql.SQLException;
import java.util.List; import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.sanqing.fenye.Page;
import com.sanqing.po.Critique; public class CritiqueDAOImpl extends HibernateDaoSupport implements CritiqueDAO { public void addCritique(Critique critique) {
this.getHibernateTemplate().save(critique);
} public List<Critique> queryByPage(final int AId, final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("select cri from Critique cri where cri.AId = ?");
query.setParameter(0, AId);
//设置每页显示多少个,设置多大结果。
query.setMaxResults(page.getEveryPage());
//设置起点
query.setFirstResult(page.getBeginIndex());
return query.list();
} });
} public int queryCritiqueCount(int AId) {
List find = this.getHibernateTemplate().find("select count(*) from Critique cri where cri.AId = ?",AId);
return ((Long)find.get(0)).intValue();
} }

7.

 package com.sanqing.dao;

 import java.util.Date;
import java.util.List; import com.sanqing.po.Dianjiliang; public interface DianjiliangDAO { //返回该文章,该IP,该时间的访问记录
public List queryByAId(int AId,String IP,Date time); //添加访问记录
public void addJilu(Dianjiliang dianjiliang);
}

8.

 package com.sanqing.dao;

 import java.sql.SQLException;
import java.util.Date;
import java.util.List; import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.sanqing.po.Dianjiliang; public class DianjiliangDAOImpl extends HibernateDaoSupport implements
DianjiliangDAO { public void addJilu(Dianjiliang dianjiliang) {
this.getHibernateTemplate().save(dianjiliang);
} public List queryByAId(final int AId, final String IP, final Date time) {
return this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery("select djl from Dianjiliang djl where djl.AId = ? and djl.ip = ? and djl.time = ?");
//设置参数
query.setParameter(0, AId);
query.setParameter(1, IP);
query.setParameter(2, time);
return query.list();
} });
} }

9.

 package com.sanqing.dao;

 import java.util.List;

 import com.sanqing.po.User;

 public interface UserDAO {
//添加用户
public void add(User user); //删除用户
public void delete(User user); //更新用户
public void update(User user); //查询所有用户
public List queryAll(); //按id查询用户
public User queryByID(String username);
}

10.

 package com.sanqing.dao;

 import java.util.List;

 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

 import com.sanqing.po.User;

 public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

     public void add(User user) {
this.getHibernateTemplate().save(user);
} public void delete(User user) { } public List queryAll() {
return null;
} public User queryByID(String username) {
List list = this.getHibernateTemplate().find("select user from User user where user.username = '" + username + "'"); if(list.size() == 0) {
return null;
} else {
return (User)list.get(0);
}
} public void update(User user) { } }