TA的每日心情![](source/plugin/dsu_paulsign/img/emot/shuai.gif) | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
W$ p0 w; X9 g4 r6 P" O% ?* Z: t+ D; ^% h0 Q3 f9 l
BookInfoAction.java:1 P3 S3 C. k; C- F+ {' U
- /**
, S% U+ r) P, }- }/ j/ h# a- S: V - * 4 G& K4 k3 z, t4 U
- */
- n+ Q+ T1 E( p+ A0 s$ p - package com.s2sh.annotation.book.action;' _8 P" _% x; C8 s4 S# O7 l7 U5 b
- import java.util.List;; q8 a. t& Z- Y
- import javax.annotation.Resource;
5 E! ?8 M% R: ~2 l+ H. c4 E* V - import org.springframework.stereotype.Controller;
% D0 w2 ?& \; C) G - import com.opensymphony.xwork2.ActionSupport;0 T" D+ n/ O8 h# F: o
- import com.s2sh.annotation.book.service.BookInfoService;3 T4 \( ?+ R) Y; M4 J3 h5 |
- import com.s2sh.annotation.entity.BookInfo;! @0 P" Z5 h; f* a) O+ U3 e" {
- import com.s2sh.annotation.entity.PageBean;
" r! }/ Y5 x l! T6 k% W. f/ I
1 ]1 Y/ g2 @ m' d6 C- H% E6 O- /**7 L. K$ v; O* m! L! C* m
- * @author Administrator4 R5 y5 O0 F0 p# n$ j* t
- *
! Y( s+ d7 W$ A- ?7 i% P( z) W - */# H* ]& q, F- G8 N. p
- @Controller // 标注控制层对象
1 P) m# B7 d [# D7 z, L/ f. y$ ]- y - public class BookInfoAction extends ActionSupport {
' x, K/ k* J: C* T" @4 c - " d( ?, c; E' H: I; K4 T7 |- ~* L. W
- private static final long serialVersionUID = 1L;, c1 y0 w% I: W$ ]" t# B g0 Y" Q
- " a1 h0 ~9 f- U/ ^% Q) N) |: d9 A
- @Resource(name="bookInfoServiceImpl")# T' _: F! ]4 T4 ^9 e$ R0 x7 U
- private BookInfoService bookInfoService;
% d9 j& X7 \4 w- G -
: k/ q0 N5 O" K n0 h - private List<BookInfo> books;8 W) V1 ^& D( V H& |' a
- private PageBean pageBean;
4 q. O9 v6 c# P - private BookInfo book;
3 a" L1 d L+ d$ O- E! r -
) h2 X7 ?8 k+ w% m9 c l - % D9 ~9 u; U. c, H+ B& P
- /** {2 x2 K7 e/ ]
- * @return the book- ?, F O2 d( A
- */
8 g3 e6 @) o' }( l; n$ a# E - public BookInfo getBook() {" E9 K+ F3 z" G* x L1 v1 A8 j
- return book;
9 g+ W( ?; N/ D9 p6 f - }
/ E$ v' _; T Z4 y# Z - /**0 L3 ^( |8 [. y9 {6 h8 b7 D! }
- * @param book the book to set
( t( |. i1 ?1 { - */
* M3 T4 V5 d( z1 D3 y - public void setBook(BookInfo book) {
" A; X a& A' B( J% }$ W- C) J - this.book = book;( Z+ D2 ^6 V1 b4 e
- }& t7 q# ~( T/ i( Y5 q
- * K- n; E2 ?1 h6 M. R
- /**
7 v9 H; k; g0 Q - * @return the pageBean. Y) H; n5 U# r. t2 U. t4 J
- */, x# D n8 N" x; w; c6 b1 E
- public PageBean getPageBean() {
, ^2 X! }4 M9 H" J$ g% r' I1 U - return pageBean;
# o! T( |0 {* ~, W: l7 V - }/ o6 t: g8 u( k, V
- /**' H$ g' y/ } V; ?" v
- * @param pageBean the pageBean to set
2 v; }, a/ `$ I2 s - */
! @5 ^$ y% d. D* L5 p0 V% ^ - public void setPageBean(PageBean pageBean) {
8 b' u: N5 ]' }: j1 p2 x - this.pageBean = pageBean;- A, J5 I |' @4 l/ a
- }; i5 \& u1 M# \ @3 `+ M& J7 Q
- 4 x: `, q$ U w. q/ j) W p1 i
- /**+ a! Q4 `1 ], N
- * @return the books
* B) H9 a( r" j8 v9 f# `: w4 h0 w - */
0 Y" Y) D/ w" a$ ]6 R7 y' g( i, P) I& I - public List<BookInfo> getBooks() {! M1 ?* c& X' l( X* D( N0 q9 ]+ R, r4 M
- return books;$ ^ m' S4 j! k8 O
- }' t; o0 y9 @2 k' \; e4 l0 ?, |
- . F* j# L& d8 j
- /**
% ?, M4 q% a& ]5 a1 W - * @param books the books to set
( Y8 j, k2 r Q8 W# a/ H2 n - */
+ P# }) W5 W2 s4 u5 L - public void setBooks(List<BookInfo> books) {
{' Z) v9 }/ t. u: p# N+ ]- v6 W# Q - this.books = books;3 }1 |9 V# r3 R! \
- }: M+ L6 z$ q8 A% C8 B) K0 P! a) Z8 u
1 m, V, F& Z3 q+ c, o- K- public String showBookInfoList() {: x" p6 o$ N7 m r2 U! C: g
- pageBean.setPageSize(5);
- P. x. f$ k! `5 A - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
8 [* q% u; U2 @3 z- i$ G' }+ L - return SUCCESS;1 |8 h, ]- L. T0 [* o
- }
( f# m- V) a. K- H5 `+ c" H$ Y& S
( O* E, M3 Z& J5 F8 |- }
/ w9 W$ U/ c8 T: h( }
复制代码 applicationContext-persistence.xml:
4 S* X. A* i+ c! H- <?xml version="1.0" encoding="UTF-8"?># e+ \! S5 L' O& u- j
- <beans xmlns="http://www.springframework.org/schema/beans"/ w8 _$ B" j G. d$ N
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 M4 C. o) b+ R* ] u2 m8 n - xmlns:context="http://www.springframework.org/schema/context": I4 Y; J* H3 l
- xmlns:tx="http://www.springframework.org/schema/tx"
l' q6 i8 l5 C1 R* g - xsi:schemaLocation="http://www.springframework.org/schema/beans
& B+ i- P3 l6 K, X. V - http://www.springframework.org/schema/beans/spring-beans.xsd * H1 g+ p5 M/ \& z+ a
- http://www.springframework.org/schema/context - G+ k9 i! E' o( |
- http://www.springframework.org/schema/context/spring-context.xsd 9 b( |7 q2 K; y" P) k
- http://www.springframework.org/schema/tx
1 z' k0 w- s5 r# I: b% _. u$ n2 Z - http://www.springframework.org/schema/tx/spring-tx.xsd">
3 ?: w* s; Q2 q - <!-- 使用注解方式装配属性文件 -->
7 N ]+ u- Y5 F: Z7 L( L7 v5 V - <context:property-placeholder location="classpath:hibernate.template.properties" />
" h( G5 k7 N w7 M; o - <!-- 配置数据源 -->0 a) N+ N( i0 b$ a5 F% c1 O
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
4 x4 x5 d3 T( e - <property name="driverClassName" value="${hibernate.connection.driver_class}" />
! ?9 Q' \% M) t& v" d' W - <property name="url" value="${hibernate.connection.url}" />6 {) _- I; L% d' h+ j% p# N) A, q
- <property name="username" value="${hibernate.connection.username}" />
. C) ]6 k1 c; C- O2 @. ]) @( b - <property name="password" value="${hibernate.connection.password}" />, Y- i2 r' l8 W; C3 F, z1 o2 i
- </bean>5 ]9 m) [1 W8 |, J0 f1 G
-
. V" [: ~ t7 h2 f) x - <!-- 整合sessionFactory对象到spring容器进行管理 -->
0 S/ Y8 G& H7 n/ T# O% N3 J - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
8 d' c) P3 \0 U4 F - <!-- 把spring容器中的数据源注入到dataSource属性中 -->: v' m$ ~& w8 N. n3 x8 B8 U
- <property name="dataSource" ref="springDataSource" />
$ f/ u4 [6 J6 A0 d! |! N. o - <!-- 扫描装配需要管理的实体类 -->
& e" M" a9 f5 S; ^. ?" @% S - <property name="packagesToScan">
! `) }& J/ d9 \7 Z0 _. h) J# G - <list>
& d2 j4 x" Q8 J - <value>com.s2sh.annotation.entity</value>
$ `5 ^5 S: D- k; p2 N' Q - </list>7 C( v; G6 P+ f7 S4 M, j
- </property>& u3 S4 A D' R7 X( ]$ Y" E+ v
- <!-- 配置额外属性 -->: g# U% x1 z" G1 I" I' a5 b. h
- <property name="hibernateProperties">
0 ~. Z0 a. F7 V1 ?5 ]8 p: ^! O - <props>/ D$ B2 r9 Z( s0 `
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
/ M/ A& \3 Z& ~; P2 M2 A0 A. d - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>+ T% K$ p: W( K6 T* W+ ~2 ?( U
- <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>4 G1 E0 M$ n' D! E" l0 G/ ]/ r; G
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
8 t- D5 G3 o, O# r9 J( I - </props>
6 U$ Z% K0 t. }+ B - </property>8 L' C" T$ P n
- </bean>% h+ _# g; `! f0 N% t: V
- , z/ g9 I, i7 g9 J, u6 E3 J
- <!-- 配置事务管理器 -->% H& b% N' s* q& s( w1 J; g4 V
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
q& Z& ^8 b: c - <property name="sessionFactory" ref="hibernateSessionFactory" />+ [0 W, a# B: R- ~( d& u) W
- </bean>! c9 s: Y/ I9 h- c% i0 {
- . d7 b" l1 r& \1 ^0 ?
- <!-- 使用注解形式来管理事务 -->
- p5 Z# h4 r* [ - <tx:annotation-driven transaction-manager="transactionManager"/>. f8 A5 a) W$ u2 d* w
- : `0 J1 h6 A" D
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->
. l8 F! o6 R) n% ` - <context:component-scan base-package="com.s2sh.annotation" />! \& `- |# V9 ]% F9 O/ f! i/ K" C
- </beans>
复制代码 struts.xml:8 _- D" K; K$ s6 q# f
- <?xml version="1.0" encoding="UTF-8"?>& F* H. Y2 m+ g; ], n8 v: z
- <!DOCTYPE struts PUBLIC
# q1 {5 j. Z. |9 `. x# u - "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
v4 M! W# {+ P2 S) S - "http://struts.apache.org/dtds/struts-2.3.dtd">
' h( {/ L9 t8 H - <struts>
" A* C% |. ^" \1 s' A) o8 t - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 --># P# j, R9 u0 P% l8 Q
- <constant name="struts.objectFactory" value="spring" />
1 G# | n9 u* K -
5 G# n/ `9 p9 h! t - <package name="base" extends="struts-default" namespace="/" abstract="true">3 ?6 u7 V7 G. g# E- @
-
8 N( h3 J1 I: {) j5 u+ d - </package>) E8 j( F- B. W& J* ^ {, J
-
+ H2 i- P4 e$ U9 j* c6 l0 ^ - <include file="config/struts-bookInfo.xml" />9 Q3 ?/ Q6 V! h. ^2 @( p0 f% r
- </struts>
复制代码
+ S; X9 D8 v' Y. [$ i0 n/ ]S2SH三大框架采用注释整合项目案例
! {2 n- E1 t! Z8 z' D6 H; c; f) _解压码:9 m) ^+ |1 t) i" ]8 e
! a$ j" x; c# K+ {2 J9 k% ?, q
$ ?0 H0 m# N& M/ I. ]' E" P% v2 j R2 V* i: m6 o
|
-
总评分: 帮币 + 1
查看全部评分
|