接下来我们看看我们 WEB应用的JAVA代码,首先是各个模块的DAO,它们充分利用了Spring对Hibernate的支持:

package org.leno.hr.dao;

import java.util.ArrayList;

import java.util.List;

import org.hibernate.Session;

import org.leno.hr.User;

import org.springframework.orm.hibernate3.HibernateCallback;

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

public class LogonDAO extends HibernateDaoSupport {

public int removeRecords(final ArrayList<String> ids) throws Exception {

if (ids == null || ids.size() == 0) {

return -1;

}

getHibernateTemplate().execute(new HibernateCallback() {

public Object doInHibernate(Session session) {

for (int i = 0; i < ids.size(); i++) {

session.createQuery(

"delete from userInfo o where o.id=" + ids.get(i))

.executeUpdate();

}

return 1;

}

});

return 1;

}

@SuppressWarnings("unchecked")

public List<User> getRecords() throws Exception {

return getHibernateTemplate().loadAll(User.class);

}

@SuppressWarnings("unchecked")

public List<User> getUserList(String userName, String passWord)

throws Exception {

List<User> results = getHibernateTemplate().find(

"from User u where u.userName='" + userName

+ "' and u.passWord='" + passWord + "'");

return results;

}

public int insertUser(User user) throws Exception {

getHibernateTemplate().persist(user);

return 1;

}

public int updateUser(User user) throws Exception {

getHibernateTemplate().merge(user);

return 1;

}

}

package org.leno.hr.dao;

import java.util.ArrayList;

import java.util.List;

import org.hibernate.Session;

import org.leno.hr.Person;

import org.springframework.orm.hibernate3.HibernateCallback;

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

public class PersonDAO extends HibernateDaoSupport {

public int removeRecords(final ArrayList<String> ids) throws Exception {

if (ids == null || ids.size() == 0) {

return -1;

}

return (Integer) getHibernateTemplate().execute(

new HibernateCallback() {

public Object doInHibernate(Session session) {

for (int i = 0; i < ids.size(); i++) {

session.createQuery(

"delete from Person o where o.id="

+ ids.get(i)).executeUpdate();

}

return 1;

}

});

}

@SuppressWarnings("unchecked")

public List<Person> getRecords(final int page, final int pageSize)

throws Exception {

return (List<Person>) getHibernateTemplate().execute(

new HibernateCallback() {

public Object doInHibernate(final Session session) {

return session.createQuery("from Person")

.setFirstResult(page).setMaxResults(pageSize)

.list();

}

});

}

public int insertPerson(Person person) throws Exception {

getHibernateTemplate().saveOrUpdate(person);

return 1;

}

public int updatePerson(Person person) throws Exception {

getHibernateTemplate().saveOrUpdate(person);

return 1;

}

public int getTotalNums() {

long count = (Long) getHibernateTemplate().execute(

new HibernateCallback() {

public Object doInHibernate(final Session session) {

return (Long) session.createQuery(

"select count(*) from Person").uniqueResult();

}

});

return (int) count;

}

}

package org.leno.hr.dao;

import java.util.ArrayList;

import java.util.List;

import org.hibernate.Session;

import org.leno.hr.Unit;

import org.springframework.orm.hibernate3.HibernateCallback;

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

public class UnitDAO extends HibernateDaoSupport {

public int removeRecords(final ArrayList<String> ids) throws Exception {

if (ids == null || ids.size() == 0) {

return -1;

}

getHibernateTemplate().execute(new HibernateCallback() {

public Object doInHibernate(Session session) {

for (int i = 0; i < ids.size(); i++) {

session.createQuery(

"delete from Unit o where o.id=" + ids.get(i))

.executeUpdate();

}

return 1;

}

});

return 1;

}

@SuppressWarnings("unchecked")

public List<Unit> getRecords() throws Exception {

return getHibernateTemplate().loadAll(Unit.class);

}

public int insertUnit(Unit unit) throws Exception {

getHibernateTemplate().saveOrUpdate(unit);

return 1;

}

public int updateUnit(Unit unit) throws Exception {

getHibernateTemplate().saveOrUpdate(unit);

return 1;

}

}

