Struts2 + Spring + Hibernate 通用 Service 和 DAO
我在 Struts2 + Spring + Hibernate 项目开发中总结出了一个Service 和 DAO ,可以用于处理任何的pojo(bean)。使用这两个Service 和 DAO 可以极大地提高开发的效率,不必再分别针对不同的pojo编写对应的Service 和 DAO。内容如下:
DAO:
接口:BaseDao.java
package dao; import java.util.List; /**
* @author 雷霄骅
* 对Object的DAO操作
* 提供了通用的一些方法
*/ public interface BaseDao {
public void save(Object object);
public void delete(Object object);
public void update(Object object);
public Object ReadSingle(String targetName,String propertyName,Object value);
public List<Object> ReadByProperty(String targetName,String propertyName,Object value);
public List<Object> ReadAll(String targetName);
public List<Object> ReadAllByOrder(String targetName,String propertyName,String order);
public Object get(int id);
public List<Object> ReadByPropertyList(String targetName,List<String> propertyName, List<Object> value);
public Integer ReadCount(String targetName);
public List<Object> ReadLimitedByOrder(String targetName, String propertyName,int num, String order);
}
实现:BaseDaoImpl.java
package 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; /**
* @author 雷霄骅
* HibernateTemplate提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作,
* Spring 2.0更增加对命名SQL查询的支持,也增加对分页的支持。大部分情况下,就可完成大多数DAO对象的CRUD操作。
*/
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{ @Override
public void save(Object object) {
getHibernateTemplate().save(object);
//System.out.println("save "+object.toString());
} @Override
public void delete(Object object) {
getHibernateTemplate().delete(object);
} @Override
public void update(Object object) {
getHibernateTemplate().update(object);
//改用saveOrUpdate,在评价的时候,第一次创建的时候Save,其他时候Update
//getHibernateTemplate().saveOrUpdate(object);
//System.out.println("update "+object.toString());
} @SuppressWarnings("unchecked")
@Override
public Object ReadSingle(final String targetName,final String propertyName, final Object value) {
// TODO Auto-generated method stub
return (Object) getHibernateTemplate().execute(new HibernateCallback() {
/*doInHibernate()。session的创建和销毁,一切都在程序内部完成。*/
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from "+targetName+" as "+targetName+" where "+targetName+"." + propertyName + "=:value";
Query query = session.createQuery(hql);
query.setParameter("value", value);
return query.uniqueResult();
}
});
} @SuppressWarnings("unchecked")
@Override
public List<Object> ReadAll(String targetName) {
// TODO Auto-generated method stub
String hql="from "+targetName;
return getHibernateTemplate().find(hql);
} @SuppressWarnings("unchecked")
@Override
public List<Object> ReadByProperty(final String targetName, final String propertyName,
final Object value) {
// TODO Auto-generated method stub
return (List<Object>) getHibernateTemplate().execute(new HibernateCallback() {
/*doInHibernate()。session的创建和销毁,一切都在程序内部完成。*/
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "from "+targetName+" as "+targetName+" where "+targetName+"." + propertyName + "=:value";
Query query = session.createQuery(hql);
query.setParameter("value", value);
return query.list();
}
});
}
//比ReadByProperty简单很多
@Override
public Object get(int id) {
// TODO Auto-generated method stub
return getHibernateTemplate().get(Object.class, id);
} @Override
public List<Object> ReadByPropertyList(final String targetName,
final List<String> propertyName, final List<Object> value) {
// TODO Auto-generated method stub
return (List<Object>) getHibernateTemplate().execute(new HibernateCallback() {
/*doInHibernate()。session的创建和销毁,一切都在程序内部完成。*/
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String hql = "from "+targetName+" as "+targetName; //-------------------------------
for(int i=0;i<propertyName.size();i++){
String propertynametemp= propertyName.get(i);
Object propertyvaluetemp= value.get(i);
if(propertynametemp!=null){
if(i==0){
hql=hql+" where "+targetName+"." + propertynametemp + "=" + propertyvaluetemp +" ";
}else{
hql=hql+" and "+targetName+"." +propertynametemp + "=" + propertyvaluetemp +" ";
}
}
}
//-------------------------------
Query query = session.createQuery(hql);
//当返回的数据不是一条的时候,不用uniqueresult(),而用list()
return query.list();
}
});
}
//这里在Hibernate2.0之前版本list.get(0)返回的是Integer类型.
//但是在Hibernate3.0以后版本list.get(0)返回的是Long类型.
//所以在这里不可以由Long型强转成Integer类型.
//Integer属于不可更改类型,而且Long和Integer没有任何继承关系,当然不能这样转换。
@Override
public Integer ReadCount(final String targetName) {
// TODO Auto-generated method stub
return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
/*doInHibernate()。session的创建和销毁,一切都在程序内部完成。*/
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql = "select count(*) from "+targetName;
//System.out.println(hql);
//注:java.lang.Number是Integer,Long的父类.
return ((Number)session.createQuery(hql).iterate().next()).intValue();
}
});
} @Override
public List<Object> ReadLimitedByOrder(final String targetName,
final String propertyName, final int num, final String order) {
// TODO Auto-generated method stub
return (List<Object>) getHibernateTemplate().execute(new HibernateCallback() {
/*doInHibernate()。session的创建和销毁,一切都在程序内部完成。*/
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String hql ="from "+targetName+" as "+targetName+ " order by "+targetName+"." + propertyName+ " " + order;
Query query = session.createQuery(hql);
query.setMaxResults(num);
//当返回的数据不是一条的时候,不用uniqueresult(),而用list()
return query.list();
}
});
} @Override
public List<Object> ReadAllByOrder(String targetName, String propertyName,
String order) {
// TODO Auto-generated method stub
String hql="from "+targetName+" as "+targetName+ " order by "+targetName+"." + propertyName+ " " + order;
return getHibernateTemplate().find(hql);
} }
Service:
接口:BaseService.java
package service; import java.util.List; /**
* @author 雷霄骅
* 对Object的Service
* 提供了一些通用的方法
*/
public interface BaseService {
public void save(Object object);
public void update(Object object);
public void delete(Object object);
public Object ReadByID(String targetName,int id);
@SuppressWarnings("rawtypes")
public List ReadAll(String targetName);
public List ReadAllByOrder(String targetName,String propertyName,String order);
@SuppressWarnings("rawtypes")
public List ReadByProperty(String targetName,String propertyName,Object propertyValue);
public List ReadByPropertyList(String targetName,List<String> propertyName,List<Object> propertyValue);
public List ReadLimitedByOrder(String targetName,String propertyName,int num,String order);
public Object ReadSingle(String targetName,String propertyName,Object propertyValue);
public int ReadCount(String targetName); }
实现:BaseServiceImpl.java
package service; import java.util.ArrayList;
import java.util.List; import dao.BaseDao;
/**
* @author 雷霄骅
* 对Object的Service
* 提供了一些通用的方法
*/
public class BaseServiceImpl implements BaseService { private BaseDao baseDao;
@Override
public void save(Object object) {
// TODO Auto-generated method stub
baseDao.save(object);
} @Override
public void update(Object object) {
// TODO Auto-generated method stub
baseDao.update(object);
} @Override
public void delete(Object object) {
// TODO Auto-generated method stub
baseDao.delete(object);
} @Override
public Object ReadByID(String targetName,int id) {
// TODO Auto-generated method stub
return baseDao.ReadSingle(targetName,"id", id);
} @SuppressWarnings("rawtypes")
@Override
public List ReadAll(String targetName) {
// TODO Auto-generated method stub
return baseDao.ReadAll(targetName);
} @SuppressWarnings("rawtypes")
@Override
public List ReadAllByOrder(String targetName,String propertyName,String order) {
// TODO Auto-generated method stub
return baseDao.ReadAllByOrder(targetName,propertyName,order);
} public BaseDao getBaseDao() {
return baseDao;
} public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
} @Override
public List ReadByProperty(String targetName, String propertyName,
Object propertyValue) {
// TODO Auto-generated method stub
return baseDao.ReadByProperty(targetName, propertyName, propertyValue);
} @Override
public Object ReadSingle(String targetName, String propertyName,
Object propertyValue) {
// TODO Auto-generated method stub
return baseDao.ReadSingle(targetName, propertyName, propertyValue);
} @Override
public int ReadCount(String targetName) {
// TODO Auto-generated method stub
return baseDao.ReadCount(targetName);
} @Override
public List ReadLimitedByOrder(String targetName, String propertyName,
int num, String order) {
return baseDao.ReadLimitedByOrder(targetName,propertyName,num,order);
} @Override
public List ReadByPropertyList(String targetName,
List<String> propertyName, List<Object> propertyValue) {
// TODO Auto-generated method stub
return baseDao.ReadByPropertyList(targetName,propertyName,propertyValue);
} }
这样,在Action层调用方法的时候,可以直接调用BaseService相应的方法完成操作。
举一个例子:
有这么一个名字叫Blog的pojo:
package bean; import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table; /**
* Blog entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "blog", catalog = "vqe")
public class Blog implements java.io.Serializable { // Fields private Integer id;
private Admin admin;
private String title;
private Timestamp modifytime;
private String content; // Constructors /** default constructor */
public Blog() {
} /** full constructor */
public Blog(Admin admin, String title, Timestamp modifytime, String content) {
this.admin = admin;
this.title = title;
this.modifytime = modifytime;
this.content = content;
} // Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "adminid")
public Admin getAdmin() {
return this.admin;
} public void setAdmin(Admin admin) {
this.admin = admin;
} @Column(name = "title", length = 200)
public String getTitle() {
return this.title;
} public void setTitle(String title) {
this.title = title;
} @Column(name = "modifytime", length = 19)
public Timestamp getModifytime() {
return this.modifytime;
} public void setModifytime(Timestamp modifytime) {
this.modifytime = modifytime;
} @Column(name = "content", length = 10000)
public String getContent() {
return this.content;
} public void setContent(String content) {
this.content = content;
} }
该类代表博客的一篇文章。
在Action层只需调用BaeService对应的方法就能完成相应的操作。换句话说,只要把pojo的类的名字当一个字符串传递给ReadByID这种的函数,就可以实现相应的功能。
//根据ID读取:
Blog blog=(Blog) baseService.ReadByID("Blog", blogid);
//添加:
baseService.save(blog);
//修改:
baseService.update(blog);
//删除:
baseService.delete(blog);
//读取所有(根据时间降序)
List<Blog> resultblog=baseService.ReadAllByOrder("Blog","modifytime","desc");
//读取num条(根据时间降序)
List<Blog> resultblog=baseService.ReadLimitedByOrder("Blog","modifytime",num,"desc");
完整的实现Blog(博客)的增删改查的Action示例:
package action; import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map; import service.BaseService; import bean.Admin;
import bean.Blog; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author 雷霄骅
* Action
*/
public class BlogAct extends ActionSupport {
private int blogid;
private int num;
private Blog blog;
private List<Blog> resultblog;
private BaseService baseService; public int getBlogid() {
return blogid;
} public void setBlogid(int blogid) {
this.blogid = blogid;
} public Blog getBlog() {
return blog;
} public void setBlog(Blog blog) {
this.blog = blog;
} public BaseService getBaseService() {
return baseService;
} public void setBaseService(BaseService baseService) {
this.baseService = baseService;
} public List<Blog> getResultblog() {
return resultblog;
} public void setResultblog(List<Blog> resultblog) {
this.resultblog = resultblog;
} public int getNum() {
return num;
} public void setNum(int num) {
this.num = num;
} public String Add(){
try{
//--------------------------------
ActionContext context = ActionContext.getContext();
Map sessionMap = context.getSession();
Admin admin=(Admin)sessionMap.get("admin");
//--------------------------------
blog.setModifytime( new Timestamp(new Date().getTime()));
blog.setAdmin(admin);
baseService.save(blog);
return SUCCESS;
}
catch(Exception ex){
ex.printStackTrace();
return ERROR;
}
} public String Delete(){
try{
blog=(Blog) baseService.ReadByID("Blog", blogid);
baseService.delete(blog);
return SUCCESS;
}
catch(Exception ex){
ex.printStackTrace();
return ERROR;
}
} public String Read(){
try{
blog=(Blog) baseService.ReadByID("Blog", blogid);
return SUCCESS;
}
catch(Exception ex){
ex.printStackTrace();
return ERROR;
}
} public String Update(){
try{
//--------------------------------
ActionContext context = ActionContext.getContext();
Map sessionMap = context.getSession();
Admin admin=(Admin)sessionMap.get("admin");
//--------------------------------
blog.setModifytime( new Timestamp(new Date().getTime()));
blog.setAdmin(admin);
baseService.update(blog);
return SUCCESS;
}
catch(Exception ex){
ex.printStackTrace();
return ERROR;
}
} public String ReadAll(){
try{
resultblog=baseService.ReadAllByOrder("Blog","modifytime","desc");
return SUCCESS;
}
catch(Exception ex){
ex.printStackTrace();
return ERROR;
}
} public String ReadLimitedByOrder(){
try{
resultblog=baseService.ReadLimitedByOrder("Blog","modifytime",num,"desc");
return SUCCESS;
}
catch(Exception ex){
ex.printStackTrace();
return ERROR;
}
}
}
Struts2 + Spring + Hibernate 通用 Service 和 DAO的更多相关文章
- SSH面试题(struts2+Spring+hibernate)
struts2 + Spring +hibernate Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory ...
- Struts2+Spring+Hibernate整合开发(Maven多模块搭建)
Struts2+Spring+Hibernate整合开发(Maven多模块搭建) 0.项目结构 Struts2:web层 Spring:对象的容器 Hibernate:数据库持久化操作 1.父模块导入 ...
- SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>
此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...
- SSH框架简化(struts2+spring+hibernate)
目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...
- Struts2 + Spring + Hibernate
Struts2 + Spring + Hibernate整合. 使用的是无配置方法进行SSH的整合,struts-convertion plugin + spring annotation + hib ...
- Struts2+Spring+Hibernate 三大框架的合并集成
这次来看看Struts2+Spring+Hibernate三大框架的整合应用,主要是Spring和Hibernate框架的整合,因为前边已经将Strtus2+Spring整合过了基本一样. 首先看一 ...
- Spring、Struts2+Spring+Hibernate整合步骤
所使用的Jar包: Hibernate: Spring(使用MyEclipse自动导入框架功能) Struts2: 注解包和MySql驱动包: 1.配置Hibernate和Spring: <be ...
- Maven搭建struts2+spring+hibernate环境
Maven搭建struts2+spring+hibernate环境(一) 本文简单的使用STS的自带的maven插件工具搭建ssh(struts2+spring+hibernate)开发环境,图文并茂 ...
- SSH---整合Struts2&Spring&Hibernate(实例)
一.SSH回顾 Struts2:核心为过滤器+拦截器.过程:Filter--->FilterDispatcher-->ActionMapper-->ActionProxy--> ...
随机推荐
- c++ string assign =
C++ string类的成员函数,用于拷贝.赋值操作,它们允许我们顺次地把一个string 对象的部分内容拷贝到另一个string 对象上. string &operator=(const s ...
- C++中的INL
inl文件介绍 inl文件是内联函数的源文件.内联函数通常在C++头文件中实现,但是当C++头文件中内联函数过多的情况下,我们想使头文件看起来简洁点,能不能像普通函数那样将内联函数声明和函数定义放在头 ...
- C#基础精华02(静态类,值类型,引用类型,枚举,结构,ref与out)
静态类 静态类不能被其他类继承,静态成员亦不能被继承(访问的是同一个),备注1. 静态类只能继承自Object类.(静态类不能继承自其它类.) 继承(多态).静态本身就是相反的. 静态类不能实现任何接 ...
- vs2015 打不开了 提示"CSharpPackage",未能正确加载xx包
原文:vs2015 打不开了 提示"CSharpPackage" 最近发现vs2015 在新建项目和加载现有项目的时候会报错 提示 开始我以为是系统的问题导致vs 配置除了问题,重 ...
- windows 下 文件属性及目录列表操作
转:http://blog.sina.com.cn/s/blog_686d0fb001012tsg.html 我们需要一个结构体和几个函数.这些函数和结构体在<io.h>的头文件中,结构体 ...
- Android在java代码中设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...
- Android开发之全屏显示的两种方法
1.通过修改清单文件中Theme,实现全屏 <application android:name=".MyApplication" android:allowBackup=&q ...
- UML中关系图解
转自http://blog.csdn.net/duran1986/article/details/5573415 最近在教软件工程项目实践,就又仔细了解了下UML中各种关系的意义,虽然有点简单,但是有 ...
- POI根据EXCEL模板,修改内容导出新EXCEL (只支持HSSF)
package excelPoiTest; import java.io.File; import java.io.FileInputStream; import java.io.FileOutput ...
- xml 实现圆形图 和 椭圆形图
1. 效果图 2.圆形图 <ImageView android:layout_width="wrap_content" android:layout_height=" ...