org.hibernate.dialect.OracleDialect
oracle.jdbc.driver.OracleDriver
mine
mine
jdbc:oracle:thin:@127.0.0.1:1521:orcl
true
update
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:tx=“http://www.springframework.org/schema/tx”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:property-placeholder location=“classpath:db.properties” ignore-unresolvable=“true”/>
<bean id=“sessionFactory”
class=“org.springframework.orm.hibernate5.LocalSessionFactoryBean”>
com/ssh/entities/Department.hbm.xml
com/ssh/entities/Employee.hbm.xml
hibernate.cfg.xml
org.hibernate.dialect.OracleDialect
update
true
true
<tx:annotation-driven transaction-manager=“transactionManager”/>
applicationContext-beans.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> false/WEB-INF/views/emp-list.jsp
text/html inputStream/WEB-INF/views/emp-input.jsp
/emp-list
3、类文件
EmployeeAction
package com.ssh.actions;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.ssh.entities.Employee;
import com.ssh.service.DepartmentService;
import com.ssh.service.EmployeeService;
public class EmployeeAction extends ActionSupport implements RequestAware,
ModelDriven,Preparable{
private static final long serialVersionUID = 1L;
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
private DepartmentService departmentService;
public void setDepartmentService(Depart 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 mentService departmentService) {
this.departmentService = departmentService;
}
public String list() {
request.put(“employees”,employeeService.getAll());
return “list”;
}
private Integer id;
public void setId(Integer id) {
this.id = id;
}
public InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String delete() {
try {
employeeService.delete(id);
inputStream = new ByteArrayInputStream(“1”.getBytes(“UTF-8”));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return SUCCESS;
}
public String input() {
request.put(“departments”, departmentService.getAll());
return INPUT;
}
public void prepareInput() {
if(id!=null) {
model = employeeService.get(id);
}
}
public String save() {
if(id == null) {
model.setCreateTime(new Date());
}
employeeService.saveOrUpdate(model);
System.out.println(“model”);
return SUCCESS;
}
public void prepareSave() {
if(id == null) {
model = new Employee();
}else {
model = employeeService.get(id);
}
}
private Map<String,Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
@Override
public void prepare() throws Exception {}
private Employee model;
@Override
public Employee getModel() {
return model;
}
}
EmployeeService
package com.ssh.service;
import java.util.List;
import com.ssh.dao.EmployeeDao;
import com.ssh.entities.Employee;
public class EmployeeService {
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
public void saveOrUpdate(Employee employee) {
employeeDao.saveOrUpdate(employee);
}
public void delete(Integer id) {
employeeDao.delete(id);
}
public List getAll(){
List employees = employeeDao.getAll();
return employees;
}
public Employee get(Integer id) {
return employeeDao.get(id);
}
}
EmployeeDao
package com.ssh.dao;
import java.util.List;
import com.ssh.entities.Employee;
public class EmployeeDao extends BaseDao{
public void delete(Integer id) {
String hql = “DELETE From Employee e where e.id=?0”;
getSession().createQuery(hql).setParameter(0,id).executeUpdate();
}
public List getAll() {
//String hql = “From Employee e LEFT OUTER JOIN FETCH e.department”;
String hql = “From Employee”;
return getSession().createQuery(hql).list();
}
public void saveOrUpdate(Employee employee) {
getSession().saveOrUpdate(employee);
}
public Employee get(Integer id) {
return (Employee)getSession().get(Employee.class,id);
}
}
emp-list.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“s” uri=“/struts-tags”%>
Employee List Page
<s:if test=“#request.employees == null||#request.employees.size() == 0”>
没有任何员工信息
</s:if>
<s:else>
<s:iterator value=“#request.employees”>
${id } ${lastName } ${email } ${birth } ${createTime } Edit</s:iterator>
</s:else>
emp-input.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@ taglib prefix=“s” uri=“/struts-tags” %>
Employee Input Page
<s:form action=“emp-save” method=“post”>
<s:if test=“id != null”>
<s:textfield name=“lastName” label=“LastName” disabled=“true”></s:textfield>
<s:hidden name=“id”></s:hidden>
</s:if>
<s:else>