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