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