TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
+ D* V1 M5 j* K$ R/ p! l" w! k+ M) ]- P
BookInfoAction.java:$ }: d8 R9 I5 ~+ R3 r. }$ u( R2 {
- /**
4 g: @; l5 P2 i$ ]2 V - * * o+ X8 C# Y' x
- */6 ^. \: i9 ^! I3 j/ P; h2 e
- package com.s2sh.annotation.book.action; z0 \* Q) v [7 T
- import java.util.List;5 B0 m2 E; h. A1 F
- import javax.annotation.Resource;* c- k- D1 F4 w) k1 p& A$ j
- import org.springframework.stereotype.Controller;
, H$ X9 H# T/ Z$ T( s! P/ ~6 \ - import com.opensymphony.xwork2.ActionSupport;
! O2 e! M2 n% i+ V' ~6 { - import com.s2sh.annotation.book.service.BookInfoService;5 Y1 h8 g- L# ^
- import com.s2sh.annotation.entity.BookInfo;: ^& W3 x2 ^/ s! {5 _
- import com.s2sh.annotation.entity.PageBean;- F6 H2 E9 G4 v- g0 |
9 u3 [2 J3 K2 T# \- i$ e- /**
/ V) \7 }+ \( c# b: b, H - * @author Administrator
( E1 n( r8 t" v( P# ]4 m - *
! W! S6 h: L- j1 V - */& v; Y, |1 g& ?& z- F+ d* ^( T
- @Controller // 标注控制层对象
# J2 W# p [) ?5 I - public class BookInfoAction extends ActionSupport {
5 D: {8 u; U7 W - 3 S% W( p6 z4 W$ F0 \
- private static final long serialVersionUID = 1L;- N, l, n# w7 P$ c/ V
- " [+ n7 f" Y9 O& Z% Z3 j: s: Z
- @Resource(name="bookInfoServiceImpl")1 d: R0 B b) X9 x+ `1 v, G% x
- private BookInfoService bookInfoService;
# t9 D% w0 ~" z, P7 \: n% V+ e7 e) g7 N -
3 M) H2 O3 X3 i; B% w - private List<BookInfo> books;
5 B$ D1 p. j8 w; i+ \ - private PageBean pageBean;
* L {& H9 Z8 f! r - private BookInfo book;
6 G" o8 y" Y; X# @0 s; f - 3 ]: e- g' x# I( L$ A3 U0 b/ y+ u
-
# I0 j7 d0 J2 c7 l2 f - /**
/ u4 J" C3 N; O3 N0 g- ^' B- D - * @return the book0 p7 z: I0 O0 S# `% q5 z
- */
( w1 i- h3 B) \0 r, Q - public BookInfo getBook() {
6 F$ Z! A, R0 z - return book;+ E& a$ X" `0 d7 d
- }
6 }. R; O" [8 B6 ?) z7 u - /**
& U/ l# z2 r! K5 m" b6 G# p( g8 w - * @param book the book to set J4 U; Z& Q9 M8 h5 Y
- */
7 `0 f6 O Y8 p+ F, Z4 O - public void setBook(BookInfo book) {
2 O9 M/ B9 E2 V/ I0 C Z* j - this.book = book;8 B7 W& ^& I: H& A& Q7 }& L
- }9 ^2 K# l# E* ~- o! Y5 p0 u
- 0 G0 g4 R2 R3 P) V
- /**
b" i2 m/ [+ g - * @return the pageBean
' f/ M% k4 G+ @0 g6 B; { - */+ H z; ^; N+ F, E7 x. L8 K
- public PageBean getPageBean() {
8 m3 \# i" \; V% k' B4 H - return pageBean;8 q$ R8 X S* V1 q( x
- }
$ N0 ]$ E+ j0 N! z0 b7 V: q - /**5 [. F7 q1 `# P6 z9 g
- * @param pageBean the pageBean to set! M. z9 y y. Y
- */
2 @8 z1 R3 d6 y! w! T - public void setPageBean(PageBean pageBean) {
( n* v! r6 {) `/ l& r1 K - this.pageBean = pageBean;/ ~* j! T8 j& U
- }
! E3 Q8 D2 q4 K
3 w% v+ S, m8 z! r G- /**
* x& @8 U( f6 x: R. I! o - * @return the books! c3 [6 m$ l6 _: \
- */
$ l3 D, }+ {& D - public List<BookInfo> getBooks() {9 S9 b$ X- L2 |- _+ O4 o# `
- return books;. N, I. [" a$ @! D
- }
. g$ r! Y% y, j# u- ?; W - 8 G8 W% f8 B8 h: a4 U8 N" Q. N! K
- /**
) l# }$ Z1 J9 D. i" K K/ _: m+ y - * @param books the books to set% w0 o+ n, K: E6 |+ I/ v) Q- w
- */
3 ]( r( O$ o" M: L9 H( R - public void setBooks(List<BookInfo> books) {$ [. t0 S6 b" f
- this.books = books;4 \& m# }: |0 @) R
- }
8 P$ G" N& _( v4 t# r
1 Q$ p9 ]% }1 l/ i! Q, a- public String showBookInfoList() {8 Z6 i. M4 |" Z0 E' F2 R
- pageBean.setPageSize(5);% g2 D* O- c# H4 [5 i# A% r5 p
- this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));9 v3 z3 ~; N1 \$ c; N
- return SUCCESS;9 u0 _8 f5 E( ]
- }
: h( g! P& b9 \% [2 ?5 O - & U( L% y h* I# Q4 G7 b3 @
- }0 Z& M/ F: k, S9 r" C. N& o
复制代码 applicationContext-persistence.xml:
8 W' M. w8 d, a9 S. k# Y- <?xml version="1.0" encoding="UTF-8"?>) {, s( x$ J# {% l3 p( H
- <beans xmlns="http://www.springframework.org/schema/beans"
9 X) [. M0 P* F$ e - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/ ^; E9 a9 V9 [2 m& q
- xmlns:context="http://www.springframework.org/schema/context"/ e/ E& I5 ^7 \( N( U9 c6 ?
- xmlns:tx="http://www.springframework.org/schema/tx"
; |6 Z \. Z3 ]3 O# d, M, G - xsi:schemaLocation="http://www.springframework.org/schema/beans - @; H. v, p0 B/ s6 }- M
- http://www.springframework.org/schema/beans/spring-beans.xsd
; ~0 W2 j" l4 c. Z$ o _" E: v - http://www.springframework.org/schema/context . ?/ Q9 j4 e) V9 e3 m! _1 |
- http://www.springframework.org/schema/context/spring-context.xsd
% L- C5 W4 p! D* H! k. v# \ - http://www.springframework.org/schema/tx ) f$ Q/ _! G4 I& c- Y
- http://www.springframework.org/schema/tx/spring-tx.xsd">
: n$ {+ V: l$ L - <!-- 使用注解方式装配属性文件 -->
3 Y7 h& q0 B0 Y/ E$ b& n/ V: B- } - <context:property-placeholder location="classpath:hibernate.template.properties" />
# G+ U3 t3 g) c' y - <!-- 配置数据源 --># x: H. a5 n+ \% ?8 ?
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
2 {' Y9 Z& r2 C2 {9 f6 j; \) B - <property name="driverClassName" value="${hibernate.connection.driver_class}" />/ V* m5 M" N* Y( N% M
- <property name="url" value="${hibernate.connection.url}" />4 Z5 D/ {4 c& w) V
- <property name="username" value="${hibernate.connection.username}" />+ C& n, v' C+ B) ^" B1 a, e
- <property name="password" value="${hibernate.connection.password}" /> Q! b; g4 M9 j8 d& o0 ^6 e: Z6 g
- </bean>- e# ]! L9 r. v7 H
- " L0 Z8 |0 v: e+ n" B& v
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
- u/ I; E3 `1 k; c5 v# G' D8 R8 j - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> C) n! H9 |( S( a9 a
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->: j3 u. ^& d" M
- <property name="dataSource" ref="springDataSource" />
" ~/ @4 {8 w5 W4 \ r" y, p - <!-- 扫描装配需要管理的实体类 --># u3 F! H3 T1 [' n- d+ {6 E
- <property name="packagesToScan">
- w0 W; N4 Q2 S7 U - <list>
b) G1 O! M" k4 ^4 Q. \4 e$ N - <value>com.s2sh.annotation.entity</value>0 r$ ]/ T* p8 [% [* Y
- </list>
% Q$ ?+ q* W* g/ x% O2 E - </property>3 d4 C1 ]) P) t N! W2 L
- <!-- 配置额外属性 -->
: o# t3 A' v) ]0 v+ z. l; q - <property name="hibernateProperties">6 G% g5 w# G4 x h' B0 a
- <props>) e2 s- g9 X# h) C( B6 g: _6 L. q
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>0 a' S( m: g! w: J" O5 d% m+ v
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
, i, v% `: ^" \; Z+ `; _% W) W - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
3 j4 {# j1 S! {, \5 a - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
) M8 P% r( G& x1 m+ u4 w% e& x - </props>5 C& W; u' _; p% Q- N% A
- </property>
* r4 N w8 q. O7 V" t# i/ w6 M - </bean>
- C+ P( q' |7 G5 M* R - 0 G, c' V+ J7 A& R, p/ ]+ s5 ~
- <!-- 配置事务管理器 -->
, U4 h' _9 I7 Q& i - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
' O" K N! y. E - <property name="sessionFactory" ref="hibernateSessionFactory" />
- H9 q9 ]" }3 m2 K; c - </bean>
4 M2 X* v1 j% g; ~ -
6 n1 n/ [8 N; {2 X- {$ n - <!-- 使用注解形式来管理事务 -->
: E9 V" q) a+ e3 q( Q7 q5 n - <tx:annotation-driven transaction-manager="transactionManager"/>, ]& I6 Z9 {6 `4 ?+ C. A4 J
- / a5 _( f+ ~! k4 U4 O
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->: u6 }4 V g6 L
- <context:component-scan base-package="com.s2sh.annotation" />
7 Y2 O$ t* w, p9 p% F - </beans>
复制代码 struts.xml:+ U1 [ b, l. ~& v) [
- <?xml version="1.0" encoding="UTF-8"?>: r- K* z$ Q# L4 k- C) E
- <!DOCTYPE struts PUBLIC
4 @: G6 ]% D6 B& ~) A& N, C9 W - "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
# D9 i+ \# s4 z7 s- }+ T& u - "http://struts.apache.org/dtds/struts-2.3.dtd">
: V" G0 _# E2 k7 W7 Y2 K6 g& x& M - <struts>
1 H, S/ Z2 i) O7 c- q, l n# s - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
) m# J/ W9 L) L1 O# V4 o$ \: ^- V - <constant name="struts.objectFactory" value="spring" />
8 `: w% A8 h3 v - ( ]/ J# Z4 u7 b R( S
- <package name="base" extends="struts-default" namespace="/" abstract="true">
( }( M+ W0 A4 w - + \) j1 S3 {- ?& J
- </package>
7 {5 m" H- m# w8 w2 P -
. Q C& L! f' r3 }2 Z5 n7 | - <include file="config/struts-bookInfo.xml" />
) v3 z( {. Z k4 u: S6 o" G# X3 @" N - </struts>
复制代码 " ^9 p7 I$ \+ j
S2SH三大框架采用注释整合项目案例" F4 v( M, x4 Y
解压码:: j, A) u5 c( p4 }# s
: t j) n) X4 T- w. X6 G$ I8 n
G: _) z& j4 V' U) C; x1 Q9 N; a5 } |
-
总评分: 帮币 + 1
查看全部评分
|