TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。4 ?6 U0 u+ O0 N* M
% J% |! Y; U+ c1 W+ I' \, D
BookInfoAction.java:3 H' \* S6 q7 x# v8 N3 f
- /**3 {: U* J) A% c3 D) r5 N
- *
& Z2 f# r3 o7 j9 b1 `8 J! Z - */! w, J: \0 I7 F" t8 S: c1 r- o, L
- package com.s2sh.annotation.book.action;3 l: F( I& G, r# W1 S- n
- import java.util.List;
5 P( ^/ P' j8 i$ ?: w: E% ~$ i; } - import javax.annotation.Resource;
& s: o- ^ n+ \0 ]$ U - import org.springframework.stereotype.Controller;% N. M8 R% ^0 w3 q: z: A% l
- import com.opensymphony.xwork2.ActionSupport;
* K% s& x9 ], {0 B - import com.s2sh.annotation.book.service.BookInfoService;
8 P6 a- E4 [" v$ H3 v0 [ - import com.s2sh.annotation.entity.BookInfo;8 W* J+ M1 S" L- x
- import com.s2sh.annotation.entity.PageBean;; y, L4 u: W8 I) a1 E
2 T+ H. m0 g/ J/ a/ s4 s$ W! H s0 n- /**1 u2 o. ~. m- W3 u2 U2 U! E( u
- * @author Administrator$ V6 L* A4 J" P0 W
- *. g @9 C% x; E. T
- */8 C0 z" Z3 ]4 b( U, R6 V9 R
- @Controller // 标注控制层对象
. T0 S, g6 d# L8 q& H - public class BookInfoAction extends ActionSupport {7 z f; n! s0 n3 N3 y$ i
# ?0 z. C: X9 L( L- private static final long serialVersionUID = 1L;
# c( B/ |: p" B$ n& u/ k - ; f# t, P$ L9 w; }8 T. H3 ?$ Y1 S
- @Resource(name="bookInfoServiceImpl")8 O4 M: z7 t9 ^
- private BookInfoService bookInfoService;2 ]( [5 f) j5 q
-
|- @8 ^' {6 R2 B - private List<BookInfo> books;9 V7 ]7 Q! ]& k4 }* L) O
- private PageBean pageBean;
" k; A, R* | _' Q - private BookInfo book;( }. m @0 R* U: a
- 3 c( o2 ? M" h3 q0 Z
-
& ~" q8 J- S+ q6 v u, S1 x+ ]) e - /**
# e+ `; b Q# |- U" _6 _ - * @return the book
1 D( h8 d# ?9 V1 W - */6 j/ T6 P7 d' B6 o
- public BookInfo getBook() {2 j4 J9 h9 F! T
- return book;) w. j k+ Q! o2 x/ ~
- }8 T5 C2 | A+ @* O2 g/ N+ H* x
- /**) M \) f* s b! F4 _1 E# p
- * @param book the book to set8 }4 Z6 n. Z% B+ p# |4 d
- */- }. F- O# X8 O0 M
- public void setBook(BookInfo book) {
' U# M6 R/ p! _7 N s - this.book = book;/ g3 I; d$ l, e7 V" E
- } ~& I3 A. e2 I* X: q/ K+ S- s# f
3 _, ^6 p/ M/ ~% o. [- /**
+ G8 P/ ?5 J/ m7 G - * @return the pageBean" B8 Z; @. j# T. |( B
- */* [) d3 m7 G5 a9 r1 L+ m7 p
- public PageBean getPageBean() {: O2 C+ v f3 @: n9 R
- return pageBean;
. F0 Y# }: ?: p+ K y - }
& B' c+ D* ?3 Y) T1 I% V* X$ } - /**8 w- z9 @8 d* _3 l, {* d. h3 O
- * @param pageBean the pageBean to set+ C6 i3 ^2 Y1 z3 Y- }
- */0 F# N5 N/ T$ l k- J
- public void setPageBean(PageBean pageBean) {
( i3 V2 h5 R" H - this.pageBean = pageBean;
; V7 P- o: ~; n$ ^: \" d8 G - }
8 E2 r6 S* |" Z, q1 `
4 p" L6 e8 }+ h) F# x- /**8 Y2 R5 m q1 ~ z( n6 |
- * @return the books
- j7 h( O1 s6 P3 } - */0 b' I+ D" a: S4 J3 L
- public List<BookInfo> getBooks() {
. F" f# Z/ Y" ?9 y# Z0 R4 t- c - return books;
$ f9 ] F) ~2 J( @; V$ A6 P - }
. }( n& B7 ] a: C - 6 q/ o' K( f8 [) n" R& s+ r: W
- /**
- M p5 i! V7 T$ @ - * @param books the books to set* `2 ]. G/ X [1 B, c" e
- */& s! o6 i# e- z/ z! J
- public void setBooks(List<BookInfo> books) {+ M4 ~1 X) @, Z7 k: m, w
- this.books = books;
" x% V0 ]* M1 ?/ L - }9 e8 r' Q- M/ Z# ~. v) a/ M
- 7 d0 c. _6 f1 D
- public String showBookInfoList() {+ s5 b9 u9 M& x; g
- pageBean.setPageSize(5);
* o4 G. E4 P: O& ~( S1 j6 I - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));0 ]6 |7 q) T* z" U
- return SUCCESS;( ~, {( M% v! y; V
- }
; G, Z1 u; J4 x% r% R0 v% B3 T - & V5 r6 ^; s0 [, C! [
- }, B& N' A8 _" |0 ^5 a' L! [9 Y
复制代码 applicationContext-persistence.xml:1 b) |1 F" a9 G, \5 S
- <?xml version="1.0" encoding="UTF-8"?>2 R! {$ y% u0 e' p
- <beans xmlns="http://www.springframework.org/schema/beans"2 F3 b: e. G; {" v, d3 r* H w
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"9 h' M" |& H E5 e& [
- xmlns:context="http://www.springframework.org/schema/context"
6 c7 K- T8 `; b' O0 S+ ? - xmlns:tx="http://www.springframework.org/schema/tx"2 ]& G/ m/ M n
- xsi:schemaLocation="http://www.springframework.org/schema/beans
9 Z8 ^, P1 [- V% Z- }4 ]4 P - http://www.springframework.org/schema/beans/spring-beans.xsd 8 L* w I( V, a5 e4 L
- http://www.springframework.org/schema/context
/ K5 J. W8 K2 V% W- y0 i' R! A - http://www.springframework.org/schema/context/spring-context.xsd 2 M. q' Q9 h8 ?$ O8 C# F% F8 W
- http://www.springframework.org/schema/tx
/ t: }5 |4 O# r& V3 A - http://www.springframework.org/schema/tx/spring-tx.xsd">
: j2 n6 [' b5 H2 n - <!-- 使用注解方式装配属性文件 -->- u& D: @4 v& H! V7 U. h
- <context:property-placeholder location="classpath:hibernate.template.properties" />' \( P* ?+ n& F( }: U
- <!-- 配置数据源 -->
" t \! Y2 j% r! | - <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> \/ I- y+ L6 W( v
- <property name="driverClassName" value="${hibernate.connection.driver_class}" />% z# I, h, A0 F2 J
- <property name="url" value="${hibernate.connection.url}" />
3 m; d* I, B' X# z8 I7 f1 n9 G - <property name="username" value="${hibernate.connection.username}" />
* _+ k6 o3 f4 N+ n - <property name="password" value="${hibernate.connection.password}" />
( r* o! O, i: X: ~% O - </bean>
- `, W/ K9 y8 i1 h! l -
5 x6 t' o7 j- Q( t - <!-- 整合sessionFactory对象到spring容器进行管理 -->
4 l% _$ I3 N1 O- i - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">9 c6 V W( _9 J1 [! c+ G4 B
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->* `; o' ]% E6 c- J
- <property name="dataSource" ref="springDataSource" />
3 k0 B! m" \; G- P - <!-- 扫描装配需要管理的实体类 -->" n( Y, S$ I0 c; h2 B% A
- <property name="packagesToScan">
$ k+ S0 z3 C$ g' j$ Q - <list>
# M |9 i0 _/ v3 l! a4 g& _; z7 w - <value>com.s2sh.annotation.entity</value>
" K9 C) d$ E$ R! s0 p4 r- J( g+ [ - </list>6 B7 G5 o+ f7 m8 ?3 v/ y9 r0 U" u
- </property>
1 I" ^4 u9 q# s9 s% T - <!-- 配置额外属性 -->. j7 T @& {- k$ P: ^+ M
- <property name="hibernateProperties">. d q$ M+ z- D z3 _
- <props>. a6 w8 e' s; k- p L1 D1 s( P; q" u
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>) i' ^. S, A* E% ?* @
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
" G: W0 l }( m/ z6 }4 s - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
: i/ d4 f5 _- v0 h! } - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>3 D& T, x# U1 p# E. p
- </props>: l' t# a. O9 _8 g
- </property>
0 p; [* R. I2 Q - </bean>1 j9 O, z8 K5 N3 |3 m
-
! ?- s L; {, } - <!-- 配置事务管理器 -->
$ \+ G- b- a+ A. D* I }, F - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> {8 d8 c/ T0 _# y# o
- <property name="sessionFactory" ref="hibernateSessionFactory" />3 Y1 w) J4 c4 a& K
- </bean>
1 p1 L5 t3 {' Y3 M# m -
! n& F2 G, }# q; R) T2 S - <!-- 使用注解形式来管理事务 -->( y. @# m" _+ U% X, W+ @; ~
- <tx:annotation-driven transaction-manager="transactionManager"/>
6 { r6 M. h+ S* q# p q$ ~+ I3 e -
0 E/ }, x |* H - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 --> P, A. R' i, \, T
- <context:component-scan base-package="com.s2sh.annotation" />1 B/ j# ^3 t8 l5 F8 }
- </beans>
复制代码 struts.xml:
' v4 m) ~6 ?* o; O4 H- <?xml version="1.0" encoding="UTF-8"?>
( J+ P# e( h/ m5 F6 f [2 ? - <!DOCTYPE struts PUBLIC
# s; m- ]% F, \* o! _, f0 J - "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN". h. c) T& k: |$ i; V: v
- "http://struts.apache.org/dtds/struts-2.3.dtd">
, o/ v2 o$ v& L( a7 y- j - <struts>
3 A: u/ v7 t; h0 h% b2 Z* i0 @, a0 S - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
$ k$ i2 @& d7 g2 N+ t. r - <constant name="struts.objectFactory" value="spring" />' w( u1 h. }/ C8 e1 `
- ; _( y+ w& j6 t- s
- <package name="base" extends="struts-default" namespace="/" abstract="true">
3 L1 v+ \1 t; T/ U8 r. [* d -
, I7 n$ P6 u% ]8 X - </package>/ u8 H+ S+ l; N+ b
- ) l4 C3 U# G' n4 F6 [/ T9 r
- <include file="config/struts-bookInfo.xml" />
( I( m- }0 H7 b2 b5 w - </struts>
复制代码
2 Z( ]; y# q8 k5 s- L& d oS2SH三大框架采用注释整合项目案例 } L* S% i* _- m: K
解压码:6 ^3 S8 { z2 X" S
! ~; m2 m9 S0 v& b/ d
. Z; }7 t( _1 T5 I1 s/ u% z/ O. B) |1 i h" }
|
-
总评分: 帮币 + 1
查看全部评分
|