ExtJS+DWR+Spring+Hibernate开发HRMS(3)

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

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 代码都做好了,大家可以前后串起来理解一下,下一篇文章我们就开始进行页面设计。

你可能感兴趣的:(Hibernate)