struts2+spring+hibernate整合步骤.docx
《struts2+spring+hibernate整合步骤.docx》由会员分享,可在线阅读,更多相关《struts2+spring+hibernate整合步骤.docx(13页珍藏版)》请在冰豆网上搜索。
![struts2+spring+hibernate整合步骤.docx](https://file1.bdocx.com/fileroot1/2022-10/27/c4c1df5f-23dd-4ab7-aa1f-c1c1b32634ae/c4c1df5f-23dd-4ab7-aa1f-c1c1b32634ae1.gif)
struts2+spring+hibernate整合步骤
引用
struts2、hibernate、spring所需jar包
struts-core-2.x.x.jar----struts核心包
xwork-core-2.x.x.jar-----身体ruts在其撒很难过构建
ognl-2.6.x.jar----对象导航语言
freemarker-2.3.x.jar------struts2的ui标签的模板使用
commons-fileupload-1.2.x.jar----文件上传组件2.1.6版本后需加入此文件
struts-spring-plugin-2.x.x.jar---用于struts2继承spring的插件
hibernate核心安装包下的(下载路径:
http:
//www.hibernate.org/,点击HibernateCore右边的download)
hibernate2.jar
lib\bytecode\hibernate-cglib-repack-2.1_3.jar
lib\required\*.jar
hibernate安装包下的(下载路径:
http:
//www.hibernate.org/;点击HibernateAnnotations右边的下载)
hibernate-annotations.jar
lib\ejb3-persistence.jar、hibernate-commons-annotations.jar
hibernate针对JPA的实现包(下载路径:
http:
//www.hibernate.org/,点击HibernateEntitymanager右边下载)
hibernate-entitymanager.jar
lib\test\log4j.jar、slf4j-log4j12.jar
spring安装包下的
dist\spring.jar
lib\c3p0\c3p0-0.9.1.2.jar
lib\aspecti\aspectjweaver.jar
aspectjrt.jar
lib\colib\cglib-nodep-2.1_3.jar
lib\j2ee\common-annotations.jar
vlib\log4j\log4j-1.2.15.jar
lib\jakarta-commons\commons_loggin.jar
数据库驱动包
引用
创建mysql数据库ssh设置编码为utf-8语句:
createdatabasesshcharacterset'utf8'collate'utf8_general_ci'
引用
1.先整合spring和hibernate
*将spring和hibernate的jar包放入lib下;
*创建spring的beans.xml配置文件
Java代码
1.
xmlversion="1.0"encoding="UTF-8"?
>
2.//www.springframework.org/schema/beans"
3.xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"xmlns:
context="http:
//www.springframework.org/schema/context"
4.xmlns:
aop="http:
//www.springframework.org/schema/aop"xmlns:
tx="http:
//www.springframework.org/schema/tx"
5.xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
6.http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
7.http:
//www.springframework.org/schema/context
8.http:
//www.springframework.org/schema/context/spring-context-2.5.xsd
9.http:
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
10.http:
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd">
11.
12.
--将bean交由spring管理可以用和扫描加注-->
13.
--
14.扫描该包及该包下的子包
15.-->
16.component-scanbase-package="com.yss">
component-scan>
17.
18.
19.
--集成hibernatesessionFactory单例模式线程安全创建耗内存-->
20.
--将hibernate的事务也交由spring管理-->
21.22.destroy-method="close">
23.
24.25.value="jdbc:
mysql:
//localhost:
3306/ssh?
useUnicode=true&characterEncoding=UTF-8"/>
26.
27.
28.
--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。
Default:
3-->
29.
30.
--连接池中保留的最小连接数。
-->
31.
32.
--连接池中保留的最大连接数。
Default:
15-->
33.
34.
--最大空闲时间,60秒内未使用则连接被丢弃。
若为0则永不丢弃。
Default:
0-->
35.
36.
--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。
Default:
3-->
37.
38.
--每60秒检查所有连接池中的空闲连接。
Default:
0-->
39.
40.
41.
42.43.class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
44.
45.--放置hibernate的配置文件-->
46.
47.com/yss/bean/Employee.hbm.xml
48.
49.
50.
51.
52.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
53.hibernate.hbm2ddl.auto=update
54.hibernate.show_sql=true
55.hibernate.format_sql=false
56.
57.
58.
59.
60.
--hibernate事务管理器配置-->
61.
62.
63.
64.
65.
--spring可以用xml和注解来配置事务声明-->
66.annotation-driventransaction-manager="transactionManager"/>
67.
*配置hibernate的model.hbm.xml和创建model类
*创建service
service接口:
Java代码
1.publicinterfaceEmployeeService{
2.publicbooleansave(Employeeemployee);
3.publicbooleanupdate(Employeeemployee);
4.publicEmployeefind(Stringusername);
5.publicbooleandelete(String...username);//表示可变参数
6.publicListfindAll();
7.}
service实现类:
Java代码
1.importjava.util.List;
2.
3.importjavax.annotation.Resource;
4.
5.importorg.apache.log4j.Logger;
6.importorg.hibernate.SessionFactory;
7.importorg.springframework.stereotype.Service;
8.importorg.springframework.transaction.annotation.Prop