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