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