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