1、首先从整合Hibernate开始
2、导入包,这里就把所有的报导进来,包括struts2的,一起导进来
一共29个包
3、编写实体类
package
star.july.entity;
public
class
Student {
private
int
id
;
private
String
name
;
private
String
gender
;
public
int
getId() {
return
id
;
}
public
void
setId(
int
id) {
this
.
id
= id;
}
public
String getName() {
return
name
;
}
public
void
setName(String name) {
this
.
name
= name;
}
public
String getGender() {
return
gender
;
}
public
void
setGender(String gender) {
this
.
gender
= gender;
}
@Override
public
String toString() {
return
"Student [id="
+
id
+
", name="
+
name
+
", gender="
+
gender
+
"]"
;
}
}
4、创建实体类的映射文件,Student.hbm.xml
<?
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
=
"star.july.entity.Student"
table
=
"student"
>
<
id
name
=
"id"
>
<
generator
class
=
"native"
></
generator
>
</
id
>
<
property
name
=
"name"
column
=
"sname"
></
property
>
<
property
name
=
"gender"
column
=
"sgender"
></
property
>
</
class
>
</
hibernate-mapping
>
5、编写dao,基本完成Hibernate的工作
package
star
.
july
.
dao
;
import
java
.
util
.
List
;
import
org
.
springframework
.
orm
.
hibernate4
.
HibernateTemplate
;
import
star
.
july
.
entity
.
Student
;
public
class
StudentDaoImpl
implements
IStudentDao
{
//
接收
hibernateTemplate
对象
private
HibernateTemplate hibernateTemplate
;
public
void
setHibernateTemplate
(
HibernateTemplate hibernateTemplate
)
{
this
.
hibernateTemplate
=
hibernateTemplate
;
}
//
查询所有的学生
public
List
<
Student
>
queryAll
()
{
return
hibernateTemplate
.
loadAll
(
Student
.
class
);
}
}
6、创建applicationContext.xml,用于连接数据库,相当于hibernate.cfg.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"
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"
>
<
context:property-placeholder
location
=
"classpath:db.properties"
></
context:property-placeholder
>
<!-- 创建连接池对象 -->
<
bean
id
=
"dataSourceID"
class
=
"com.mchange.v2.c3p0.ComboPooledDataSource"
>
<!-- 注入参数 -->
<
property
name
=
"jdbcUrl"
value
=
"${jdbcUrl}"
></
property
>
<
property
name
=
"driverClass"
value
=
"${driverClass}"
></
property
>
<
property
name
=
"user"
value
=
"${user}"
></
property
>
<
property
name
=
"password"
value
=
"${password}"
></
property
>
</
bean
>
<!-- 创建SessionFactory -->
<
bean
id
=
"sessionFactoryID"
class
=
"org.springframework.orm.hibernate4.LocalSessionFactoryBean"
>
<!-- 注入连接池 -->
<
property
name
=
"dataSource"
ref
=
"dataSourceID"
></
property
>
<!-- 注入hibernate环境配置:方言,是否显示sql,是否维护表 -->
<
property
name
=
"hibernateProperties"
>
<
props
>
<
prop
key
=
"hibernate.dialect"
>
org.hibernate.dialect.MySQL5InnoDBDialect
</
prop
>
<
prop
key
=
"hibernate.show_sql"
>
true
</
prop
>
<
prop
key
=
"hibernate.hbm2ddl.auto"
>
update
</
prop
>
</
props
>
</
property
>
<!-- 注入映射文件的路径 -->
<
property
name
=
"mappingLocations"
>
<
array
>
<
value
>
classpath:star/july/entity/Student.hbm.xml
</
value
>
</
array
>
</
property
>
</
bean
>
<!-- 创建HibernateTemplate对象 -->
<
bean
name
=
"hibernateTemplateID"
class
=
"org.springframework.orm.hibernate4.HibernateTemplate"
>
<!-- 注入工厂 -->
<
property
name
=
"sessionFactory"
ref
=
"sessionFactoryID"
></
property
>
</
bean
>
<!-- 创建dao对象 -->
<
bean
name
=
"studentDaoID"
class
=
"star.july.dao.StudentDaoImpl"
>
<!-- 注入hibernateTemplate对象 -->
<
property
name
=
"hibernateTemplate"
ref
=
"hibernateTemplateID"
></
property
>
</
bean
>
</
beans
>
其中,db.properties文件的内容如下:
jdbcUrl=
jdbc:mysql://localhost:3306/day36
driverClass=
com.mysql.jdbc.Driver
user=
root
password=
root
7、开始整合struts,导入对应的struts2包,前面已导入
8、编写service层
package
star
.
july
.
service
;
import
java
.
util
.
List
;
import
star
.
july
.
entity
.
Student
;
public
interface
IStudentService
{
public
List
<
Student
>
queryAll
();
}
StudentServiceImpl.java
package
star
.
july
.
service
;
import
java
.
util
.
List
;
import
star
.
july
.
dao
.
IStudentDao
;
import
star
.
july
.
entity
.
Student
;
public
class
StudentServiceImpl
implements
IStudentService
{
IStudentDao stuDao
;
public
void
setStudao
(
IStudentDao studao
)
{
this
.
stuDao
=
studao
;
}
public
List
<
Student
>
queryAll
()
{
System
.
out
.
println
(
"
调用了
queryAll
方法
"
);
return
stuDao
.
queryAll
();
}
}
9、创建Action
package
star
.
july
.
web
;
import
java
.
util
.
List
;
import
star
.
july
.
entity
.
Student
;
import
star
.
july
.
service
.
IStudentService
;
import
com
.
opensymphony
.
xwork2
.
ActionContext
;
import
com
.
opensymphony
.
xwork2
.
ActionSupport
;
public
class
StudentAction
extends
ActionSupport
{
private
IStudentService stuService
;
public
void
setStuService
(
IStudentService stuService
)
{
this
.
stuService
=
stuService
;
}
//
查询所有学生
public
String list
(){
List
<
Student
>
list
=
stuService
.
queryAll
();
ActionContext
.
getContext
().
put
(
"list"
,
list
);
System
.
out
.
println
(
"
调用
list
方法
"
);
return
SUCCESS
;
}
}
10、在applicationContext.xml中配置
<!-- 创建service -->
<
bean
name
=
"studentServiceID"
class
=
"star.july.service.StudentServiceImpl"
>
<!-- 注入dao -->
<
property
name
=
"studao"
ref
=
"studentDaoID"
></
property
>
</
bean
>
<!-- 创建Action -->
<
bean
name
=
"studentActionID"
class
=
"star.july.web.StudentAction"
>
<!-- 注入dao -->
<
property
name
=
"stuService"
ref
=
"studentServiceID"
></
property
>
</
bean
>
11、配置struts.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
struts
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"
>
<
struts
>
<
package
name
=
"base"
namespace
=
"/"
extends
=
"struts-default"
>
<
action
name
=
"student_*"
class
=
"studentActionID"
method
=
"{1}"
>
<
result
>
/success.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
12、在web.xml中配置全局过滤器和spring的Listener
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.5"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<
display-name
></
display-name
>
<!-- 配置全局过滤器 -->
<
filter
>
<
filter-name
>
StrutsPrepareAndExecuteFilter
</
filter-name
>
<
filter-class
>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>
StrutsPrepareAndExecuteFilter
</
filter-name
>
<
url-pattern
>
/*
</
url-pattern
>
</
filter-mapping
>
<!-- 配置启动spring的Listener -->
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<!-- 配置参数,指向文件路径 -->
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/classes/applicationContext.xml
</
param-value
>
</
context-param
>
<
welcome-file-list
>
<
welcome-file
>
index.jsp
</
welcome-file
>
</
welcome-file-list
>
</
web-app
>
13、创建JSP页面,显示数据
<%@
page
language
=
"java"
import
=
"java.util.*"
pageEncoding
=
"UTF-8"
%>
<%@
taglib
uri
=
"/struts-tags"
prefix
=
"s"
%>
<!
DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<
html
>
<
head
>
<
title
>
title
</
title
>
<
meta
http-equiv
=
"pragma"
content
=
"no-cache"
>
<
meta
http-equiv
=
"cache-control"
content
=
"no-cache"
>
<
meta
http-equiv
=
"expires"
content
=
"0"
>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</
head
>
<
body
>
<
h2
>
学生信息
</
h2
>
<
s:iterator
value
=
"#list"
var
=
"s"
>
ID:
<
s:property
value
=
"#s.id"
/>
 
姓名:
<
s:property
value
=
"#s.name"
/>
 
性别:
<
s:property
value
=
"#s.gender"
/><
br
>
</
s:iterator
>
</
body
>
</
html
>