5,创建包和文件
数据库创建脚本:
DROP DATABASE IF EXISTS `test`;
CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET gbk */;
USE `test`;
CREATE TABLE `employee` (
`EmployeeId` varchar(32) NOT NULL,
`EmployeeName` varchar(32) NOT NULL,
PRIMARY KEY (`EmployeeId`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
org.springframework.web.context.ContextLoaderListener
6,将数据库对应表映射增加到hibernate映射中
package pojo;
public class Employee implements java.io.Serializable {
private static final long serialVersionUID = 7430294674934594308L;
private String employeeId;
private String employeeName;
public Employee() {
}
public Employee(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeId() {
return this.employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return this.employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}
xml version="1.0" encoding="utf-8"?>
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="pojo.Employee" table="employee" catalog="test">
<id name="employeeId" type="java.lang.String">
<column name="EmployeeId" length="32" />
<generator class="uuid.hex" />
id>
<property name="employeeName" type="java.lang.String">
<column name="EmployeeName" length="32" not-null="true" />
property>
class>
hibernate-mapping>
7,编写action和dao
package dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import pojo.Employee;
public class AddEmployeeDao {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void insert(Employee emp){
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
session.save(emp);
tx.commit();
session.close();
}
}
package action;
import pojo.Employee;
import com.opensymphony.xwork2.ActionSupport;
import dao.AddEmployeeDao;
public class AddAction extends ActionSupport {
private static final long serialVersionUID = 7922979648150320921L;
private Employee employee;
public void setEmployee(Employee employee) {
this.employee = employee;
}
private AddEmployeeDao dao;
@Override
public String execute() throws Exception {
dao.insert(employee);
return SUCCESS;
}
public void setDao(AddEmployeeDao dao) {
this.dao = dao;
}
}
在Webroot/WEB-INF下创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="mydataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="mydataSource"/>
<property name="mappingResources">
<list>
<value>pojo/Employee.hbm.xml value>
list>
property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
prop>
<prop key="hibernate.show_sql">true prop>
props>
property>
bean>
<bean id="addEmployeeDao" class="dao.AddEmployeeDao">
<property name="sessionFactory" ref="sessionFactory"/>
bean>
<bean id="employee" class="pojo.Employee"/>
<bean id="addAction" class="action.AddAction">
<property name="employee" ref="employee"/>
<property name="dao" ref="addEmployeeDao"/>
bean>
beans>
7,删除hibernate.cfg.xml
8,在src目录下创建struts.xml
xml version="1.0" encoding="UTF-8" ?>
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"/>
<include file="config/ch01.xml" />
struts>
9,在config目录下创建ch01.xml
xml version="1.0" encoding="UTF-8"?>
DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="ch01" extends="struts-default">
<action name="login" class="addAction">
<result name="success">/success.jsp result>
<result name="error">/error.jsp result>
<result name="input">/index.jsp result>
action>
package>
struts>
10,index.jsp的主要内容:
<s:form action="login" method="post">
<s:textfield name="employee.employeeName"/>
<s:submit value="添加员工"/>
s:form>
Log4j配置
log4j.rootLogger=WARN,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
项目最终结果: