TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
6 A, e/ I8 w* D6 ]7 K% n- A1 ~% X2 e* E, o+ @ P' P* {
BookInfoAction.java:9 p: T* ^$ r5 O3 ? |
- /**
1 L) r4 U# Z8 x; T - *
2 _+ D2 S/ y6 r1 D - */
8 ` I2 S+ I4 N: ]- D1 Y: U - package com.s2sh.annotation.book.action;
+ ^0 ]2 F/ n' @% m7 S4 } - import java.util.List;
' Y; C6 P: V) @6 d' N: Y- C# Z! k - import javax.annotation.Resource;3 h( M$ X8 H! S8 N+ H7 g
- import org.springframework.stereotype.Controller;
& ?. T: d5 P) p7 g# r+ q - import com.opensymphony.xwork2.ActionSupport;6 w; c5 T$ {2 j Y
- import com.s2sh.annotation.book.service.BookInfoService;
9 [. E( s9 x$ o4 Q$ ~6 L - import com.s2sh.annotation.entity.BookInfo;
# @0 v1 n5 P7 ~! B% e5 z- l - import com.s2sh.annotation.entity.PageBean;
$ v3 X# j/ J8 A. E8 a
7 s+ {; K% J# q* f8 ]5 t- k- /**
! G4 b. n! q$ [1 ^, u( { - * @author Administrator
4 z i% l" @/ N! l9 Z, u5 H - *
' C r0 N# h7 m& d: O% X; _ - *// N! O U( }5 _ q- r
- @Controller // 标注控制层对象
0 n! \* _9 i% Y) s: o - public class BookInfoAction extends ActionSupport {
6 O" M0 F& t2 ?, S+ E& G4 {$ f
) U) f& i: E& E1 [" s& s9 ~% G- private static final long serialVersionUID = 1L;1 V9 v7 q3 d5 Z# D
- 8 x* O9 u" w( z+ x
- @Resource(name="bookInfoServiceImpl")
( H* D l/ f5 u- e+ A O# H - private BookInfoService bookInfoService;8 _1 R" i6 \! i$ L# O
-
" I: K$ Z {8 q) T - private List<BookInfo> books;
. N q$ z$ T$ a+ S - private PageBean pageBean;" ?+ ^$ O" J) ^8 O. ~2 k$ k
- private BookInfo book;+ j+ {$ k% K1 d
-
8 ?" ]5 [& g8 I3 J$ A -
* S* A0 A5 t5 U- k - /**3 H" p- V! l' Z7 h6 {
- * @return the book8 R# U) P9 Y! w' x% N1 |) J- i" b
- */7 b9 k8 V+ a. y) F+ v- P
- public BookInfo getBook() { G7 V0 {- N+ X$ o2 {9 g1 ~' O
- return book;
( { f5 i8 P* J& v/ s - }
) ]4 O0 Y: b3 a4 I2 t2 [ - /**4 a. i3 I" ]% M
- * @param book the book to set. i; x: x# ?/ C7 N! J* p' f9 V
- */2 N, s. |8 U0 B4 K$ u
- public void setBook(BookInfo book) {( N, Q; w, a) I. u/ Y
- this.book = book;
& h" B3 G' v9 J% H% ^ - }+ Q T0 \$ X* V2 j) T: @* J
# O# o7 x F7 K9 l4 Z- /**9 \3 }4 z+ I' C8 s+ q! L
- * @return the pageBean
! o% G5 F3 F* U# Q& d - */& m8 f. y- Q& _& ~2 `1 r
- public PageBean getPageBean() {
9 }% I* Y" k; E - return pageBean;6 `/ q9 D8 `( |# ~0 U
- }
; D2 e/ @9 U$ H! f - /**; Z0 @8 Q4 l2 U4 b: U
- * @param pageBean the pageBean to set
4 d% K$ w2 E# F4 p% z2 C - */
8 w0 g% N3 n, H( ~: V7 @" o - public void setPageBean(PageBean pageBean) {* g( ^ R4 Q: b* v% O. N$ e/ G* L
- this.pageBean = pageBean;# x) m9 \, s) ]" _ I; G
- }7 p ~1 _5 k/ R/ t
$ V2 C& @2 c% A4 n* Z+ P5 }/ Q- /**$ _+ t$ E1 e1 f0 ^
- * @return the books
+ x' c; ?9 d, k; ~2 d8 v2 E( Z9 d - */4 D p- G+ ^1 P1 T. }( T
- public List<BookInfo> getBooks() {
4 w5 F7 Y* a& y7 V3 ]% r - return books;3 r8 B4 z5 e& S# u
- }# H! v2 v) b, Q) Z2 [& v
- 0 K: V n" X2 S# y1 S
- /**
" W( H! f S; n. ?9 L" B - * @param books the books to set
/ Z4 [5 a/ h* p4 q - */: M% E- z: \; t. c, I& {9 q+ a, X
- public void setBooks(List<BookInfo> books) {
% j8 N: [; C2 s+ e - this.books = books;* r% j3 a: h& E6 K! h
- }5 I% s! E a) _7 d9 c
& |) j3 B5 ~ O; { U. Q9 {- public String showBookInfoList() {" {* s$ L( ~! e4 L M+ y Q+ n
- pageBean.setPageSize(5);
7 P7 C& K ?0 [1 `7 m" z9 L0 d- [ - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
: S6 d3 u" t u% | - return SUCCESS;6 I% x9 s% v3 I" Q! g e8 u
- }
. w) I9 a& o$ I9 s8 a1 G2 \
]' W+ Z& H2 F) N, v. |; ^1 @9 z- }
# K$ S3 P% V8 r' M( n
复制代码 applicationContext-persistence.xml:
o2 F5 x/ z( h0 L/ ^0 X- <?xml version="1.0" encoding="UTF-8"?>2 `3 A' j+ F( K) J( M8 @
- <beans xmlns="http://www.springframework.org/schema/beans"
# _5 X( b( I/ j0 N5 O2 K+ k - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
( p7 k! Q# A! t0 n+ \1 _ - xmlns:context="http://www.springframework.org/schema/context"1 M5 U& p1 A F0 N# c
- xmlns:tx="http://www.springframework.org/schema/tx"
. O2 {0 F4 { i- O. b6 w - xsi:schemaLocation="http://www.springframework.org/schema/beans
" E' _* v( P# Y6 A' K) I, ` - http://www.springframework.org/schema/beans/spring-beans.xsd
8 r" }! Y2 f/ c1 \8 `# h - http://www.springframework.org/schema/context
" T9 A* `9 C4 T3 ~4 G3 F - http://www.springframework.org/schema/context/spring-context.xsd % ?- k4 \5 {# T. V( v( m* R- T0 P3 X
- http://www.springframework.org/schema/tx
( C% e9 @2 \. q9 z0 ? ?( M$ X% @ - http://www.springframework.org/schema/tx/spring-tx.xsd">
; E9 D. q6 g$ c. F, e( Z& T1 u - <!-- 使用注解方式装配属性文件 -->
2 J2 j- O: u6 k# W+ z - <context:property-placeholder location="classpath:hibernate.template.properties" />2 t: d; k( A- |9 O
- <!-- 配置数据源 -->3 a% ~. ^1 V- R( C. E' c
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
0 s6 `' e5 N4 l - <property name="driverClassName" value="${hibernate.connection.driver_class}" />; V2 T5 K0 m: m. S' w2 W5 s
- <property name="url" value="${hibernate.connection.url}" />: i8 a T2 z: T
- <property name="username" value="${hibernate.connection.username}" />2 R7 |5 R3 I; W/ m( J$ p
- <property name="password" value="${hibernate.connection.password}" />; l7 v: p2 k! l' N. O: [
- </bean>
5 [' a2 D! i$ c - ) U5 v% k6 g! s5 \! Z) r; L' N
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
* _$ F4 t% l9 {# J- P0 [ - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
& V, }' u) k$ E7 f% d. C6 S - <!-- 把spring容器中的数据源注入到dataSource属性中 -->
4 O/ l: a. a+ |( @* O! L) m - <property name="dataSource" ref="springDataSource" />/ i' }: S& o* ]; d
- <!-- 扫描装配需要管理的实体类 -->
4 b. X# N7 I4 \% F0 G2 x: | - <property name="packagesToScan">6 |/ E! _( @5 w! r$ Y# B/ f; @
- <list>. m7 E+ N# x3 m" a4 J* k
- <value>com.s2sh.annotation.entity</value>
; s" |: m+ w( P4 X, D$ X# T2 `7 O. Q - </list>5 S" _& W9 |. Q S9 i& M9 S( M) h
- </property>
( l' Y+ t- w, o2 a2 E - <!-- 配置额外属性 -->! g- s+ u8 J- z# o& L: E# ?4 b
- <property name="hibernateProperties">
- O D, T |! u5 t - <props>
1 [& b0 e' H7 B. h/ u9 @ - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
5 }4 l8 b3 h7 M1 P7 `7 w - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
% A& p* E: Y6 u5 H - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
, f, X& M3 P8 ? `, z$ j | X - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
0 B2 @ f# ~% d7 W$ j7 C - </props>
" M. w3 `8 i$ i5 t2 `0 Y+ X3 j& o - </property>
' p$ K+ B0 I5 H - </bean>
) w) N6 }' u" Q -
' z& y0 _0 W5 x. n - <!-- 配置事务管理器 -->' @3 g( ~, r( p4 _9 I
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">$ m, {; l5 ]& I1 C- U- s
- <property name="sessionFactory" ref="hibernateSessionFactory" />
) C9 p" k+ l: R* P/ L - </bean>
& W/ K8 R% P. I8 P; W - : _* w) v# e( U6 a8 \* r
- <!-- 使用注解形式来管理事务 -->
% |% y; w# \7 ^# _' a2 Z+ i - <tx:annotation-driven transaction-manager="transactionManager"/>1 V: B1 G$ }/ V3 H4 [% q
-
1 A; j* r4 F" X7 f! b - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->
6 E1 W) x ` V7 l* X) a# e - <context:component-scan base-package="com.s2sh.annotation" />$ P; D O) a" N1 ?/ N
- </beans>
复制代码 struts.xml:
; v: ^5 O/ H k, K7 ], y- <?xml version="1.0" encoding="UTF-8"?> F* r6 d- N* N0 M, T2 O
- <!DOCTYPE struts PUBLIC: R0 F# u& R1 a: D T. A
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
( s! _9 m9 j& q c% O1 |. c" [ - "http://struts.apache.org/dtds/struts-2.3.dtd">- D1 e' }$ r+ W( N8 Y; @5 b) r
- <struts>
( \) D* c4 s! @0 S5 o - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->: S5 @! `2 C! C2 y
- <constant name="struts.objectFactory" value="spring" />$ W y& G) H1 b' w" [9 f
-
4 F. q+ H+ a; b u1 P+ r& t( Z8 ~ - <package name="base" extends="struts-default" namespace="/" abstract="true">/ c% j" c5 f8 M/ C, P
-
- z2 f6 m9 k# b7 Y& e+ X9 P b% z - </package>
! d9 o% ?+ }9 V. k `9 c" ]& W - 5 I/ s( F- h8 d7 X6 K4 @* _
- <include file="config/struts-bookInfo.xml" />1 e% A+ U3 ~4 i& D" B' \& p! H, K
- </struts>
复制代码 5 k/ A5 J% w! Y6 E- I% W
S2SH三大框架采用注释整合项目案例3 n1 Q) Y0 M# t$ i# W4 f7 _
解压码:
. ]) F, g3 q! Y/ z; \3 U& J8 B+ ~0 E( p5 o
( H$ P# ^: @7 Q0 Q- o8 o
: V# [) o& L: W# X3 y4 e |
-
总评分: 帮币 + 1
查看全部评分
|