接下来我们看看我们 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&g