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