Spring3.0+Struts2.2+Hibernate3.6+ExtJS3.2.0+DWR框架 调整三
Spring3.0+Struts2.2+Hibernate3.6+ExtJS3.2.0+DWR框架 整合三
一、实现业务层基础
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;
}
}
一、实现业务层基础
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;
}
}