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