TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。5 F) R8 Y6 x+ Z; A: n9 r6 n
! v. C0 ~& K* f4 N' i; I" e3 P% d
BookInfoAction.java:
c4 L+ n% ]7 P* P E- /**
- S) \3 j- m3 e! \3 G' L* a8 x - *
4 x) g; ] F8 F1 R }& F7 L% c - */) S. X% ]0 D( [ _! \" C4 [
- package com.s2sh.annotation.book.action;" V, N5 u/ X# U0 M: @9 C: g s5 s+ F
- import java.util.List;7 n( U+ f# z+ X
- import javax.annotation.Resource;) k- ~$ _5 h& x+ t# W' j) T
- import org.springframework.stereotype.Controller;
- ^' `" n# D9 P* P$ o! H2 f - import com.opensymphony.xwork2.ActionSupport;
/ b. k) a' s* G! V) O, G& ^ - import com.s2sh.annotation.book.service.BookInfoService;/ ]5 o) Z/ m# B7 ^ C
- import com.s2sh.annotation.entity.BookInfo;
# ? C' \" F4 d6 ]+ y' Z - import com.s2sh.annotation.entity.PageBean;
- w6 L! Y/ U; f/ g! @ - % r5 h, S6 F: z5 ^, W# t" B
- /**9 x' l7 _* y6 g! k
- * @author Administrator2 g8 W( K( B* t! Q- [
- *
+ k* ?- N7 ~$ [1 k! }% U - *// K, q1 Z0 o1 R" `& S
- @Controller // 标注控制层对象
7 a p1 ` d. u' {1 G' {8 A- a3 e - public class BookInfoAction extends ActionSupport {$ F5 `: p, t$ \ t
- " G0 M$ S3 G0 ?& ~
- private static final long serialVersionUID = 1L;0 [$ X: U# p5 i |- B
- 4 q, C8 O( j) [: a# p
- @Resource(name="bookInfoServiceImpl")5 r$ O3 o3 F# s5 H( [5 P/ P% U Z
- private BookInfoService bookInfoService;
( m4 S1 |" p; w$ u -
2 L1 V! R1 z9 V2 ^5 E - private List<BookInfo> books;& L9 I1 \& O) s* M
- private PageBean pageBean;
3 U! [! n7 j( _8 X* e) o - private BookInfo book;1 V* w1 n6 w- K5 s: L- [" H! n& m
-
0 z- D( g: I& p9 d' w- w0 U+ e -
& T' h* n& l' b - /**$ m4 b# o' c" N* p9 D
- * @return the book
% r3 g) ^4 w0 X; \8 d8 A/ I - */
( r1 `' H1 y: n. h9 @7 K) K1 h! t6 V9 ? - public BookInfo getBook() {6 i. s( J- Y5 Y9 b7 g" v* U2 I
- return book;
& T1 g6 r) o9 B4 q: Q$ G" B! f1 M( { - }) l* A, ]/ ?, K9 P; v
- /**. e. N/ l; O5 A
- * @param book the book to set
! P" l' n x8 y( [9 |5 ` - */0 R- T6 U0 x6 n7 {) C
- public void setBook(BookInfo book) {" Y1 N J- ^ O% ^/ W
- this.book = book; m* M; }9 x- P, {7 B6 v& D% b
- }
: L, o- X* n- G# \: F/ [. k; Z - ! \2 ^' |/ p2 t/ l; U
- /**" y9 K' b, [0 w) U5 d
- * @return the pageBean5 p; d% t) t8 a% _' u% R5 S X
- */
: H Q% R% P; E2 s( x3 l# O - public PageBean getPageBean() {; B% R. u3 F6 j3 {8 V
- return pageBean;# U, g7 U8 y" n% A3 e
- }% I1 H' O8 v$ B( b- _. k
- /**. V5 |. h K: }/ f
- * @param pageBean the pageBean to set b9 k @7 I: r% q g
- */# _$ N* o# s( E0 Z! ~8 e
- public void setPageBean(PageBean pageBean) {# T( |6 q& j3 l) \
- this.pageBean = pageBean;3 C$ P ~9 |; H7 U
- }
2 d. E1 G$ \, M: f: x S; M9 R0 {
% [. N7 ^& ], a t- /**
2 S) H2 [) h6 I7 N* k7 n, r - * @return the books
' x$ T6 M" ^% V+ n - */
8 q8 s; u" Q1 O J' C P/ Q5 p1 S `9 ? - public List<BookInfo> getBooks() {/ L0 W) n+ @, |, D
- return books;
$ w& M# t# b5 ~ - }
. R" a2 ?6 s2 B& I: w
$ F; Y. P, C5 t- /**0 b& u* D3 z9 i- |4 I* p! s& R
- * @param books the books to set" V/ k* o8 y/ ~5 r8 D* s. p: \
- */
) u A4 {/ g4 d4 P' V - public void setBooks(List<BookInfo> books) {
$ ^/ _& S2 |8 O0 W - this.books = books;
# P7 w- h1 u4 p0 t& V+ p% j; W* @ - }
4 H( y% P3 r' O8 s+ X
8 t9 U L5 A! j: ]' E' s+ P- public String showBookInfoList() {
. w6 z7 b3 z3 i. G( J7 P - pageBean.setPageSize(5);
2 `. |& h# R2 J. F7 S a6 W - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
7 L8 V0 w" \! \* ] - return SUCCESS;
; s* d' @) Y1 s/ E; Q' k% ?# k! ` - }
, y: D9 U* a% B1 ]! G+ ^! x0 y
U6 k1 y+ a# Y- }
* c7 R+ N- H. R( L, T
复制代码 applicationContext-persistence.xml:. s( \, M" j: P/ B1 U0 `
- <?xml version="1.0" encoding="UTF-8"?>
0 t5 s% G& Q$ y+ m+ R - <beans xmlns="http://www.springframework.org/schema/beans"( ^% \+ E+ @ \ s
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"- ~5 `) S/ w1 P! h& n* ]9 T
- xmlns:context="http://www.springframework.org/schema/context"7 {) H6 Y# f- ?. l Q3 G: s0 u3 X
- xmlns:tx="http://www.springframework.org/schema/tx"
$ T) D' C" W' j+ ~ - xsi:schemaLocation="http://www.springframework.org/schema/beans % @7 X5 @5 C- Y* w3 Q7 \: l
- http://www.springframework.org/schema/beans/spring-beans.xsd
+ }) V' P/ E8 g/ {1 j. z - http://www.springframework.org/schema/context
) R; H" a8 n; l2 O - http://www.springframework.org/schema/context/spring-context.xsd
6 [4 v- D4 U6 t6 T/ w( p- s - http://www.springframework.org/schema/tx * l- b- ^9 Z9 h) h5 j9 S
- http://www.springframework.org/schema/tx/spring-tx.xsd">+ L3 ?# x' f X8 X7 `7 r
- <!-- 使用注解方式装配属性文件 -->
) z+ t p0 \/ z* d - <context:property-placeholder location="classpath:hibernate.template.properties" />
' J/ A" ]+ T6 q( a4 [ - <!-- 配置数据源 -->
. T+ M" I2 `: s; e4 u) F, o; T% c - <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">) ?% h. H" w, O& v6 M( _+ ^- U
- <property name="driverClassName" value="${hibernate.connection.driver_class}" />
' [6 K& R. x l8 j' V# J9 c - <property name="url" value="${hibernate.connection.url}" />
8 {+ x J) `- j8 D0 e4 D$ B. T, { - <property name="username" value="${hibernate.connection.username}" />" f- {$ K% V& W* t
- <property name="password" value="${hibernate.connection.password}" /> Z) W/ I! K; t2 p5 n4 W+ g
- </bean>
8 ~) ^. B* N# S4 S: [# y9 F# }& M -
! W: R2 Q& s0 R6 w - <!-- 整合sessionFactory对象到spring容器进行管理 -->
/ Y' l' j$ A1 P0 N6 A - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
& e/ s V6 B* i! P/ {. M - <!-- 把spring容器中的数据源注入到dataSource属性中 -->7 V! }6 O8 n+ Q, K# Q& o* ^
- <property name="dataSource" ref="springDataSource" />
- l+ u+ h' I' j( k! @, ^/ s - <!-- 扫描装配需要管理的实体类 -->$ R% e9 E* ]3 Z- c0 \: c
- <property name="packagesToScan">
* y0 f4 l9 G: c1 [ - <list>
' o3 o7 ~7 W! D' W( V3 E - <value>com.s2sh.annotation.entity</value>
8 w8 a2 ?* b! ]3 Q - </list>
! Z) t1 ^1 R# ?" u) }5 c - </property>
; ?; |: n' Y& Q6 O - <!-- 配置额外属性 -->- k6 Q- T0 I/ T: d5 o, y
- <property name="hibernateProperties">( a: B) n& p; {3 j! G, T
- <props>
. i* ^( [1 R& L4 D - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
$ T( n$ Q) l* |2 }; E& p* [ - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
2 ~ I* j P6 f1 j - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
! y0 U4 B( C% U8 I$ P& ]+ X9 N - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
2 O$ I8 }, V% d) {/ S! k - </props>. _& [- |; Z; M& i! u
- </property>" y, u% \0 Y6 m% a1 o7 [! h
- </bean>9 |6 R1 u1 _! V# f& h( g
- % k6 B. N4 C' p; t# t# V4 A+ J
- <!-- 配置事务管理器 -->1 b6 M/ }) @/ E& a
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">4 n0 k* x0 x5 o2 m* f& W
- <property name="sessionFactory" ref="hibernateSessionFactory" />
3 K, `7 B5 k& z0 v6 G' ^# F - </bean>
/ g n. [# A3 M - ( V* V: V# ?# h2 Q8 m% u7 P
- <!-- 使用注解形式来管理事务 -->
$ N; b6 ~/ H' \ E J" l6 t' Y2 |; x - <tx:annotation-driven transaction-manager="transactionManager"/>7 Z6 g$ Q" W: B2 E/ K
- ) r- |: d8 N$ \8 P3 w
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->
. S% y6 i4 _6 l& y% L7 m; G - <context:component-scan base-package="com.s2sh.annotation" />0 `' H9 ]3 v5 e# a5 g3 L
- </beans>
复制代码 struts.xml:
) V. I2 A( G$ H/ Z1 q- <?xml version="1.0" encoding="UTF-8"?>( W2 w5 m- g8 g
- <!DOCTYPE struts PUBLIC
" R7 ~! U9 n; b$ T6 s( r - "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
9 M$ x7 y g$ a% d; s - "http://struts.apache.org/dtds/struts-2.3.dtd">. h" ], \- e9 `. Y& k" l
- <struts>* E7 ~+ L$ \: H- G# M
- <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->. Z" g+ |3 R1 b$ F
- <constant name="struts.objectFactory" value="spring" />
9 v) Y' x( Y! y! C2 g. m3 v -
, ^; b C W( t/ G - <package name="base" extends="struts-default" namespace="/" abstract="true">4 c6 F) H) O6 g& y9 j$ s3 m- F
-
# `+ k+ n5 r" R8 Q - </package>) N3 H2 M" z: D3 d" p
-
$ ~" o- ]) Q& W" h' V+ B; R - <include file="config/struts-bookInfo.xml" />
4 K( x0 E2 y( m: ]4 a! l - </struts>
复制代码 - x7 V, d$ y" d( E2 f! ^8 ]
S2SH三大框架采用注释整合项目案例
% X. Z- u: T5 p1 l解压码:
+ P4 M9 }- [- N5 r0 s( D) q
* F' A4 a( u1 W f- G9 b
( X+ H- _! L4 y( W2 L+ A. R' n. l& Y2 `
|
-
总评分: 帮币 + 1
查看全部评分
|