TA的每日心情![](source/plugin/dsu_paulsign/img/emot/shuai.gif) | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
+ r) m2 O2 U! Q( s; {* |& w' c7 X' ~$ G, k& |/ i/ v5 }1 ^
BookInfoAction.java:' q6 c8 p( p) A5 D
- /**; d5 p0 O0 u4 F1 K7 f( P) ?
- * 1 P1 l b1 f B: U* O5 s' X& W
- */
* _$ a" a R7 A1 T3 [/ B - package com.s2sh.annotation.book.action;1 t" _) z, y2 Q5 T8 o* d2 }
- import java.util.List;
8 }/ O/ _. T9 l7 g - import javax.annotation.Resource;
5 r% o3 R; d* L - import org.springframework.stereotype.Controller;! P0 s0 C3 y6 n
- import com.opensymphony.xwork2.ActionSupport;
! o- w% T! Z. q, p# l6 c5 E - import com.s2sh.annotation.book.service.BookInfoService;
1 l. W" U5 F: @4 `3 [/ ?: S& L: s - import com.s2sh.annotation.entity.BookInfo;
4 U% O- A5 `6 F# W9 P0 P8 M, B - import com.s2sh.annotation.entity.PageBean;8 W4 K# M7 v2 D& S8 p2 I" P
- 0 j* k8 c4 S1 U
- /**, S6 ^+ k" p/ c' k1 ^& Z: F
- * @author Administrator' F5 U/ }5 D1 R2 ], Z
- ** h0 V* ]& I1 k4 A, r- Q6 m- g
- */: Y+ z7 E& D- ]8 y7 g4 b
- @Controller // 标注控制层对象
! o) g0 X6 T, ]' }4 E1 `0 H0 } - public class BookInfoAction extends ActionSupport {. C* W( H2 P. c' L
- $ s* }( Q* J, S6 v4 K1 i2 H9 F
- private static final long serialVersionUID = 1L;. V& E1 h0 ^; e2 ?6 x1 s1 P; z
-
# N) {( q# f/ ^ - @Resource(name="bookInfoServiceImpl")* y$ c/ l& I" c1 }
- private BookInfoService bookInfoService;
$ ], ] s; e' ` - 6 I3 O$ z. Q2 N9 }# J/ X' V
- private List<BookInfo> books;
# U* o( a3 ?! s; i - private PageBean pageBean;
, v$ K" z3 T+ }6 O/ `. g/ N0 n/ H - private BookInfo book;+ Q6 V6 w# e/ z2 R
-
) T4 F, x* O9 y! z c -
" D" z0 O& R% O# u: n, ^ - /**
) ~/ {0 T' L9 W3 L, m - * @return the book# \% U/ b5 t8 o s: A* j+ o7 Z
- */8 ?% B1 i; N1 ]6 V, \
- public BookInfo getBook() {0 r8 Z! k/ G. t. I
- return book;
; V6 N. B3 F" [/ o+ C7 A' F - }
2 `0 D3 J0 e0 Z9 {$ s7 p2 G - /**& p: ]1 ^ k1 f' W9 U9 g1 ]
- * @param book the book to set
5 y- g! p. a, E - */
+ R6 G4 x8 e8 y4 I* l - public void setBook(BookInfo book) {( J* R5 J+ l1 W2 T7 w+ ^
- this.book = book;
0 d- Q" g8 m* V8 a7 I4 {0 y - } O% ]1 [9 L& K# }/ h$ j% W
- - l. Y% L* ?: `' [1 v
- /**2 [% N/ u$ J- M
- * @return the pageBean/ M0 x7 h- z0 z) W, }3 f9 O7 ]
- */
. C" D+ c5 j' ~ - public PageBean getPageBean() {
3 F& @1 e% B* K7 s' N1 [ - return pageBean;
. E$ ? f: B5 h, J: L6 K - }
# ^( c2 Y+ i- m8 L# N* L - /**
6 _$ N9 u$ I4 }$ b - * @param pageBean the pageBean to set
$ d3 l; m, L' b9 w; o x, a - */$ d0 M# }0 @, a' m q
- public void setPageBean(PageBean pageBean) {
0 O8 H* R$ X0 G8 H3 |' L - this.pageBean = pageBean;
0 i+ h. i" q" t+ D. V9 l8 @ - }4 C, o, M% H2 x
1 a7 @3 i. m. B3 d5 q! N, f# L- /**
2 }0 x" ^! Y/ V1 C* J2 L/ [ - * @return the books
/ ]9 \% n9 Q7 U; a8 T - */
) ]0 h; Z( p# j V4 x' k - public List<BookInfo> getBooks() {
! j, h- ~/ k5 s, F/ F) S - return books;6 N4 |% @0 V Y4 e4 w; h
- }2 l9 ?* X) M* L+ q; q" t/ x
1 G8 [6 \; g9 y- /**
/ U+ L* J4 B! b& T( c. q2 @, A4 K - * @param books the books to set2 ?" g0 G+ m+ {8 G% [- \' w" R- i: ~
- */% x( c9 r7 I: p+ Z6 B- K
- public void setBooks(List<BookInfo> books) {
$ w* r, |8 P! F% g- v" w - this.books = books;
2 L4 G1 n; b2 O* P7 R - }
, C/ I( y* R4 z3 C - 7 l4 }1 Z9 l9 S/ ]7 B4 I1 ?5 Q
- public String showBookInfoList() {
/ C! j* a3 j" @% b - pageBean.setPageSize(5);+ ]. r. Q, `( Q1 M8 O* N
- this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
9 e8 S; I' I" d% d - return SUCCESS;
/ P% z' q6 w" b; e - }+ V4 V1 e3 L1 O' S
- ; |# V6 P0 ~" p( N7 v' A6 i
- }; P& _% Z- ?* x# i% t
复制代码 applicationContext-persistence.xml:9 I. r& Y4 `/ b! q0 x* \- g
- <?xml version="1.0" encoding="UTF-8"?>
- C U& I5 ^7 K( l8 b - <beans xmlns="http://www.springframework.org/schema/beans"5 M. i! D% n/ h; J
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 c/ L9 J0 o5 t' O" e+ J3 D7 Q - xmlns:context="http://www.springframework.org/schema/context"
5 n! _, B1 W9 p1 @ S - xmlns:tx="http://www.springframework.org/schema/tx"
/ r l# C6 Q- r) b$ Q+ a" E8 B - xsi:schemaLocation="http://www.springframework.org/schema/beans : K9 D7 T! e9 z& W" G/ Q
- http://www.springframework.org/schema/beans/spring-beans.xsd
& T Z3 s! A- R3 m [ - http://www.springframework.org/schema/context
& R$ S1 l# ~' G# f$ U - http://www.springframework.org/schema/context/spring-context.xsd - a* s( W e) P4 J, y) H
- http://www.springframework.org/schema/tx 6 c" C1 I5 h& K
- http://www.springframework.org/schema/tx/spring-tx.xsd">
" y2 F: h$ h1 H0 w - <!-- 使用注解方式装配属性文件 -->
5 ~ w6 `& w$ y$ w9 z6 n4 D! u) m - <context:property-placeholder location="classpath:hibernate.template.properties" />0 Q$ \: z( g* j6 F& w, i
- <!-- 配置数据源 -->( U# ^6 L3 W. z! O
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
" D5 E; p' W# U/ A - <property name="driverClassName" value="${hibernate.connection.driver_class}" />2 b7 ?: j2 n0 C) j9 b
- <property name="url" value="${hibernate.connection.url}" />
. e7 _0 h2 m: Z' Y) u- { - <property name="username" value="${hibernate.connection.username}" />
6 k( G7 B: D* w6 I C - <property name="password" value="${hibernate.connection.password}" />, i" k0 R% Z$ Y
- </bean>
) _* n7 G0 P+ R8 x - k! }/ ~& Y, r2 E( h
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
( `* [* Y( ~4 A: c& B$ I" j. L - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">8 b X( b* @' b; q
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->
. o' x: S8 i4 I+ p# H/ Z# i% C - <property name="dataSource" ref="springDataSource" />$ ?- V, e% ^/ p! O$ ~4 W' j
- <!-- 扫描装配需要管理的实体类 -->
( r1 H) K8 X2 g/ X, ] - <property name="packagesToScan">
0 K" h. R( |; t8 \# Q1 \ - <list>
7 z, F4 ]% B+ Q& u6 O F, f - <value>com.s2sh.annotation.entity</value>
% ~8 g% B2 T9 \& ^$ ~7 n - </list>
+ Z/ J3 P# y0 b5 H: l6 C, ~+ W - </property>, L7 a" a' j( x. `
- <!-- 配置额外属性 -->! T# s% \6 [9 M
- <property name="hibernateProperties">
# B T4 Q+ @ H, ]% L6 f - <props>; B; d' o, {+ D( A* j6 f
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>6 c1 B8 k& m! B$ `, L
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
6 X( Z: i" ?3 Z3 Y - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>0 A& B& }* R9 X2 T9 s" M
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
% Z: v% {/ Z8 w' H9 e. \, Q( F - </props>$ G7 {( H5 m% I0 a2 I2 ]: f
- </property>
. K3 o; B/ g: W) e3 S/ K' q# O7 O j - </bean>2 g8 U' d* l, [( i, i, b' l1 x
- : R& E+ U- D6 R* N- o7 V
- <!-- 配置事务管理器 -->
! j1 j" ~- k6 A- }$ v - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
. l8 T$ n! B& B" L/ J" \ - <property name="sessionFactory" ref="hibernateSessionFactory" />) |* F( Y4 W7 q1 e
- </bean>
7 s" L8 d) K/ m2 r# o) R! w, o# U -
' ]1 y- _- O4 y5 I9 X- ^, E - <!-- 使用注解形式来管理事务 -->
& b; P6 a8 s# b2 U" E - <tx:annotation-driven transaction-manager="transactionManager"/>: K5 I7 F$ _. p1 j
- ! x4 X& w: `& b8 Z" u
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->/ n% v$ G' {/ Q% }6 ]' C% a& O
- <context:component-scan base-package="com.s2sh.annotation" />1 y V+ V; A% k# @! y
- </beans>
复制代码 struts.xml:- | ~: C% Z+ V
- <?xml version="1.0" encoding="UTF-8"?>' u& b i# b0 u7 _' W6 g
- <!DOCTYPE struts PUBLIC; G/ \( h4 s: @8 H+ ~' i5 S2 W+ k
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"9 W2 u. _0 y% O6 I8 r! J
- "http://struts.apache.org/dtds/struts-2.3.dtd">
9 F/ {) M4 s- [ - <struts>
7 r/ U7 E; e3 L( j6 G4 y& @ - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
4 c6 R) [8 E9 M" O3 @$ K+ { - <constant name="struts.objectFactory" value="spring" />
' ~4 A, [) b" d2 \ -
) I2 J% [: m8 I- u - <package name="base" extends="struts-default" namespace="/" abstract="true">/ y9 N" t% T6 a6 C4 J3 o2 j
-
/ P: R1 Z8 G& b7 l - </package>
$ _5 l5 Y) Z% s8 F/ V1 d$ z -
4 C, i/ [* m" H$ C - <include file="config/struts-bookInfo.xml" /> F& G: Z a' @5 ^9 i& y
- </struts>
复制代码
1 j% B3 |2 w3 | m2 l3 G- i5 @# _S2SH三大框架采用注释整合项目案例
1 m" C" [: `8 k2 o# x$ @6 ^0 q解压码:- I, L i! U; D* \+ y
9 Q$ j8 [7 Q4 Q5 V7 } H
6 b2 x {1 I+ {' e- c$ v T: A6 {% Q& p: q$ {# X
|
-
总评分: 帮币 + 1
查看全部评分
|