|
该用户从未签到
|
- struts2、hibernate、spring所需jar包 * l6 d: `2 \, Z# E* g( k8 n9 N# T R
-
2 f1 @* t5 Y( @# {( s$ E0 k* ` - struts-core-2.x.x.jar ----struts核心包
( z# s' T! c/ F6 ?0 l4 c - xwork-core-2.x.x.jar -----身体ruts在其撒很难过构建
2 H& G! s3 N* z# D) S) \ - ognl-2.6.x.jar ----对象导航语言
1 O4 v2 g Q2 ~/ Z - freemarker-2.3.x.jar ------struts2的ui标签的模板使用
: x s$ F+ t! F1 i D/ h1 [ - commons-fileupload-1.2.x.jar ----文件上传组件 2.1.6版本后需加入此文件 # F4 T0 u; D4 |& T1 t
- struts-spring-plugin-2.x.x.jar ---用于struts2继承spring的插件
/ J& j2 u- B& [( B. a - 5 K+ Z5 C- r8 a b0 D; Q7 H: C/ n
- hibernate核心安装包下的(下载路径:http://www.hibernate.org/ ,点击Hibernate Core 右边的download)
" J* R, P3 O1 {9 F - hibernate2.jar # T" I( p v0 Z- R- O. i+ o
- lib\bytecode\hibernate-cglib-repack-2.1_3.jar
1 Q: R, ~5 D3 F - lib\required\*.jar
$ [1 I! d6 t; c2 H: i - hibernate安装包下的(下载路径:http://www.hibernate.org/;点击Hibernate Annotations 右边的下载)
! R; m2 U4 {$ |# E - hibernate-annotations.jar
& r9 t4 H& W2 E% S4 g& h9 U; O - lib\ejb3-persistence.jar、hibernate-commons-annotations.jar 3 j8 u4 W# W" S$ x7 R7 ~
- hibernate针对JPA的实现包(下载路径:http://www.hibernate.org/ ,点击Hibernate Entitymanager右边下载)8 ~$ e3 p: B8 T$ F: h* }: U
- hibernate-entitymanager.jar 1 ~6 _/ n/ F6 i5 b: B" W; J
- lib\test\log4j.jar、 slf4j-log4j12.jar 3 }6 q( N- K% H+ v/ {
- 6 f% V* C6 u* ^( i- o# R, v
- spring安装包下的
: }( C/ r# L1 i3 ~' A) M( Y) A - dist\spring.jar ' \$ P2 U" ~2 _
- lib\c3p0\c3p0-0.9.1.2.jar
& Y2 q9 Z. c6 x - lib\aspecti\aspectjweaver.jar 5 b7 e0 F9 e- l% F8 R2 x0 J( Z0 P
- aspectjrt.jar 8 k! N8 g, A- c- p4 e- n2 H
- lib\colib\cglib-nodep-2.1_3.jar : b0 z h, O+ S$ H
- lib\j2ee\common-annotations.jar 5 ?$ u4 { P* V: n4 f* @. o
- vlib\log4j\log4j-1.2.15.jar
, d3 ^& c% F, K/ \: [# z - lib\jakarta-commons\commons_loggin.jar
! g6 @" h# O# e9 a5 Y6 F6 D - $ \; }6 J" B* [' P7 o- Z
- 数据库驱动包
1 a, S0 a6 ^& F* C - # v/ V4 o# S, u0 K- D v# I
- + V; t- ?: x" a
-
7 y# S! V! q& S5 [2 K' `' |5 f - 引用
# F2 C# l& M3 ]6 Y -
- t$ ]2 \1 n) `% }8 I - ; i, d, I( j t
- 创建mysql数据库ssh 设置编码为utf-8 语句: 5 n0 g2 _' d! h2 f
- create database ssh character set 'utf8' collate 'utf8_general_ci'$ f \! P' s1 D" J' L/ T* ^
- : ~$ k, ?. ]1 M8 C; k) b/ e
-
4 s8 b9 z' T4 I. c2 G. { - # T9 P& _5 L w; \" _" c! }
- 引用
, Q7 L$ p3 }7 r* X -
; W9 _4 K* A- j% U- E+ s -
2 U9 N! D. |9 X& H+ b - 1.先整合spring和hibernate : \. N# y1 ~0 J* N0 l
- *将spring和hibernate的jar包放入lib下;
& [ J0 V! U0 E" p( j L) f+ k. S - *创建spring的beans.xml配置文件
+ r! F6 A3 X. Z+ M% A- @ -
$ v6 @" {5 I5 m) _( I -
/ v1 H& o! l6 ^6 W -
; D$ b- }$ ~5 g0 s8 K2 m; o- B - java代码 2 s4 N( X- |# h$ t, i
- 1.<?xml version="1.0" encoding="UTF-8"?>
: v4 `9 C9 Q% J8 A - 2.<beans xmlns="http://www.springframework.org/schema/beans" - v0 ?, [, k) y \# ^! C
- 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
. A9 m. V7 W( g, G- S2 e: Z - 4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
/ y9 ^, R2 _8 o% @ - 5. xsi:schemaLocation="http://www.springframework.org/schema/beans 0 G; C" x- x# ^1 E) l7 I0 s+ Z l
- 6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
) W5 @+ L/ ~4 y: q+ w3 O - 7. http://www.springframework.org/schema/context " s$ V& e; A$ J- w# f) D0 I
- 8. http://www.springframework.org/schema/context/spring-context-2.5.xsd
9 G$ [! R2 u6 l+ A) w - 9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
$ l! Y- i, n9 b, J+ l" T7 j - 10. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
# x" `( C: F* k. Z9 W - 11. 6 _5 h6 _- o& v
- 12. <!-- 将bean交由spring管理可以 用<bean></bean>和扫描加注 -->
& e, l2 z0 D- Y7 g; Q( y - 13. <!-- " G3 [& k) N% {7 a2 [8 R+ ^
- 14. 扫描该包及该包下的子包 7 A: i8 s) G! T; |
- 15. -->
4 _. s; i: b c% e$ x# t - 16. <context:component-scan base-package="com.yss"></context:component-scan>
' [( K0 o1 b3 r& g. X) ? - 17. ) Q6 j* ^8 r5 u- c( n2 j
- 18. $ y Y% z7 Z/ N+ A' ^$ T( D& f1 D
- 19. <!-- 集成hibernate sessionFactory单例模式 线程安全 创建耗内存-->
/ p7 `/ Y- Z4 y4 u- M/ d% ] - 20. <!-- 将hibernate的事务也交由spring管理 --> 5 A- L: u( t' C" P: B
- 21. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" & @- l& _0 b* D; x
- 22. destroy-method="close"> ; H& e) z# y+ p
- 23. <property name="driverClass" value="org.gjt.mm.mysql.Driver" />
# [7 G" M; m7 ` _- k - 24. <property name="jdbcUrl" 0 p2 c& m7 n0 t( F, Z2 J
- 25. value="jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8" /> ( q8 @7 u/ ]$ N& a2 Q
- 26. <property name="user" value="root" /> $ l7 Q* ~/ y- ]. B4 T; W- a- s% ^7 f% o# ~
- 27. <property name="password" value="root" />
6 G- W# T& W) @; p/ j. J& } - 28. <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> ( c7 `: g U* e
- 29. <property name="initialPoolSize" value="1" /> : T. B: _! g( x( b* U* d' S. ?4 _
- 30. <!--连接池中保留的最小连接数。--> 5 i0 E4 C4 [) x; @
- 31. <property name="minPoolSize" value="1" /> ( r0 F4 H9 K# ]
- 32. <!--连接池中保留的最大连接数。Default: 15 -->
! E+ x( T' x3 |! O - 33. <property name="maxPoolSize" value="300" /> 6 u) ^4 e! q' ~5 y _, W
- 34. <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
5 B+ \0 G& B8 H, k' G( M/ p( g - 35. <property name="maxIdleTime" value="60" /> ' z2 K/ h6 N7 x( C# e+ N
- 36. <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> 2 I' C! x7 B7 f
- 37. <property name="acquireIncrement" value="5" />
; \. R, w. ~' a9 e - 38. <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
! A- Z: d$ b7 Y6 b- n - 39. <property name="idleConnectionTestPeriod" value="60" /> * z2 x& W; ]% y* ~0 M9 l1 s9 C
- 40. </bean> 5 F: M7 G E, q2 h
- 41. 0 N3 d; l9 v4 e2 G1 O4 D1 U% g: j( {8 d
- 42. <bean id="sessionFactory"
: n, U: e' [6 Q1 g2 w" y5 Z1 K; { - 43. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> x7 b2 L' [0 F7 Q( V
- 44. <property name="dataSource" ref="dataSource" /> E7 ?( z7 j4 S( q9 Y# v
- 45. <property name="mappingResources"><!-- 放置hibernate的配置文件 --> 6 k) N+ D, W+ c" Q: e9 t: J
- 46. <list>
+ w* A5 g# T4 X0 A8 U4 e - 47. <value>com/yss/bean/Employee.hbm.xml</value> 7 E% M9 N& I. F5 G/ P8 i I8 S' U
- 48. </list> 9 q% i% X3 s; e: i" h
- 49. </property>
0 U) u, B/ W7 P# t U - 50. <property name="hibernateProperties">
$ O# b* O! o& U, K$ J' m - 51. <value>
. @- o: N* f9 i% O& x1 D% U: t - 52. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
2 L( s# G1 y6 ?8 ]5 O - 53. hibernate.hbm2ddl.auto=update
6 E9 t! T `7 b; ]: W - 54. hibernate.show_sql=true , v, \( p& v' r1 H: V) E ?; |
- 55. hibernate.format_sql=false : A/ {8 Q. q2 y( x8 X1 [
- 56. </value>
7 ~3 v1 k, q7 g2 P0 x - 57. </property>
+ `% g# \) P5 }& g5 C9 s0 @ - 58. </bean> : y+ s6 r5 H' u
- 59.
r5 S/ o; H: L3 B8 y. b# U - 60. <!--hibernate事务管理器配置-->
$ |4 K1 S8 Z N. f$ v4 X6 v - 61. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
$ a2 l; b* N$ T( H - 62. <property name="sessionFactory" ref="sessionFactory"></property>
! V2 `' F/ H& X# D/ O/ V, K - 63. </bean>
( v- b- w( C# F( |/ h0 B - 64.
7 ?) v) e! A' m. ^2 p - 65. <!--spring可以用xml和注解来配置事务 声明 -->
- G; a8 ?$ x" R) V- E - 66. <tx:annotation-driven transaction-manager="transactionManager"/>
7 k% q4 [7 A. J$ c5 f! D( i - 67.</beans> 5 ^7 V1 Y8 w( m* P7 v
-
. J/ A0 I/ {$ t, ?+ z) w5 K7 K: e - *配置hibernate的model.hbm.xml和创建model类
6 P( s- d5 `0 g% R9 X6 X6 Z - *创建service
# d& V$ X7 E7 ?9 J4 S/ F r# a - service接口:
1 M( J5 j- p6 v6 y) B+ y: q9 |. E6 f - ! ^3 L' i* ?* l. r! }
-
) z- U7 ^- A S7 U -
2 C i% l& M) H - Java代码
4 \; l3 ^$ C9 {& g _8 L9 O - 1.public interface EmployeeService {
2 f- R+ i8 A) G- J - 2. public boolean save(Employee employee);
4 }1 K; [1 `! q9 L+ E d4 L% ]# O) G - 3. public boolean update(Employee employee); 3 {9 a, L) ]- }* B
- 4. public Employee find(String username); , h7 P9 G: `. k4 U# e8 K" G
- 5. public boolean delete(String... username);//表示可变参数 " q9 e" g& B+ f7 O; Y; j( f% o* z
- 6. public List<Employee> findAll(); ) Z" `$ H. P0 Z" E( L5 g- d
- 7.}
) }' M8 n, `8 o( Q -
! F" q. _ q# x+ a - service实现类:
. P8 s/ M( D8 n* ~ -
) O4 V% ^+ O. P' {6 t- l - : C/ x$ t5 r& ?2 k9 u
-
. s+ @) F( w& L - Java代码
! `8 e/ K/ ] x+ r - 1.import java.util.List;
8 t4 h$ L. l/ L) `+ V1 E - 2.
) u; y0 r( o0 u. k3 A - 3.import javax.annotation.Resource;
2 \$ \0 [1 K! ?, v. v - 4. ! L$ X! V: \8 y/ Q& O( p! F$ F
- 5.import org.apache.log4j.Logger;
$ `: _0 ^& d. v: H" } - 6.import org.hibernate.SessionFactory;
2 _7 g4 b# F1 M& v1 V H; D - 7.import org.springframework.stereotype.Service;
) Z+ O! n: w. T - 8.import org.springframework.transaction.annotation.Propagation; ; M0 P7 K8 g" x; k. V
- 9.import org.springframework.transaction.annotation.Transactional;
0 S$ {/ s, M s7 E4 z) z4 _ - 10.
! u: K6 `. c* V/ D& c$ G! R/ T$ _( N' [ - 11.import com.yss.bean.Employee; & q9 p" D ^2 _; }8 n" q
- 12.import com.yss.service.EmployeeService;
: k& G! L8 o, J& w0 e: y8 I% ` - 13. + u. v1 }9 {( V$ d3 x5 [
- 14./** M$ P0 u+ [* e
- 15. * @author qing 默认bean名称 employeeServiceBean : X# M O/ r, P9 [+ Q2 S$ D. F
- 16. *@Service @Transactional 注入service和开启事务
1 h# `4 j/ `4 P0 b( _+ g' z - 17. */ 5 D6 s1 V3 W' B2 A, V* F
- 18.@Service
" E# D0 \! t6 T- J4 ~! O - 19.@Transactional ! t& X& F& s. m0 Y( {& b) M: i
- 20.public class EmployeeServiceBean implements EmployeeService {
) F; u" t; T: v9 {& Y4 `: g - 21. private static Logger logger = Logger.getLogger(Employee.class); ! h# |# Q8 z' S
- 22. /**
& Q# d! ^5 X5 E3 j2 Z - 23. * 注入sessionFactory
K( F. I% q2 D7 B9 \ - 24. */
% U* z4 Z" _2 \6 w( ? - 25. @Resource SessionFactory factory; & Z9 [9 g8 B5 f3 y
- 26.
0 c6 i$ Q9 E" Z - 27. public boolean delete(String... usernames) { 0 O/ X1 A. J4 g7 t+ c6 L2 o
- 28. try {
& x t. Y5 e. H0 x - 29. for (String username : usernames) { : A8 s" L$ V8 I4 T
- 30. factory.getCurrentSession().delete(
: v: b6 G/ h# Y* O. t& C) {) I - 31. factory.getCurrentSession().load(Employee.class, ( O4 u5 u/ r. S0 S
- 32. username));
0 h5 ]8 b4 t: S. M7 i; W - 33. }
, O( [0 b0 a. b* \; u: x) C6 _ - 34. } catch (Exception e) { ! z0 r4 E4 n6 ?8 Z
- 35. logger.error(e.getMessage()); 1 R) W4 F& F2 H* t: t+ o: }& R
- 36. return false;
- K' M1 Y( t: ]0 h" I, U; G( b) i - 37. } 4 e# D- E$ Y; ~( d2 G0 U' E
- 38. return true; 4 S7 ?' F# @) R4 g1 W
- 39. }
" O5 O: P$ \& \ l - 40.
& N$ L: |8 }1 q$ g - 41. /*
+ `( E& N8 U3 Z( T8 c0 q, O9 G% K - 42. * (non-Javadoc) + T0 L( \& H& R. j' a% Q% [ v/ C
- 43. * 0 I, E- \" Y" k3 {
- 44. * @see com.yss.service.EmployeeService#find(com.yss.bean.Employee)
$ ~5 b( ?4 \2 }( n" j: o/ j5 } - 45. * 此标注表示不需要事务处理
3 a H4 q( v }8 P$ B - 46. */ 2 m( _; v; S+ g3 ? [) R9 w& f7 ^6 s
- 47. @Transactional(propagation = Propagation.NOT_SUPPORTED)
2 S* ^- ?9 l& L& _ v& @0 F - 48. public Employee find(String username) {
# S' m6 [1 }. d4 v- Q/ E - 49. return (Employee) factory.getCurrentSession().get(Employee.class, 5 b; J+ W, O# a
- 50. username); , O# S; v$ K* v5 T
- 51. }
9 [- U. G/ ?! A O* _ - 52. + h/ ~7 L0 `, k, E" U
- 53. @SuppressWarnings("unchecked")
1 Q3 e+ S' Z8 u - 54. @Transactional(propagation = Propagation.NOT_SUPPORTED) / {' g3 b5 M) C3 H
- 55. public List<Employee> findAll() {
) `+ k$ l3 Y2 ~, F5 `0 r - 56. return factory.getCurrentSession().createQuery("from Employee emp") 4 z2 h* M* P+ ` c) B
- 57. .list(); - C) A, R8 r& }+ z% T, s7 D. g* e2 H
- 58. } / ], K! ?/ N, S& S) L" }- Q
- 59. + n9 Z4 j7 l! }4 v7 K
- 60. public boolean save(Employee employee) { 9 I. w0 M8 u0 x3 d; p; F6 G6 Y
- 61. try {
8 u% _$ ^$ C0 i* K - 62. factory.getCurrentSession().persist(employee);// .save(employee);//
$ z" \3 z% e0 L/ u2 s' w( I) C - 63. // 获取已经开好的Session
1 O5 |! \3 ~7 y - 64. } catch (Exception e) { ( e4 N9 _+ j: q3 E0 |+ z% o
- 65. logger.error(e.getMessage()); " u0 j1 Y, Q; }
- 66. return false;
2 u x+ l$ Q0 {" w - 67. } ) L8 ^& k5 c/ P: [1 z! ^2 q
- 68. return true;
+ A+ k- \# \' ?7 y - 69. }
- P% h- ~7 ^# ^% d( P/ b - 70.
! C& }- D" V, v7 J2 w. T& D - 71. public boolean update(Employee employee) { ( f1 f2 n* d4 q9 p- \
- 72. try { / V- s2 D3 O0 r
- 73. factory.getCurrentSession().merge(employee);// 类似于saveOrUpdate()方法 $ o! a2 y; ]2 q$ V7 Q
- 74. } catch (Exception e) {
' Q# w T) C! K0 Z) N" V& J4 f+ b - 75. logger.error(e.getMessage()); - U( o' s, K0 k4 V1 _
- 76. return false;
6 |' R* p9 Z- b - 77. } 1 L0 p& ^* _6 o4 K& t4 w0 o- P8 G
- 78. return true;
9 t- n2 J; {: X/ m - 79. } 9 z: M( q; Y- v3 _9 J. R- [$ a- D
- 80. - H3 |0 M# Z- Q; M7 S8 H% Y# b
- 81.} 8 b3 j2 G. S5 b ~
-
6 y& j& g- b: w. t `* D - *新建测试类
8 a: H0 M# a! B6 Y6 f2 b -
9 M7 p6 b7 O, N7 L* e* `, k -
4 w6 W! d% x- B" b' }* j - 0 X& P; k: ]' R/ }. }! a% {
- Java代码
: O/ P! \) _8 f- c8 [ - 1.public class EmployeeTest { ! x9 B7 Z9 Q5 n' `- l' g
- 2. private static EmployeeService employeeService;
, d! J! ~7 g/ `6 @1 E - 3.
( n; u% ^) ~7 b2 u9 j- B; s - 4. @BeforeClass
' j5 Q, N3 ]9 a - 5. public static void setUpBeforeClass() throws Exception {
# f- i& p( V* U) @. S. C+ q/ ] - 6. try { # A x9 s5 `5 Y+ S" ?: H
- 7. ApplicationContext context = new ClassPathXmlApplicationContext( 5 ]. u( U! t& _4 P0 a6 G6 R& |
- 8. "beans.xml"); 6 Y* j* W) Z6 e
- 9. employeeService = (EmployeeService) context
' V3 t) W E* _- ~ - 10. .getBean("employeeServiceBean");
/ k' k/ E% ]* u - 11. } catch (Exception e) {
, q" E' w) w8 e: G7 n/ b% C6 ? - 12. System.out.println(e.getMessage());
$ Y2 W+ _' _# d - 13. } . R! W2 q* S/ \% V, {5 a# w; M
- 14. } ! t/ ? z+ ^, [/ U* n4 x
- 15. 1 x" c6 I; G& c& u& ~- F
- 16. @Test
( |- O6 F. d! \- z$ J+ U - 17. public void createTable() { ; Q) b" L: w: S! B m
- 18. //new ClassPathXmlApplicationContext("beans.xml"); 0 r0 m' q8 p1 k7 y/ P8 V
- 19. };
6 q( h- l8 C: V m2 N# m: q' j - 20. / k4 T6 W. n9 G0 `6 Y
- 21. @Test 8 ~" u( R/ i9 y$ _. M
- 22. public void save() {
+ E* f. C+ e- p3 \7 i - 23. boolean result=employeeService.save(new Employee("long","long"));
* T5 f0 ~: o; Y - 24. if (result) {
! m9 c) e- a: G+ Q1 y/ `6 O5 l - 25. System.out.println("保存成功。。。。。"); : ]: k) L& y M; f0 H: b
- 26. }else{
4 r' ]6 ~5 Y) Q8 E- Y3 Z. m - 27. System.out.println("保存出错....");
: @, w4 _+ T4 Z - 28. }
7 g$ D9 W. }/ y - 29. }; + A( r1 K- f: F& P6 K9 e- t
- 30.
u- j$ P# B* w8 D2 U2 u" S - 31. @Test 1 P1 g0 J1 b3 z0 S+ `: C
- 32. public void delete() { # {. d& i! Y9 d1 M
- 33. boolean result=employeeService.delete("long"); ( D/ \' H# R3 K4 A
- 34. if (result) { 7 a' J! u: n# }# I* n
- 35. System.out.println("删除成功。。。。。"); ( l- E7 J1 j8 Q1 Q1 o/ @
- 36. }else{ 5 A; S. o7 @! V
- 37. System.out.println("删除出错....");
6 X$ b! O R s. H( n6 q. Q - 38. } ( g7 [/ P) n# i7 ~/ E6 x. E
- 39. };
9 w- e3 a. _8 m+ i; ] - 40.
N( Y) }9 V2 \) ?" s% G U1 T8 |1 r - 41. @Test
% C* ^' l1 b; _ H T, n9 ] Q - 42. public void update() {
. G6 g3 |8 I. k5 K6 i - 43. boolean result=employeeService.update((new Employee("qing","long"))); 5 ?5 v$ G& M0 P, c! v2 @; N
- 44. if (result) { 3 y4 a" \" Q& B$ d- T
- 45. System.out.println("更新成功。。。。。"); 4 x5 b# e: e4 E2 {1 X
- 46. }else{ $ w: [+ E( S6 X: v/ @
- 47. System.out.println("更新出错....");
; x" A( A. r' D" C* n4 L6 s. ~ - 48. } $ |& k* N% A$ x4 k+ O
- 49. }; 5 U7 @3 v" M2 i' z
- 50.
- Q# G9 X/ b+ C: s - 51. @Test
% q0 m: |0 i4 z4 Z& d( o0 q - 52. public void findAll() { ( Y# C$ c/ X; ?" i
- 53. List<Employee> elist=employeeService.findAll();
) F/ [& l" |/ { - 54. Iterator itor=elist.iterator(); : y/ a; a7 Z9 n3 ^
- 55. while(itor.hasNext()){
' A1 Z! O9 D: h; z( }) z - 56. Employee emp=(Employee)itor.next(); - G4 U' q" x, J
- 57. System.out.println(emp.getPassword());
+ L* o4 n2 v# g7 ^) v3 S - 58. }
$ f% N* y' @* W1 u+ l0 T - 59. };
( P# d( ~5 J: M* F1 Z; k - 60.
# ]2 }/ a& B5 o - 61. @Test
! ~$ ~6 p& p& _$ y" r* k- x - 62. public void find(){ % S. I' a" n5 ?
- 63. Employee employee=employeeService.find("qing"); * j: [2 q' f. W6 F4 h' z
- 64. System.out.println(employee.getPassword()); $ K6 M0 |, ~$ M$ Q' k
- 65. } # d& U* W% G8 V4 b/ T. w
- 66.}
# B8 k% A Q% Y0 p - : Q; G1 T- E) b6 e& N. n
- *ok 没问题spring和hibernate整合完毕
, v: i! ^$ @- W$ [, r - / O6 U/ }2 O/ c
- *再将struts2所需包加入lib中
' Y9 ~+ T0 \( W) A" H* C- P - *创建struts.xml配置文件
+ F6 N* M! M- A: y - 9 z0 E5 H* T+ f" B$ m- T" g/ J/ ~
- 0 i6 D }. o% d! \& F9 ?
- 2 }, w3 E3 D( i! K* n" A
- Java代码
7 J/ i+ k# M6 W+ [0 J, @5 e( e - 1.<?xml version="1.0" encoding="UTF-8" ?>
- O# v, L* @0 }+ K. ?0 { - 2.<!DOCTYPE struts PUBLIC " F3 ` J2 ^# Z
- 3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
% B; V6 D7 C% u$ f2 N. j- A# Q - 4. "http://struts.apache.org/dtds/struts-2.0.dtd">
: `- F$ h) Z: E0 z) A - 5.<struts>
/ Q4 [- A7 G, I1 q% Q* y - 6. <!-- 将struts的action交由spring管理 不在由struts的工厂介入 --> 6 d& B: k V: f% i8 m, k# s
- 7. <constant name="struts.objectFactory" value="spring" /> . m5 O- P- V9 [. j# g8 g9 V5 j1 ]
- 8. # w; p- a4 i# y1 G( i! G! K
- 9. <package name="employee" namespace="/employee" extends="struts-default">
- h/ O; I9 e5 k2 ` - 10. <action name="list" class="employeeAction"> 2 O9 n% @- ^+ @' B. A0 \
- 11. <result name="success"> , y C7 ]5 l6 S+ n! b2 c8 S, r6 P
- 12. /WEB-INF/feapp/employee.jsp 4 g0 t& r% E* z* `6 `2 Y5 Z9 }
- 13. </result> & S+ X5 q8 N7 p E/ t: C( K
- 14. </action> ( i( u ]9 ~# M& |5 O. m9 B$ g8 M/ g
- 15.
% N# R- u( n8 C D V - 16. <action name="manager_*" class="employeeManagerAction" method="{1}">
: j" a9 N! ~# P$ b( b! ` - 17. <result name="success"> ' i0 v2 @' ]; ~7 M
- 18. /WEB-INF/feapp/employeeadd.jsp 4 t. s0 h# L/ i
- 19. </result>
! Q: O6 Y" [* ~ - 20. <result name="message">
4 `1 U* h( ^+ Y6 P) @1 m - 21. /WEB-INF/feapp/result.jsp
" _/ _" C8 l. d0 K" _ - 22. </result>
9 w e X O% E5 u: B. g - 23. </action>
. X I r( [# [- w1 r7 M( z9 m - 24. </package> 2 s, b9 w9 M8 @5 K7 s6 `% g9 ~; ?
- 25.</struts> * C2 m% Q! ]5 B c5 F
- 6 n# D3 I7 i, M9 e
- *在web.xml中加入
& Q' r; L- X: H: v+ O) n% n - ; K6 b; T: U' B7 z- X6 K8 l
- 5 Y. X+ ~3 G4 ^( o8 D* G
- * a9 O! [4 X, e( y7 N1 E5 T
- Java代码
2 A P K6 v3 S - 1.<?xml version="1.0" encoding="UTF-8"?>
8 L& q+ F: L8 E0 F2 s - 2.<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
8 r% E8 B6 t* A - 3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
$ x8 ~7 c# g: I$ X7 I0 @; V+ z - 4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 8 h& C) w; A. \% h
- 5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
( h7 P G L/ I6 y2 i1 A! n8 t - 6.
+ f: r8 n# |+ A) M3 @7 M- \ - 7. <!--
" Z/ p$ f4 g# I$ ~; `' u- y _9 A% k3 { - 8. 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 0 M4 v0 F! c a+ u: X% m
- 9. -->
% `6 i& j: |! L O- C% g - 10. <context-param>
" Z9 m7 b# [# D/ V - 11. <param-name>contextConfigLocation</param-name>
+ ?: l( j* L2 \# T5 k z - 12. <param-value>classpath:beans.xml</param-value><!-- 多个配置文件的写法 classpath:beans1.xml,classpath:beans2.xml,classpath:beans3.xml --> * h; f( {% U7 u. \
- 13. </context-param> # \% v% Y3 _$ Q% v) X/ ?8 B
- 14. <!-- 对Spring容器进行实例化 -->
+ l5 u [( Q' s' R- ^* i0 H( I - 15. <listener>
; U2 F0 T* o2 r - 16. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
V( N% N2 x ~% F - 17. </listener> & o# H7 A4 Q, H! f4 D
- 18. ( B( w* }8 ]7 J! S% A. O- T
- 19. <!-- struts2 的监听器 --> Y, m, m" ?! ~/ D
- 20. <filter>
; z; Q" p3 k! X, ?& ?' ^8 q. R - 21. <filter-name>struts2</filter-name>
) M% @1 i, m2 A5 b3 ^ - 22. <filter-class> & m0 o7 ?: [! t0 y$ C" k. k- M$ h1 _
- 23. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 2 `* p* C* p( S
- 24. </filter>
' c3 _1 ^) w) T% v - 25. <filter-mapping>
: g1 z$ X+ Z# I, S( p; k! p2 V - 26. <filter-name>struts2</filter-name>
' R! n- s& N i# W8 x; L) I - 27. <url-pattern>/*</url-pattern>
H$ z& [" [; z - 28. </filter-mapping>
9 c! D' Q. [ H6 I; B - 29.
! P3 S! \8 r) j6 j0 z- @( S! l - 30. <welcome-file-list> 8 B5 r, U/ A! S m( O& O
- 31. <welcome-file>index.jsp</welcome-file> , `5 [: ?% O# v% x0 ~: m, d
- 32. </welcome-file-list>
$ M" \6 Y4 h0 m6 p' m: Z+ y - 33.</web-app> , h# D; D m, W0 t& g7 B
-
' k7 c: a# u2 ]4 M$ S - *创建相关jsp和action
: w1 P* Q+ a( H" ?. ^2 V - 6 P3 q9 k2 S! E1 _; g" p- r# |
- % m) ?9 Q3 w2 l' S. V7 I% A
- & W1 u+ f0 F. Y5 d n1 M
- Java代码
1 s) O* F3 r+ i0 t - 1.@Controller @Scope("prototype")
- A/ p3 X8 G" L/ ?; `5 u - 2.public class EmployeeManagerAction extends ActionSupport { / u7 d* d1 ?3 x3 `4 H8 U3 l
- 3. @Resource EmployeeService employeeService;
6 r2 S- k! h: Z# f) U5 A7 Z3 O - 4. private Employee employee; $ b& q, N! a2 U5 A9 Y4 y' A
- 5.
( I4 B3 w) e. N: |$ B - 6. public String addUI(){ ' L6 j7 f# J. S/ L. K) j- r% m. [ ?1 q
- 7. //System.out.println("user come"); ! K4 j9 \ |( g
- 8. return SUCCESS; & u1 d- n/ X7 E5 U
- 9. }
$ M, Y2 n8 M/ x% S - 10.
* l! ]: g1 h, q& X. b" f - 11. public String add(){
- \4 p. w2 L& j& D4 _. d2 j0 x( |6 b - 12. //System.out.println("--------------");
5 H, q' _0 ]% u- T( ^9 n# ] A }$ V) [ - 13. boolean result=employeeService.save(employee); 7 U" r* z: F0 d& u9 S
- 14. //ActionContext.getContext().put("genders", Gender.values()); 0 m- a3 m) \7 E) ~! j6 A: B
- 15. if(result){
+ v; ^" Y2 A: K1 J - 16. ActionContext.getContext().put("message", "保存成功!"); 7 X% O! y* I9 B0 ~, o5 Z4 I
- 17. }else{ / f" r& ^% f$ \. H9 N) | Z9 P, J
- 18. ActionContext.getContext().put("message", "保存失败!"); ( k, J3 l5 V% g" M/ S4 I# y8 H
- 19. }
) G4 V5 D* f9 S. D' o$ N - 20. return "message"; 3 m+ F* \0 S4 ~, g
- 21. }
. j3 A' L$ B& I5 {) @% A* w& [ - 22. " a- x1 b$ ^1 o) G7 Z/ O8 H
- 23. public Employee getEmployee() { 0 H+ S4 R+ m. _4 A2 n8 F; k+ b8 w
- 24. return employee;
( h) i- e3 v, x& v - 25. }
- f4 d: X8 d8 ]5 A& O/ e5 ` - 26. l g. o m2 T. T, v
- 27. public void setEmployee(Employee employee) {
$ Q0 j" ^1 H1 O( L9 k& I - 28. this.employee = employee;
9 `1 P/ ^, n6 |" U6 f4 P - 29. } $ G- Q( t7 O$ D' [* k& D1 p7 C$ w
- 30.}
复制代码
" H" g) x @! Z
' N$ p& Y, V8 e |
|