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