上面各个模块的DAO都大同小异了,接下来是Service,只是简单调用DAO做事,并且利用了Spring的声明式事务管理:

package org.leno.hr.service;

import java.util.ArrayList;

import java.util.List;

import org.leno.hr.User;

import org.leno.hr.dao.LogonDAO;

public class LogonService {

private LogonDAO logonDAO;

public LogonDAO getLogonDAO() {

return logonDAO;

}

public void setLogonDAO(LogonDAO logonDAO) {

this.logonDAO = logonDAO;

}

public int removeRecords(ArrayList<String> ids) throws Exception {

return logonDAO.removeRecords(ids);

}

public List<User> getRecords() throws Exception {

return logonDAO.getRecords();

}

public List<User> getUserList(String userName, String passWord)

throws Exception {

return logonDAO.getUserList(userName, passWord);

}

public int insertUser(User user) throws Exception {

return logonDAO.insertUser(user);

}

public int updateUser(User user) throws Exception {

return logonDAO.updateUser(user);

}

}

package org.leno.hr.service;

import java.util.ArrayList;

import java.util.List;

import org.leno.hr.Person;

import org.leno.hr.dao.PersonDAO;

public class PersonService {

private PersonDAO personDAO;

public PersonDAO getPersonDAO() {

return personDAO;

}

public void setPersonDAO(PersonDAO personDAO) {

this.personDAO = personDAO;

}

public int removeRecords(ArrayList<String> ids) throws Exception {

return personDAO.removeRecords(ids);

}

public List<Person> getRecords(int page, int pageSize) throws Exception {

return personDAO.getRecords(page, pageSize);

}

public int insertPerson(Person person) throws Exception {

return personDAO.insertPerson(person);

}

public int updatePerson(Person person) throws Exception {

return personDAO.updatePerson(person);

}

public int getTotalNums() {

return personDAO.getTotalNums();

}

}

package org.leno.hr.service;

import java.util.ArrayList;

import java.util.List;

import org.leno.hr.Unit;

import org.leno.hr.dao.UnitDAO;

public class UnitService  {

private UnitDAO unitDAO;

public int removeRecords(ArrayList<String> ids) throws Exception {

return unitDAO.removeRecords(ids);

}

public List<Unit> getRecords() throws Exception {

return unitDAO.getRecords();

}

public UnitDAO getUnitDAO() {

return unitDAO;

}

public void setUnitDAO(UnitDAO unitDAO) {

this.unitDAO = unitDAO;

}

public int insertUnit(Unit unit) throws Exception {

return unitDAO.insertUnit(unit);

}

public int updateUnit(Unit unit) throws Exception{

return unitDAO.updateUnit(unit);

}

}

最后是我们各模块的Spring核心控制类:

package org.leno.hr.controller;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.leno.hr.User;

import org.leno.hr.service.LogonService;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class LogonController implements Controller {

private LogonService logonService;

public LogonService getLogonService() {

return logonService;

}

public void setLogonService(LogonService logonService) {

this.logonService = logonService;

}

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) throws Exception {

String isUserManagerOperation = request

.getParameter("isUserManagerOperation");

String userName = request.getParameter("userName");

String passWord = request.getParameter("passWord");

if (isUserManagerOperation == null) {

response.setContentType("text/html; charset=UTF-8");

PrintWriter pw = response.getWriter();

if (userName == null || passWord == null || userName.equals("")

|| passWord.equals("")) {

pw.print("用户名或者密码不能为空!");

} else if (userName.indexOf(" ") != -1

|| passWord.indexOf(" ") != -1) {

pw.print("输入无效!");

}

List<User> results = logonService.getUserList(userName, passWord);

if (results.size() == 1) {

User user = (User) results.get(0);

if (user.getIsValid() != 1) {

pw.print("该用户已被锁�?!");

} else {

request.getSession().setAttribute("user", user);

pw.print("success");

}

} else {

pw.print("用户名或者密码不正确!");

}

} else {

String id = request.getParameter("id");

String isValid = request.getParameter("isValid");

int count;

response.setContentType("text/html; charset=UTF-8");

PrintWriter pw = response.getWriter();

User user = new User(userName, passWord, Integer.parseInt(isValid));

if (id == null && userName != null) {

count = logonService.insertUser(user);

if (count == 1) {

pw.print(count);

} else {

pw.print("error");

}

} else if (id != null && userName != null) {

user.setId(Integer.parseInt(id));

count = logonService.updateUser(user);

if (count == 1) {

pw.print(count);

} else {

pw.print("error");

}

}

}

return null;

}

