一、实现业务层基础
1、业务层基础类接口
package com.hanz.service.base;
public interface BaseServcie {
/**
* 以对象类型和对象编号为标示,获取相对应的对象
*
* @param pojoClass
* 对象类型
* @param id
* 对象编号
* @return 获取的简单对象
* @exception RuntimeException
*/
public Object get(Class pojoClass, int id) throws RuntimeException;
/**
* 创建新对象
*
* @param pojo
* Object 新对象
* @throws RuntimeException
*/
public boolean save(Object pojo) throws RuntimeException;
/**
* 更新已有对象
*
* @param pojo
* Object 需要更新的对象
* @throws RuntimeException
*/
public boolean update(Object pojo) throws RuntimeException;
/**
* 以对象类型和对象编号为标示,删除相对应的对象
*
* @param pojoClass
* 对象类型
* @param id
* 对象编号
* @exception RuntimeException
*/
public boolean delete(Class pojo, Integer id) throws RuntimeException;
/**
* 删除集合中的全部对象
*
* @param pojo
* pojo
* @param ids
* 要删除的ID集合
* @throws RuntimeException
*/
public boolean deleteAll(Class pojo, String[] ids) throws RuntimeException;
/**
* 插入或者更新对象
*
* @param pojo
* Object 目标pojo对象
* @throws RuntimeException
*/
public boolean insertOrUpdate(Object pojo) throws RuntimeException;
}
2、业务层基础接口实现类
package com.hanz.service.base.imp;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.hanz.dao.base.BaseDao;
import com.hanz.service.base.BaseServcie;
@Component("baseService")
public class BaseServiceImp implements BaseServcie {
private BaseDao baseDao;
@Resource(name = "baseDao")
public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}
/*
* (non-Javadoc)
*
* @see com.hanz.service.BaseService.IBaseService#delete(java.lang.Class,
* long)
*/
public boolean delete(Class pojo, Integer id) throws RuntimeException {
try {
baseDao.delete(baseDao.loadById(pojo, id));
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/*
* (non-Javadoc)
*
* @see com.hanz.service.BaseService.IBaseService#get(java.lang.Class, long)
*/
public Object get(Class pojoClass, int id) throws RuntimeException {
return baseDao.loadById(pojoClass, id);
}
/*
* (non-Javadoc)
*
* @see
* com.hanz.service.BaseService.IBaseService#insertOrUpdate(java.lang.Object
* )
*/
public boolean insertOrUpdate(Object pojo) throws RuntimeException {
try {
baseDao.insertOrUpdate(pojo);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/*
* (non-Javadoc)
*
* @see com.hanz.service.BaseService.IBaseService#save(java.lang.Object)
*/
public boolean save(Object pojo) throws RuntimeException {
try {
baseDao.save(pojo);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/*
* (non-Javadoc)
*
* @see com.hanz.service.BaseService.IBaseService#update(java.lang.Object)
*/
public boolean update(Object pojo) throws RuntimeException {
try {
baseDao.update(pojo);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/*
* (non-Javadoc)
*
* @see com.hanz.service.BaseService.IBaseService#deleteAll(java.lang.Class,
* java.lang.String[])
*/
public boolean deleteAll(Class pojoName, String[] ids)
throws RuntimeException {
try {
Integer[] intIds = new Integer[ids.length];
for (int i = 0; i < ids.length; i++) {
intIds[i] = Integer.parseInt(ids[i]);
}
baseDao.deleteAll(pojoName, intIds);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
3、自定义业务接口
package com.hanz.service;
import com.hanz.action.PageBean;
import com.hanz.domain.User;
import com.hanz.service.base.BaseServcie;
public interface UserService extends BaseServcie {
public PageBean queryForPage(int start, int limit) throws RuntimeException;
public PageBean queryForPage() throws RuntimeException;
public PageBean queryForPage(final String hql,int start, int limit) throws RuntimeException;
public User login(User user) throws RuntimeException;
}
4、实现自定义业务接口类
package com.hanz.service.imp;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.hanz.action.PageBean;
import com.hanz.dao.UserDAO;
import com.hanz.domain.User;
import com.hanz.service.UserService;
import com.hanz.service.base.imp.BaseServiceImp;
@Component("userService")
public class UserServiceImp extends BaseServiceImp implements UserService {
private UserDAO userDAO;
public void init() {
System.out.println("init");
}
@Resource(name = "userDao")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
/**
* 分页查询
*
* @param currentPage
* 当前第几页
* @param pageSize
* 每页大小
* @return 封闭了分页信息(包括记录集list)的Bean
*/
public PageBean queryForPage(int start, int limit) {
final String hql = "from User"; // 查询语句
int totalCount = userDAO.getAllRowCount(hql); // 总记录数
List<User> list = userDAO.queryForPage(hql, start, limit); // "一页"的记录
// 把分页信息保存到Bean中
PageBean pageBean = new PageBean();
pageBean.setTotalCount(totalCount);
pageBean.setList(list);
return pageBean;
}
public void destroy() {
System.out.println("destroy");
}
@Override
public PageBean queryForPage() throws RuntimeException {
final String hql = "from User"; // 查询语句
int totalCount = userDAO.getAllRowCount(hql); // 总记录数
List<User> list = userDAO.queryForPage(hql); // "一页"的记录
// 把分页信息保存到Bean中
PageBean pageBean = new PageBean();
pageBean.setTotalCount(totalCount);
pageBean.setList(list);
return pageBean;
}
/**
* 分页查询
*
* @param currentPage
* 当前第几页
* @param pageSize
* 每页大小
* @return 封闭了分页信息(包括记录集list)的Bean
*/
public PageBean queryForPage(final String hql,int start, int limit) {
long totalCount = userDAO.getAllRowCount(hql); // 总记录数
List<User> list = userDAO.queryForPage(hql, start, limit); // "一页"的记录
// 把分页信息保存到Bean中
PageBean pageBean = new PageBean();
pageBean.setTotalCount(totalCount);
pageBean.setList(list);
return pageBean;
}
@Override
public User login(User user) throws RuntimeException {
// TODO Auto-generated method stub
try {
return userDAO.getUser(user);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
分享到:
相关推荐
项目配置:Hibernate3.3.2+Spring3.0+Struts2.2.3 + ext3.4.0,真实项目实现,项目中可参考经典权限设计,通用数据录入,批量数据处理(poi),统一的JSON格式封装,灵活的组合模式设计,EXTJS面向组件编程参考......
Spring3.0+Hibernate4.0+SpringMVC整合ExtJS4是一个集成的应用系统,希望对学习javaweb和extjs的朋友有帮助
ExtJSweb 开发指南中的 简单图书管理系统 ExtJS3.0+spring+hibernate+struts
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。这个是分卷,同时下载2.3.4部分
Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0_Annotations方式快速开发框架资料。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
extjs+spring+struts+hibernate整合实例
Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS...
extjs3.0+struts2 实现登陆 验证码 tree树的加载 首页面。 主要代码还是借鉴别人,然后我再原有基础上再加工。 还没添加数据库类。对于初学者来说还是不错的入门源码。
Struts2(json-plugin) + Spring2 + ExtJS2.2 开源网络硬盘系统
Struts2+Spring+Hibernate+ExtJS开发CRUD功能
Hibernate+Spring+Struts2+ExtJS开发CRUD功能 ,里面包含源码及安装说明文档。可以作为项目开发的基础框架。
Hibernate+Spring+Struts2+ExtJS整合开发实例 非常经典!学习的好帮助!
strut2+hibernate+spring+extjs3.0+excel 动态加载grid 数据导出excel,导入excel datasource自己修改你自己的数据源,数据库中的表可见实体类创建,或者自己修改实体类
采用了struts2.0,spring2.2,ibatis,quartz,extjs3.0 实现了关于短信的调度框架,对接收人维护,动态添加每日短信数据,编写短信模版,最后通过quartz定时发送,因为短信接口收费所以不提供,需要jar包自己下载,...
利用dwr,整合extjs,spring,hibernate.是你学习dwr的参考指南
Extjs3.1+struts2+spring2.5+hibernate3.5环境