最近在学习用struts+spring+hibernate来做开发,把学习过程记录一下,以便以后回顾:
整个项目搭建完成的结构如图:
下面是搭建过程:
第一步、新建一个employeeMgmt的web项目
第二步、导入spring相关jar包、hibernate相关jar包、struts相关jar包,还有整合log4j的jar包、以及数据库所用的jar包,jar包结构如图:
第三步、
(1).在src根目录下新建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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" >
</beans>
(2).在src根目录下新建struts.xml文件
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
(3).在src根目录下新建jdbc.properties属性文件,我用的是Oracle数据库,等号左边的名字可以自定义,没有固定格式。
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:IP:端口号:数据库全局名
jdbc.username=用户名
jdbc.password=密码
(4).在src根目录下新建log4j.properties日志文件
### set log levels ###
log4j.rootLogger = info , Console , D
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO
#output2file
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = D\:/logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO \#\# \u00E8\u00BE\u0093\u00E5\u0087\u00BAinfo\u00E7\u00BA\u00A7\u00E5\u0088\u00AB\u00E4\u00BB\u00A5\u00E4\u00B8\u008A\u00E7\u009A\u0084\u00E6\u0097\u00A5\u00E5\u00BF\u0097
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
第四步、配置web.xml中的spring的监听和struts的拦截器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第五步、在domain包下新建两个实体类和两个映射文件
(1)部门实体类
package com.moocer.employee.domian;
import java.util.HashSet;
import java.util.Set;
//部门的实体
public class Department {
private Integer did;
private String dname;
private String ddesc;
//部门的员工集合
private Set<Employee> employees = new HashSet<Employee>();
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getDdesc() {
return ddesc;
}
public void setDdesc(String ddesc) {
this.ddesc = ddesc;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
}
(2)员工实体类
package com.moocer.employee.domian;
import java.util.Date;
//员工的实体
public class Employee {
private Integer eid;
private String ename;
private String sex;
private Date birthday;
private Date joinDate;
private String eno;
private String username;
private String password;
//员工所属部门
private Department department;
public Integer getEid() {
return eid;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
public String getEno() {
return eno;
}
public void setEno(String eno) {
this.eno = eno;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
(3)部门映射文件Department.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5 <hibernate-mapping>
6 <class name="com.moocer.employee.domian.Department" table="t_department">
7 <id name="did" column="did">
8 <generator class="native"/>
9 </id>
10 <property name="dname" column="dname" length="20"/>
11 <property name="ddesc" column="ddesc" length="100"/>
12 <!-- 关联关系映射 一对多 -->
13 <set name="employees">
14 <key column="dno"/>
15 <one-to-many class="com.moocer.employee.domian.Employee"/>
16 </set>
17 </class>
18 </hibernate-mapping>
(4)员工映射文件Employee.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
5 <hibernate-mapping>
6 <class name="com.moocer.employee.domian.Employee" table="t_employee">
7 <id name="eid" column="eid">
8 <generator class="native"/>
9 </id>
10 <property name="ename" column="ename" length="20"/>
11 <property name="sex" column="sex" length="10"/>
12 <property name="birthday" column="birthday"/>
13 <property name="joinDate" column="joinDate"/>
14 <property name="eno" column="eno" length="20"/>
15 <property name="username" column="usrename" length="20"/>
16 <property name="password" column="password" length="20"/>
17
18 <!-- 关联关系映射 多对一 -->
19 <many-to-one name="department" class="com.moocer.employee.domian.Department" column="dno"/>
20 </class>
21 </hibernate-mapping>
引入
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
头部,这个段代码可以在hibernate3.jar的核心文件中的最后找到找到,如下图:
第六步、配置配置文件
(1)配置applicationContext.xml文件
在原有基础上加下如下代码:
1 <!-- 引入外部属性文件 -->
2 <context:property-placeholder location="classpath:jdbc.properties"/>
3
4 <!-- 配置连接池 c3po-->
5 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
6 <property name="driverClass" value="${jdbc.driverClass}"/>
7 <property name="jdbcUrl" value="${jdbc.url}"/>
8 <property name="user" value="${jdbc.username}"/>
9 <property name="password" value="${jdbc.password}"/>
10 </bean>
11
12 <!-- 配置hibernate的相关属性 -->
13 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
14 <!-- 连接池的注入 -->
15 <property name="dataSource" ref="dataSource"/>
16 <!-- 配置hibernate的属性 -->
17 <property name="hibernateProperties">
18 <props>
19 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
20 <prop key="hibernate.show_sql">true</prop>
21 <prop key="hibernate.format_sql">true</prop>
22 <prop key="hibernate.hbm2ddl.auto">update</prop>
23 </props>
24 </property>
25 <!-- 配置hibernate的映射文件 -->
26 <property name="mappingResources">
27 <list>
28 <value>com/moocer/employee/domian/Department.hbm.xml</value>
29 <value>com/moocer/employee/domian/Employee.hbm.xml</value>
30 </list>
31 </property>
32 </bean>
33
34 <!-- action交由spring管理,
35 配置action 因为spring默认是单例的,所以要加上scope=“protoype”-->
36
37 <!-- 配置业务层的类 -->
38
39 <!-- 配置DAO的类 -->
40
41 <!-- 配置事务管理 -->
42 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
43 <property name="sessionFactory" ref="sessionFactory"/>
44 </bean>
45
46 <!-- 开启注解事物 -->
47 <tx:annotation-driven transaction-manager="transactionManager"/>
(2)struts.xml中加入如下代码
1 <struts>
2 <package name="moocer" extends="struts-default" namespace="/">
3
4 </package>
5 </struts>
第七步、把工程发布到Tomcat上部署,我的是7,启动Tomcat
此时查看数据库会发现
t_department部门表和t_employee表已经建好,搭建结束。