public int removeRecords(ArrayList<String> ids) throws Exception {

return logonService.removeRecords(ids);

}

public List<User> getRecords() throws Exception {

return logonService.getRecords();

}

}

package org.leno.hr.controller;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import org.leno.hr.Person;

import org.leno.hr.service.PersonService;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class PersonController implements Controller {

private PersonService personService = null;

public int removeRecords(ArrayList<String> ids) throws Exception {

return personService.removeRecords(ids);

}

public PersonService getPersonService() {

return personService;

}

public void setPersonService(PersonService personService) {

this.personService = personService;

}

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) throws Exception {

String action = request.getParameter("action");

if (action != null) {

response.setCharacterEncoding("utf-8");

//分页实现

String start = request.getParameter("start");

String limit = request.getParameter("limit");

int index = Integer.parseInt(start);

int pageSize = Integer.parseInt(limit);

List<Person> list = personService.getRecords(index, pageSize);

JSONObject json = new JSONObject();

json.put("totalProperty", personService.getTotalNums());

JSONArray arr = JSONArray.fromObject(list);

json.put("root", arr);

System.out.println(json);

response.getWriter().print(json);

return null;

}

String id = request.getParameter("id");

String name = request.getParameter("name");

String age = request.getParameter("age");

String unitName = request.getParameter("unitName");

String address = request.getParameter("address");

String telephone = request.getParameter("telephone");

String gender = request.getParameter("gender");

int count;

response.setContentType("text/html; charset=UTF-8");

PrintWriter pw = response.getWriter();

Person person = new Person(name, age, unitName, address, telephone,

gender);

if (id == null && name != null) {

count = personService.insertPerson(person);

if (count == 1) {

pw.print(count);

} else {

pw.print("error");

}

} else if (id != null && name != null) {

person.setId(Integer.parseInt(id));

count = personService.updatePerson(person);

if (count == 1) {

pw.print(count);

} else {

pw.print("error");

}

}

return null;

}

}

package org.leno.hr.controller;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.leno.hr.Unit;

import org.leno.hr.service.UnitService;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.Controller;

public class UnitController implements Controller {

private UnitService unitService = null;

public UnitService getUnitService() {

return unitService;

}

public void setUnitService(UnitService unitService) {

this.unitService = unitService;

}

public int removeRecords(ArrayList<String> ids) throws Exception {

return unitService.removeRecords(ids);

}

public List<Unit> getRecords() throws Exception {

return unitService.getRecords();

}

public ModelAndView handleRequest(HttpServletRequest request,

HttpServletResponse response) throws Exception {

String id = request.getParameter("id");

String name = request.getParameter("name");

String description = request.getParameter("description");

String parentId = request.getParameter("parentId");

int count;

response.setContentType("text/html; charset=UTF-8");

PrintWriter pw = response.getWriter();

Unit unit = new Unit(name, description, Integer.parseInt(parentId));

if (id == null && name != null) {

count = unitService.insertUnit(unit);

if (count == 1) {

pw.print(count);

} else {

pw.print("error");

}

} else if (id != null && name != null) {

unit.setId(Integer.parseInt(id));

count = unitService.updateUnit(unit);

if (count == 1) {

pw.print(count);

} else {

pw.print("error");

}

}

return null;

}

}

上面的JAVA代码并不晦涩,熟悉SSH的同志扫一下就清楚了。现在我们的配置文件以及服务器端JAVA代码都做好了,大家可以前后串起来理解一下,下一篇文章我们就开始进行页面设计。

