TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
" ?) f, y) `4 e; F+ y. Y: q6 q" q% ?
BookInfoAction.java:
- X6 Y2 B7 T$ O9 Y% U- /**, d$ _: j) d4 Z# r; G
- *
T6 r; O, f' N$ m - */# a$ s; f! h, D4 z
- package com.s2sh.annotation.book.action;8 h0 f+ [- J4 s! r* C- `- }" i
- import java.util.List;% O1 ~8 j. J( [9 d" s
- import javax.annotation.Resource;- q) B/ S z5 _ N2 F. `
- import org.springframework.stereotype.Controller;' w: q) E( g' \7 B" f
- import com.opensymphony.xwork2.ActionSupport;; M8 d9 O/ F, `% ?* M
- import com.s2sh.annotation.book.service.BookInfoService;
1 O' I! p3 ^7 } - import com.s2sh.annotation.entity.BookInfo;
0 |# v v* }3 q - import com.s2sh.annotation.entity.PageBean;
) m* j$ T0 ~$ d1 T% e - 6 c+ h% V7 L. X+ L8 t8 V/ H
- /**" M5 H& D/ A' D+ J) s1 b8 s
- * @author Administrator8 {* b# |. j8 J: ~
- *
8 ]5 o/ c7 c! }+ {, g r7 K7 L7 D - */
, b/ ^9 N& _ ` - @Controller // 标注控制层对象
/ N$ ]! l- m3 K3 Y - public class BookInfoAction extends ActionSupport {
+ ]3 m6 b1 [! g) c. ~. E
" l5 z( b t) X2 a- private static final long serialVersionUID = 1L;# D* { y5 K6 u9 W$ d8 q
- ' d2 M. |' @! p. J
- @Resource(name="bookInfoServiceImpl")
. i, u! I* Q3 q1 K - private BookInfoService bookInfoService;* ^( \5 p% Y6 v/ h. ?3 j
-
( p, U% o) a0 W9 | - private List<BookInfo> books;
1 t5 _$ G, Z8 K" O, Y' x4 \. g - private PageBean pageBean;
3 b2 Y& M, _! B: T - private BookInfo book;# V* B& P& h" o, a- `) |8 H; P) q
-
- Y4 {2 c9 m" S, r3 O$ L& ^; r -
& S' O1 `6 c' p( g# [2 l+ G0 G8 g - /**
7 K$ J' e) o+ Y - * @return the book9 g& [" `' A2 L8 T2 V* w I
- */
- u, ?/ Z/ Z0 @) n" D- { - public BookInfo getBook() {. S9 T! @ C) K4 d; V
- return book;
4 f+ y( h7 M: I6 f - }/ O8 g, J, M0 v% J% g4 a$ Y) e
- /**
* p6 E# ? O0 D3 m g+ S - * @param book the book to set8 P2 e) @/ u* O' m3 Y- x
- */; a# f8 T! g/ M
- public void setBook(BookInfo book) {3 R$ j: ?9 ]2 C
- this.book = book;
) l# `; y8 S! r2 s) m Z - }
/ Y8 [7 r, N5 U- Q, d - 8 d- f2 H3 \# C) D% o& v0 z
- /**# I; m# c6 k! [+ V1 ~5 t! C. a
- * @return the pageBean
' u0 n+ x6 T9 @3 p+ O" W# h - */
; X, n' H* m) e# i0 i% t - public PageBean getPageBean() {) ?2 C' J6 y$ A" p* R' X5 L7 c
- return pageBean;! ^1 p' ^9 U. H0 K N/ b4 W; O
- }( \' N/ G9 V- ~% `* K
- /**
# M9 r. ]. d4 c - * @param pageBean the pageBean to set: o: ` _; @3 D
- */( ?7 l( z, ~" s+ ^: a j& d& }
- public void setPageBean(PageBean pageBean) {
+ U' h+ n' U( g$ l - this.pageBean = pageBean;- z3 N+ G8 {: L# ], x+ q0 I ^8 s
- }
& A4 V; b$ @0 p" U! x2 L
( O5 y. k7 x. u6 V% G t! E- /**- P. }/ h$ h# {# T2 [5 D6 ^
- * @return the books
1 e( K: r+ H+ S; ?8 c! V - */' u* ?9 ?2 O2 ~) @( A9 ^
- public List<BookInfo> getBooks() {
7 L+ p1 Y# I& M; F - return books;# ~9 N) U( r# w0 G1 C
- }2 [+ L) _9 {% i' ?; _
]- X" w, e- ^, A; U2 ~9 F, F- /**; Q" Q2 k$ l9 y4 Y
- * @param books the books to set
4 k6 z# k, n! p, n - */+ G8 c$ ~* s; X3 G% q9 S# J
- public void setBooks(List<BookInfo> books) {
: S" T) [6 A# C8 |# o8 z% [. S9 d* ] - this.books = books;
$ x! t6 X' k: `" Y( R' Q - }
: i. R7 [ L# ~3 j/ n% c7 ?" ~ H - ' f. y E+ Z5 _
- public String showBookInfoList() {
( k" k* Q/ R5 e2 A8 k7 q - pageBean.setPageSize(5);" ]. F, l3 X# Z; @
- this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));. s h: G9 l9 A- j/ q
- return SUCCESS;
5 `5 M5 Y) d" r O7 p - }+ _- z1 T. f4 S
7 L) C- u& H$ [5 X/ b- }9 k' b6 q& Z+ ^- X* b1 `" }$ r
复制代码 applicationContext-persistence.xml:$ u( m' J* t! h- X6 a9 L/ E1 T
- <?xml version="1.0" encoding="UTF-8"?>/ L0 n( C, S+ D! _- k6 n4 q
- <beans xmlns="http://www.springframework.org/schema/beans"
- B5 M% n4 D- N6 w6 ^ - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9 J/ V6 Q [. \3 H* c$ `1 M - xmlns:context="http://www.springframework.org/schema/context"
2 k! J7 B: v) e; ^5 D y - xmlns:tx="http://www.springframework.org/schema/tx"
7 O' J7 J( ?" ~# U& \ l% M - xsi:schemaLocation="http://www.springframework.org/schema/beans 2 ]6 F* @* v! T8 @) U/ v$ ~/ ~0 ?
- http://www.springframework.org/schema/beans/spring-beans.xsd 9 A e- V, N8 c3 r' y$ d, j7 L1 k
- http://www.springframework.org/schema/context ( b8 X5 p ]# j$ F) V/ ~6 B
- http://www.springframework.org/schema/context/spring-context.xsd 0 F E- S3 k; g6 a
- http://www.springframework.org/schema/tx
. I' L- k7 H8 X) D - http://www.springframework.org/schema/tx/spring-tx.xsd">
" V/ |& e3 _/ Q& \0 ]9 G4 N - <!-- 使用注解方式装配属性文件 -->' d# n5 T% D1 m2 R8 O3 \
- <context:property-placeholder location="classpath:hibernate.template.properties" /> h4 V ^. g6 x H* K' E
- <!-- 配置数据源 -->/ N+ I# Z& A* n a% Y2 f
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
9 g' Q* p+ r+ ?6 C( ? - <property name="driverClassName" value="${hibernate.connection.driver_class}" />4 o: d. ?7 G, J! M
- <property name="url" value="${hibernate.connection.url}" />; g, H7 D5 A3 K( z1 t2 U
- <property name="username" value="${hibernate.connection.username}" />
" {6 [$ p$ m0 ^6 Y8 Y4 J0 r - <property name="password" value="${hibernate.connection.password}" />
. j2 t- S5 m4 U - </bean>% ^! \5 N0 z0 c+ {" e
-
2 s( `" f' E/ n# n* V( Q, R - <!-- 整合sessionFactory对象到spring容器进行管理 --> D7 j! Y: j# T* Y9 z8 `8 ^1 p
- <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"># V4 T5 u+ u# I; C" ?* a2 D' \
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->- a6 o: _( n8 E; Z
- <property name="dataSource" ref="springDataSource" />" R% S& }' N! m2 k. w
- <!-- 扫描装配需要管理的实体类 -->$ U" T/ e+ b( \
- <property name="packagesToScan">& N8 n9 r! z. ^+ t, c* a2 ?
- <list>+ o2 u/ A- _1 U! q& z4 z
- <value>com.s2sh.annotation.entity</value>) G# [# K s9 s1 E4 X6 Z) m3 j1 U
- </list>
X8 X( C4 z" R% O% T& K [4 b - </property>
4 E& O' z1 H. I- B. G* |& h$ K$ G - <!-- 配置额外属性 -->1 Q+ X, ?# w0 ^! c$ Q% n
- <property name="hibernateProperties">
3 d0 ]+ j; V3 y2 R- r# h9 t - <props>
6 [1 O) M5 }$ Z2 y) \ X+ i! | - <prop key="hibernate.dialect">${hibernate.dialect}</prop>
5 K% H( R. \2 x& d* k3 p0 ~ - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>& R/ r' Z1 r6 I& B
- <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>! W5 o) \1 N$ [/ n7 r$ z
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>7 F9 V/ z, C C, z* f; j& X: Q- q1 @
- </props>8 q- O7 x# K1 X, u
- </property>8 D3 _8 q6 p l9 N
- </bean>! i' H. p1 g G
-
9 \4 V) `: ]+ `+ ~3 T2 u& P" B - <!-- 配置事务管理器 -->
a3 Y) L+ y, C+ `( b) s- n - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
; W; x' e4 b+ n2 O, } - <property name="sessionFactory" ref="hibernateSessionFactory" />+ g- s/ W/ `; n0 J) q5 q( M4 e
- </bean>. n0 h% V2 [1 g- i* w4 v9 t) |
- - s% l$ P9 Q& H% {) u
- <!-- 使用注解形式来管理事务 -->, L3 n' J. F1 |3 x! e$ v% O
- <tx:annotation-driven transaction-manager="transactionManager"/>
# D7 ^# N9 j3 c - 3 z+ F9 w" }' l& ?% R3 M
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->1 t; s: I# T: G8 i! m0 F- }- X
- <context:component-scan base-package="com.s2sh.annotation" /> O' R* `) i7 |3 ^- B
- </beans>
复制代码 struts.xml:& V% w/ K5 e5 a4 `
- <?xml version="1.0" encoding="UTF-8"?>
' x/ [, \: }2 ]: k3 S3 i2 x0 `" m - <!DOCTYPE struts PUBLIC/ h9 d/ t! w! X- t. `8 @+ ]
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"4 e' M3 l* H( w! A3 f- X- ~' B0 t
- "http://struts.apache.org/dtds/struts-2.3.dtd">
; O$ F0 m1 n! r. ?* ^2 [/ ]) q - <struts>
" a. v1 M. S% L+ @# q% M3 O1 V - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
) b8 p7 e0 T% B% q1 h - <constant name="struts.objectFactory" value="spring" />
, ^ F8 A* B/ S; X - ' d' Y+ O+ x; r
- <package name="base" extends="struts-default" namespace="/" abstract="true">: q: `8 ^8 ]7 m$ x1 P
- * @: S4 R( U& r: S
- </package>
`. C: P; V7 |( Y5 l1 X5 \# Q+ L -
8 k! _$ h z( ] A6 m7 R S - <include file="config/struts-bookInfo.xml" />+ a: y: y5 H; d7 q! I! C. L
- </struts>
复制代码
# |8 h" X* u2 j) ES2SH三大框架采用注释整合项目案例
8 S5 i' a2 z5 Q& j) e5 _解压码:
. ~/ c+ `" \3 f4 b
6 b$ L- Z% r; [, D* `
0 P8 d1 b# I/ O, t/ x+ j, c
+ d( o8 w K, R, u/ h4 t7 o1 q) ^ |
-
总评分: 帮币 + 1
查看全部评分
|