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