ExtJS+DWR+Spring+Hibernate开发HRMS(3)相关推荐

  1. ssh(struts,spring,hibernate)开发的初步集成01--依赖

    一.概念 SSH 通常指的是 Struts2 做前端控制器,spring 管理各层的组件,hibernate 负责持久化层. SSM 则指的是 SpringMVC 做前端控制器,Spring 管理各层 ...

  2. eclipse 环境下整合 struts2+spring+hibernate 开发web应用常见问题及解答

    索引 1. org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned b ...

  3. Eclipse下搭建struts、spring、hibernate开发环境

    开发环境的搭建,Myeclipse 下搭建 struts+spring+hibernate 开发环境:记得刚学struts.Spring.hibernate三大框架时就为这些基本环境的搭建发愁,那是使 ...

  4. Spring MVC+Spring +Hibernate配置事务,但是事务不起作用

    最近做项目,被一个问题烦恼了很久.使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据.怀疑是配 ...

  5. eclipse 达梦 连接_达梦Hibernate Spring集成开发示例

    [IT168 技术文档]DM是武汉华工达梦数据库有限公司推出的新一代高性能.高安全性的数据库产品.它具有开放的.可扩展的体系结构,高性能事务处理能力,以及低廉的维护成本.DM是完全自主开发的数据库软件 ...

  6. 【struts2+spring+hibernate】ssh框架整合开发

    SSH框架整合 1 Struts2+Spring+Hibernate导包 Struts2导入jar包: * struts2/apps/struts2-blank.war/WEB-INF/lib/*.j ...

  7. java高级框架应用开发案例教程_Java高级框架应用开发案例教程:struts2+spring+hibernate PDF...

    资源名称:Java高级框架应用开发案例教程:struts2+spring+hibernate PDF 第1章 struts+spring+hibernate概述 1.1 框架概述 1.2 struts ...

  8. 基于SSH(Spring+Struts2+Hibernate)开发健身俱乐部会员管理系统

    你知道的越多,你不知道的越多 点赞再看,养成习惯 如果您有疑问或者见解,或者需要毕业设计项目,大作业指导,购买付费源码等,欢迎指教: 企鹅:869192208 文章目录 一.开发背景 二. 需求分析 ...

  9. 【Java EE (Struts2 + Spring + Hibernate)开发】:Struts2(二)之【拦截器机制】

    [Java EE (Struts2 + Spring + Hibernate)开发]:Struts2(二)之[拦截器机制] 本文地址:http://blog.csdn.net/shanglianlm/ ...

最新文章

  1. MATLAB_7-彩色图像【长长的笔记!】车牌放在文章末尾有链接
  2. 超越百度的口罩检测算法
  3. 消息驱动 微服务器,消息驱动的微服务-Spring Cloud Stream整合RocketMQ
  4. c语言用if如何删除末尾空格,新人提问:如何将输出时每行最后一个空格删除
  5. ARMV8 datasheet学习笔记3:AArch64应用级体系结构之Memory order
  6. Gerrit搭建与代码下载
  7. 微信小游戏开发(11)-文件系统
  8. 时间插件--做到前几个月和后个几月的设置
  9. Ambari——大数据平台的搭建利器之进阶篇[配置spark]
  10. Javascript 中的map/reduce
  11. 微博上看到的,小白兔系列
  12. win7系统备份还原软件_傲梅轻松备份bug导致系统还原0x81000203错误,
  13. 基于SSM的大学生创业众筹平台网站毕业设计源码212000
  14. ios android与wp,在iOS与Android间选择WP
  15. js点击获取—通过JS获取图片的相对坐标位置
  16. 国内与国外CRM系统相比有哪些优劣势?
  17. 【20200401程序设计思维与实践 Week7作业】
  18. Memcached单键超1M数据量的拆分设计及测试
  19. 面向广义的rl代理商
  20. adb复制root到手机,怎样通过adb命令来root手机

热门文章

  1. 渐变颜色Qt学习:QPainter之渐变填充
  2. 推荐一款强大的SQL Internal 查看工具InternalsViewer
  3. 03MyBatis动态sql
  4. 03. 数组中重复的数字
  5. vb红绿灯自动切换_VB红绿灯程序.doc
  6. MySQL 性能优化--QueryCache的原理
  7. L2-007. 家庭房产
  8. 巾帼不让须眉——女生做运维,一样可以很好
  9. [跟我学中小企业架构部署]之八:备份服务器部署
  10. 4.4 一个完整的Google Maps应用