1.8、数据库MYSQL5.1以上
Struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
Struts2
/*
index.jsp
false
emp-list
/emp-{1}.jsp
/success.jsp
package com.splend.crud.entity;
public class Employee {
private Integer employeeId;
private String firstName;
private String lastName;
private String email;
public Employee() {
}
public Employee(Integer employeeId, String firstName, String lastName, String email) {
super();
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
splend
1230
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/sh
org.hibernate.dialect.MySQL5InnoDBDialect
true
true
update
2
true
10
5
2
2000
2000
10
100
30
package com.splend.crud.dao;
import java.util.List;
public interface BaseDao {
T get(T entity);
/**
* 获取全部列表
*
* @return 所有T对象
*/
List getAll();
/**
* 持久化实例
* @param entity
* @return 成功返回true,失败返回false
*/
boolean save(T entity);
/**
* 更新一个T实例
* @param entity
* @return 更新成功返回True否则false
*/
boolean update(T entity);
/**
* 删除T实例
* @param entity
* @return 成功返回true,失败false
*/
boolean delete(T entity);
}
package com.splend.crud.dao;
import com.splend.crud.entity.Employee;
public interface EmployeeDao extends BaseDao {
}
package com.splend.crud.dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import com.splend.crud.entity.Employee;
public class EmployeeDaoImpl implements EmployeeDao {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
public EmployeeDaoImpl() {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
try {
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
session = sessionFactory.openSession();
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
e.printStackTrace();
System.out.println("EmployeeDaoImpl error");
}finally {
if (session != null) {
session.close();
}
}
}
@Override
public Employee get(Employee entity) {
Employee employee = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
employee = session.find(Employee.class, entity.getEmployeeId());
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
employee = null;
e.printStackTrace();
System.out.println("public Employee get(Employee entity) error");
}finally {
if (session != null) {
session.close();
}
}
return employee;
}
@SuppressWarnings("unchecked")
@Override
public List getAll() {
List employees = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
employees = session.createQuery("FROM Employee").list();
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
employees = null;
e.printStackTrace();
System.out.println("public List getAll error");
}finally {
if (session != null) {
session.close();
}
}
return employees;
}
@Override
public boolean save(Employee entity) {
boolean result = false;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.save(entity);
transaction.commit();
result = true;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
result = false;
e.printStackTrace();
System.out.println("public boolean save(Employee entity) error");
}finally {
if (session != null) {
session.close();
}
}
return result;
}
@Override
public boolean update(Employee entity) {
boolean result = false;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.saveOrUpdate(entity);
transaction.commit();
result = true;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
result = false;
e.printStackTrace();
System.out.println("public boolean update(Employee entity) error");
}finally {
if (session != null) {
session.close();
}
}
return result;
}
@Override
public boolean delete(Employee entity) {
boolean result = false;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
result = true;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
result = false;
e.printStackTrace();
System.out.println("public boolean delete(Employee entity) error");
}finally {
if (session != null) {
session.close();
}
}
return result;
}
}
package com.splend.crud.service;
import java.util.List;
import com.splend.crud.entity.Employee;
public interface EmployeeService {
Employee get(Employee employee);
List getEmployees();
boolean save(Employee employee);
boolean update(Employee employee);
boolean delete(Employee employee);
}
package com.splend.crud.service;
import java.util.List;
import com.opensymphony.xwork2.inject.Inject;
import com.splend.crud.dao.EmployeeDao;
import com.splend.crud.entity.Employee;
public class EmployeeServiceImpl implements EmployeeService {
@Inject(value="employeeDao",required=true)
private EmployeeDao employeeDao;
@Override
public Employee get(Employee employee) {
return employeeDao.get(employee);
}
@Override
public List getEmployees() {
return employeeDao.getAll();
}
@Override
public boolean save(Employee employee) {
return employeeDao.save(employee);
}
@Override
public boolean update(Employee employee) {
return employeeDao.update(employee);
}
@Override
public boolean delete(Employee employee) {
return employeeDao.delete(employee);
}
}
package com.splend.crud.handler;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.inject.Inject;
import com.splend.crud.entity.Employee;
import com.splend.crud.service.EmployeeService;
public class EmployeeAction implements RequestAware, ModelDriven, Preparable {
@Inject(value = "employeeService", required = true)
private EmployeeService employeService;
private Employee employee;
public String edit() {
return "edit";
}
public void prepareEdit() {
Employee emp = new Employee();
emp.setEmployeeId(employeeId);
employee = employeService.get(emp);
}
public String update() {
employeService.update(employee);
return "success";
}
public void prepareUpdate() {
employee = new Employee();
}
public String save() {
employeService.save(employee);
return "success";
}
public void prepareSave() {
employee = new Employee();
}
public String delete() {
Employee emp = new Employee();
emp.setEmployeeId(employeeId);
employeService.delete(emp);
return "success";
}
public String list() {
requset.put("emps", employeService.getEmployees());
return "list";
}
private Map requset;
private Integer employeeId;
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
/**
* 获取Request对象的方式,这里选择实现RequestAware接口。
*/
@Override
public void setRequest(Map request) {
this.requset = request;
}
/**
* 使用ModelDriven把employee对象放到值栈栈顶。
*/
@Override
public Employee getModel() {
return employee;
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
Insert title here
ID
FirstName
LastName
Email
Edit
Delete
${employeeId }
${firstName }
${lastName }
${email }
Edit
Delete
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
Insert title here
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
List All Employees