1.idea建立項目:很方便,在下面添加各自的jar包,注意點:hibernate和struts2都有一個javassiste的jar包,把低級的刪掉便可,由於有兩個的時候,會有轉換異常html
idea會幫你配置好基本的信息。java
jar包一覽:mysql
2.首先,在大佬web.xml配置好加載各小弟的配置信息:idea幫忙生成的web
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<web-app xmlns=
"http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation=
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version=
"3.1"
>
<!--讀取spring配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:applicationContext-beans.xml</param-value>
</context-param>
<!--struts2配置-->
<filter>
<filter-name>struts2</filter-name>
<filter-
class
>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-
class
>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--listener-->
<listener>
<listener-
class
>org.springframework.web.context.ContextLoaderListener</listener-
class
>
</listener>
</web-app>
|
3.把hibernate搞到spring的容器中去......(已經寫過這一方面的內容,就不詳細了,直接貼代碼 )spring
applicationContext.xml這個文件是配置hibernate與spring的整合信息的sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
>
<context:property-placeholder location=
"classpath:db.properties"
/>
<bean id=
"dataSource"
class
=
"com.mchange.v2.c3p0.ComboPooledDataSource"
>
<property name=
"user"
value=
"${user}"
/>
<property name=
"password"
value=
"${password}"
/>
<property name=
"driverClass"
value=
"${driveClass}"
/>
<property name=
"jdbcUrl"
value=
"${url}"
/>
</bean>
<bean id=
"sessionFactory"
class
=
"org.springframework.orm.hibernate5.LocalSessionFactoryBean"
>
<property name=
"dataSource"
ref=
"dataSource"
/>
<property name=
"mappingLocations"
value=
"classpath:*.hbm.xml"
/>
<property name=
"hibernateProperties"
>
<props>
<prop key=
"hibernate.format_sql"
>
true
</prop>
<prop key=
"hibernate.show_sql"
>
true
</prop>
<prop key=
"hibernate.hbm2ddl.auto"
>update</prop>
<prop key=
"hibernate.dialect"
>org.hibernate.dialect.MySQL5InnoDBDialect</prop>
</props>
</property>
</bean>
<bean id=
"transactionManager"
class
=
"org.springframework.orm.hibernate5.HibernateTransactionManager"
>
<property name=
"sessionFactory"
ref=
"sessionFactory"
/>
</bean>
<aop:config>
<aop:pointcut id=
"pointCut"
expression=
"execution(* com.service.*.*(..))"
/>
<aop:advisor advice-ref=
"txAdvice"
pointcut-ref=
"pointCut"
/>
</aop:config>
<tx:advice id=
"txAdvice"
transaction-manager=
"transactionManager"
>
<tx:attributes>
<tx:method name=
"get*"
read-only=
"true"
/>
<tx:method name=
"*"
/>
</tx:attributes>
</tx:advice>
</beans>
|
db.propertiesexpress
1
2
3
4
|
user=root
password=
driveClass=com.mysql.jdbc.Driver
url=jdbc:mysql:
//localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8
|
爲完善上面這個配置文件,須要生產bean類和mapping映射文件:apache
Department.java瀏覽器
Employee.javasession
Department.hbm.xml
Employee.hbm.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package
com.entities;
import
javax.persistence.Basic;
import
javax.persistence.Column;
import
javax.persistence.Entity;
import
javax.persistence.Id;
@Entity
public
class
Department {
private
int
id;
private
String
departmentname;
@Id
@Column(name =
" ID"
, nullable =
false
)
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
@Basic
@Column(name =
"DEPARTMENTNAME"
, nullable =
true
, length =
255
)
public
String
getDepartmentname() {
return
departmentname;
}
public
void
setDepartmentname(
String
departmentname) {
this
.departmentname = departmentname;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package
com.entities;
import
javax.persistence.*;
import
java.util.
Date
;
@Entity
public
class
Employee {
private
int
id;
private
String
lastname;
private
String
email;
private
Date
birth;
private
Date
createtime;
private
Department department;
@Id
@Column(name =
"ID"
, nullable =
false
)
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id = id;
}
@Basic
@Column(name =
"LASTNAME"
, nullable =
true
, length =
255
)
public
String
getLastname() {
return
lastname;
}
public
void
setLastname(
String
lastname) {
this
.lastname = lastname;
}
@Basic
@Column(name =
"EMAIL"
, nullable =
true
, length =
255
)
public
String
getEmail() {
return
email;
}
public
void
setEmail(
String
email) {
this
.email = email;
}
@Basic
@Column(name =
"BIRTH"
, nullable =
true
)
public
Date
getBirth() {
return
birth;
}
public
void
setBirth(
Date
birth) {
this
.birth = birth;
}
@Basic
@Column(name =
"CREATETIME"
, nullable =
true
)
public
Date
getCreatetime() {
return
createtime;
}
public
void
setCreatetime(
Date
createtime) {
this
.createtime = createtime;
}
@Basic
@Column(name =
"DEPARTMENT_ID"
, nullable =
true
)
public
Department getDepartment() {
return
department;
}
public
void
setDepartment(Department department) {
this
.department = department;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?xml version=
'1.0'
encoding=
'utf-8'
?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping>
<
class
name=
"com.entities.Department"
table=
"department"
schema=
"ssh"
>
<id name=
"id"
>
<column name=
" ID"
sql-type=
"int(11)"
/>
<generator
class
=
"native"
/>
</id>
<property name=
"departmentname"
>
<column name=
"DEPARTMENTNAME"
sql-type=
"varchar(255)"
not-
null
=
"true"
/>
</property>
</
class
>
</hibernate-mapping>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?xml version=
'1.0'
encoding=
'utf-8'
?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping>
<
class
name=
"com.entities.Employee"
table=
"employee"
schema=
"ssh"
>
<id name=
"id"
>
<column name=
"ID"
sql-type=
"int(11)"
/>
<generator
class
=
"native"
/>
</id>
<property name=
"lastname"
>
<column name=
"LASTNAME"
sql-type=
"varchar(255)"
not-
null
=
"true"
/>
</property>
<property name=
"email"
>
<column name=
"EMAIL"
sql-type=
"varchar(255)"
not-
null
=
"true"
/>
</property>
<property name=
"birth"
>
<column name=
"BIRTH"
sql-type=
"date"
not-
null
=
"true"
/>
</property>
<property name=
"createtime"
>
<column name=
"CREATETIME"
sql-type=
"date"
not-
null
=
"true"
/>
</property>
<many-to-one name=
"department"
class
=
"com.entities.Department"
>
<column name=
"DEPARTMENT_ID"
/>
</many-to-one>
</
class
>
</hibernate-mapping>
|
至此:打開Tomcat來運行,若是各方面配置沒問題,就能夠自動生成數據表了。
3.開始操做數據:寫一個業務獲取數據(數據由本身填充 )
EmployeeDao.java
這裏的sessionfactory經過set的方法獲取..... set方法把該類注入到bean管理器,得到sessionfactory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package
com.service;
import
com.entities.Employee;
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
java.util.List;
public
class
EmployeeDao {
private
SessionFactory sessionFactory;
public
void
setSessionFactory(SessionFactory sessionFactory) {
this
.sessionFactory = sessionFactory;
}
public
Session getSession() {
return
sessionFactory.getCurrentSession();
}
public
List<Employee> getAll() {
// 左向外聯獲取department信息,fetch獲取單一信息,不用獲取一大堆
String
hql =
"from Employee e left outer join fetch e.department"
;
return
getSession().createQuery(hql).list();
}
}
|
再寫一個service把上面的業務結果中轉(本身形象地 理解 )
EmployeeService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
com.service;
import
com.entities.Employee;
import
java.util.List;
public
class
EmployeeService {
private
EmployeeDao employeeDao;
public
void
setEmployeeDao(EmployeeDao employeeDao) {
this
.employeeDao = employeeDao;
}
public
List<Employee> getAll() {
return
employeeDao.getAll();
}
}
|
4.到了這裏,就須要寫struts2的東西了,首先 在web的開始頁面寫一個action超連接
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page contentType=
"text/html;charset=UTF-8"
language=
"java"
%>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href=
"emp-list"
>List all Employees</a>
</body>
</html>
|
實現這個效果,點擊後就出現查詢結果:
接着須要實現這個挑戰的action,須要在struts.xml中註冊這個action的信息,這裏用通配符共用這個action
struts.xml
這裏的employeeAction就是spring中已經注入的bean的id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
<struts>
<!--打開開發者模式,若有bug會顯示之類,不過也消耗性能-->
<constant name=
"struts.devMode"
value=
"true"
/>
<!--關閉動態方法調用,這樣就不能在瀏覽器經過調用類方法來調用action了-->
<constant name=
"struts.enable.DynamicMethodInvocation"
value=
"false"
/>
<!--引用默認配置-->
<
package
name=
"default"
extends
=
"struts-default"
>
<!--通配符表示多種相似的方法共用一個模塊,這裏就是數據交互出,本身的理解-->
<action name=
"emp-*"
class
=
"employeeAction"
method=
"{1}"
>
<result name=
"list"
>/WEB-INF/pages/emp-list.jsp</result>
</action>
</
package
>
</struts>
|
再寫一個關於處理與struts相關bean的spring容器文件,實際上是一個樣的
applicationContext-beans.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<beans xmlns=
"http://www.springframework.org/schema/beans"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
<!--經過
set
方法把emplayDao注入到bean中,在EmployeeDao使用到sessionFactory-->
<bean id=
"employeeDao"
class
=
"com.service.EmployeeDao"
>
<property name=
"sessionFactory"
ref=
"sessionFactory"
/>
</bean>
<!--同上-->
<bean id=
"employeeService"
class
=
"com.service.EmployeeService"
>
<property name=
"employeeDao"
ref=
"employeeDao"
/>
</bean>
<!--配置action,這裏在struts.xml的action會與此綁定,進行數據交互,scope=
"prototype"
確保每一個請求都是一個新的action,不能使用單例,那樣的話,就會出現信息錯亂-->
<bean id=
"employeeAction"
class
=
"com.actions.EmployeeAction"
scope=
"prototype"
>
<property name=
"employeeService"
ref=
"employeeService"
/>
</bean>
</beans>
|
最後,就須要寫數據的交匯點,EmployeeAction.java了
這裏就把EmployeeService.java查詢得來的數據寫入到值棧中,一邊在jsp中讀取而且顯示..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package
com.actions;
import
com.opensymphony.xwork2.ActionSupport;
import
com.service.EmployeeService;
import
org.apache.struts2.interceptor.RequestAware;
import
java.util.Map;
public
class
EmployeeAction
extends
ActionSupport
implements
RequestAware {
private
EmployeeService employeeService;
public
void
setEmployeeService(EmployeeService employeeService) {
this
.employeeService = employeeService;
}
public
String
list() {
request.put(
"employee"
, employeeService.getAll());
return
"list"
;
}
private
Map<
String
,
Object
> request;
@Override
public
void
setRequest(Map<
String
,
Object
> map) {
this
.request = map;
}
}
|
最後完善顯示結果的jsp文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<%@ page contentType=
"text/html;charset=UTF-8"
language=
"java"
%>
<%@taglib prefix=
"s"
uri=
"/struts-tags"
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<h4>Employee List Page</h4>
<s:
if
test=
"#request.employee==null||request.employee.size()==0"
>
沒有任何員工信息
</s:
if
>
<s:
else
>
<table border=
"1"
cellpadding=
"2"
cellspacing=
"0"
>
<tr>
<td>ID</td>
<td>LAST_NAME</td>
<td>E_MAIL</td>
<td>BIRTH</td>
<td>CREATE_TIME</td>
<td>DEPARTMENT</td>
</tr>
<s:iterator value=
"#request.employee"
>
<tr>
<td>${id}</td>
<td>${lastname}</td>
<td>${email}</td>
<td>${birth}</td>
<td>${createtime}</td>
<td>${department.departmentname}</td>
</tr>
</s:iterator>
</table>
</s:
else
>
</body>
</html>
|
6.文件一覽:
運行結果: