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