TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
6 e! `3 A2 I! G* h9 V+ C8 \1 {$ f* m4 |! s, W
BookInfoAction.java:
7 A) Q5 C" w9 G( ~8 M. e% S o- /**
8 N2 Q) o5 n# h& y6 A, x4 n - * - Y9 l7 O7 ~, S2 B9 `+ Z& l
- */
) a) x" {1 ] G1 y8 j - package com.s2sh.annotation.book.action;
/ H$ _/ Y; X, r' x/ s - import java.util.List;3 F/ b$ ~5 T4 x
- import javax.annotation.Resource;
8 M) t; W, Y9 n5 ]; \, g - import org.springframework.stereotype.Controller;6 e e. I, \" S
- import com.opensymphony.xwork2.ActionSupport;$ a. ]4 q2 y/ ?" @( d$ N+ a$ ]
- import com.s2sh.annotation.book.service.BookInfoService;+ r+ f: h9 S# p% n; v( `2 e
- import com.s2sh.annotation.entity.BookInfo;
* _8 r+ Q0 ~. V; z$ ]0 V1 o; K - import com.s2sh.annotation.entity.PageBean;5 E' \ S# m' F5 h$ S
- & b" s0 U- z" ^! o
- /**" i" t* E5 r; H4 }
- * @author Administrator
7 O" a# I0 Y7 s% D - *& g: @% h( e: g5 u5 x5 H% \& Z) U
- */
1 g1 r5 ]& l/ X - @Controller // 标注控制层对象
. A$ G. p; E# n% ^- `* {6 w - public class BookInfoAction extends ActionSupport {+ w5 X/ L, O6 d/ P$ x
5 g# z9 g y3 o/ C- private static final long serialVersionUID = 1L;* d' j- ~. O# f# s
- # P( m4 Y1 u9 l7 y$ J" e8 \
- @Resource(name="bookInfoServiceImpl")
3 w. [+ h5 z& ~+ z6 v - private BookInfoService bookInfoService;& W5 C( @. I+ f
-
( d1 \) j. b( _: v+ M7 Y9 k+ S - private List<BookInfo> books;9 @8 e* }0 V, M7 P1 K) H8 M4 R; [+ v
- private PageBean pageBean;% g) J/ s! g( I9 V) v
- private BookInfo book;# f( A: `. r, C) h) R
-
( j" n3 o# q9 I8 }2 r7 I; c+ l0 h - 9 r7 g' F) K4 z) |
- /**9 y2 O3 j3 w) B: z- N: t
- * @return the book
8 Y4 q* A3 u6 ^: S! G: O/ X4 Z - */1 e/ a V" S" x; Z( S4 v
- public BookInfo getBook() {
3 A( E7 G% r* V* g5 p1 _5 t - return book;
9 U5 s' k4 y: _3 ~6 A1 i - }" P. `6 T# ^! D6 k' T4 z
- /**
8 ?4 a( O% I3 R- H, y - * @param book the book to set$ c& t8 T; L: x# ^; B
- */+ q" }# _ ^4 A s9 `# J" n
- public void setBook(BookInfo book) {
! k* |5 h, ~) c, K) U: {! e$ \( p/ [ - this.book = book;0 F2 _, w f. M+ e9 d7 b
- }! h6 n% G4 N& j9 u9 L; T
- 5 @$ a: j1 E3 y' l& H+ s
- /**5 s& T2 G0 c7 F0 U7 S3 z% w" U
- * @return the pageBean, {3 E) E1 B F
- */
7 u+ a! [8 k; r8 F I! g/ f - public PageBean getPageBean() {. I# A- H, d1 k+ `" `
- return pageBean;5 _, h8 ^. X7 ~
- }
. V& ^: O! M: f! m3 A- y4 | - /**
( e% g& @1 v+ y" p8 `5 p - * @param pageBean the pageBean to set$ B2 q# t/ E7 r4 j
- */, s$ [; R) D; ` T) h
- public void setPageBean(PageBean pageBean) {
+ }: r2 D; ]! I2 _ - this.pageBean = pageBean;
: V5 G/ Z8 O5 [( |, K2 e - }$ S1 t. R& ^4 |* ?' |0 }! \: d
0 l# G/ ^5 i2 D3 C9 X- /**
/ F9 }4 F8 V' C+ R. Y. ~ E7 \2 \ - * @return the books" g# P% `1 {( G" [- a
- */7 x: g {. d2 C' ~
- public List<BookInfo> getBooks() {! ?; B9 ]0 ~( T5 b
- return books;: ?7 ?2 @: f, D+ _9 t1 z7 i
- }9 l/ E* j3 Z7 K# M/ K' N6 P: m1 O
+ x" R0 F, S& k& G, d- {8 e- /**
; ^0 A7 Q% P1 R2 h$ r; U - * @param books the books to set7 ]0 B, B& P- z& d, h$ C. X
- */
6 {& {6 ^9 W+ a, t: f5 C; p - public void setBooks(List<BookInfo> books) {
. S( F9 l5 v% Z- ^ - this.books = books;
% t6 a! }7 m- \$ j0 J - }: O8 c8 G! q5 ]( H8 ^+ T
' e" M' S0 |0 k- public String showBookInfoList() {/ I9 K6 a: C1 V. o, w
- pageBean.setPageSize(5);
' {% J8 O7 c$ y, { - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
7 \% T) e$ X" V- t; n& \! y - return SUCCESS;
) Q" n6 A6 J# ~ - }
3 g3 _9 _* {+ ^# `
6 ~- }$ R# T) P$ B$ F& o5 |- }
& ~8 D* d% N- T; A! i- c
复制代码 applicationContext-persistence.xml:; y- ]& k1 q, h4 ]- W- o/ A
- <?xml version="1.0" encoding="UTF-8"?>+ e/ ?. n' q4 B$ o' }
- <beans xmlns="http://www.springframework.org/schema/beans": B2 F0 o6 B! n
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3 S" x3 n% y8 ^4 H1 l. y3 a/ ~ K2 L
- xmlns:context="http://www.springframework.org/schema/context"
0 C2 v; o x3 t0 R2 y' { - xmlns:tx="http://www.springframework.org/schema/tx"
6 w5 D3 O; P9 L0 P' d8 o - xsi:schemaLocation="http://www.springframework.org/schema/beans
7 _$ [5 o* N9 B# W! |' z - http://www.springframework.org/schema/beans/spring-beans.xsd
# G2 {- U' |% V1 R! W: W - http://www.springframework.org/schema/context
3 N5 ~$ m' @4 s9 {6 F4 H - http://www.springframework.org/schema/context/spring-context.xsd " o( L, n( v- P
- http://www.springframework.org/schema/tx , z* r! P, g3 f! } n- ~ C4 F; `
- http://www.springframework.org/schema/tx/spring-tx.xsd">
+ L3 V. V# N% m& U- S9 F. h, B - <!-- 使用注解方式装配属性文件 -->
' l* f/ h6 V' D) T - <context:property-placeholder location="classpath:hibernate.template.properties" />
0 T5 S) _+ @) v4 J - <!-- 配置数据源 -->
+ T* H Y* o2 b6 I. Y m - <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">" h! P9 }: g9 S' ~6 Z6 i
- <property name="driverClassName" value="${hibernate.connection.driver_class}" />
" d8 N$ T* t( f* B - <property name="url" value="${hibernate.connection.url}" />
$ Q: K; M+ @1 N! U$ H9 ] - <property name="username" value="${hibernate.connection.username}" />: y/ F" Q, t, L0 B5 z' n
- <property name="password" value="${hibernate.connection.password}" />
3 U+ e: ?" P/ u - </bean>
6 Y) e: n0 q. ~# c -
7 Z* m. ], t: z z. ` - <!-- 整合sessionFactory对象到spring容器进行管理 -->
8 K2 ?' }) P; R7 q - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">$ }" h4 _$ R, p/ Y0 p0 l& T
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->
& o2 e2 Q% H8 S' }! Q, H& E, C - <property name="dataSource" ref="springDataSource" />" g( A A7 C+ h) B" V8 S
- <!-- 扫描装配需要管理的实体类 -->0 Y' X2 p. Q" s1 u& {2 j
- <property name="packagesToScan">
5 w$ x! }" |- i1 K - <list>
2 v" Z c) T* T% u# f" O( Q - <value>com.s2sh.annotation.entity</value>% C4 c/ I- {* K5 R9 K# ?; a# L" H
- </list>
) l/ d5 a' E9 y* D - </property> S) x4 P% u, b1 ?" O1 u% V
- <!-- 配置额外属性 -->9 f8 l& l% v. |1 U, K
- <property name="hibernateProperties">
8 j. r; j; U# S% U7 @/ g) d4 n, ] - <props>
# Y& |7 \' m, S& U - <prop key="hibernate.dialect">${hibernate.dialect}</prop>7 D7 q6 a& v2 L5 W. i: s
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>/ K5 m- h' `% R s7 O5 \$ Y
- <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
7 }; S: I" j( y: n O - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>% I; n( N' d* d; U7 W
- </props>; X- ~& Z: ^- B+ n1 n" G( {6 o$ K
- </property>6 _) u4 g( ^: O2 S) G
- </bean>% @# k) R* x7 r7 j- {
-
9 K6 L( z+ C) j( j9 v4 V - <!-- 配置事务管理器 -->( H3 n$ |) S9 x0 r- A6 k) P
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
! U, h$ e0 s$ Q0 F/ d - <property name="sessionFactory" ref="hibernateSessionFactory" />
6 X5 T7 J8 }* _. o8 J2 L" ~2 [! m - </bean>8 X7 A1 w& e* B+ \9 W5 B6 d
- * i9 c# y) Z. l5 B8 s& @
- <!-- 使用注解形式来管理事务 -->
+ k& L7 P0 H$ W% Y1 u5 [ - <tx:annotation-driven transaction-manager="transactionManager"/>1 x) O0 ?, h2 N- V
-
! @6 ~+ O! m3 u+ y$ c0 |( p - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->9 A& L0 ~) o* T+ m
- <context:component-scan base-package="com.s2sh.annotation" />, n: b% ~- \3 }1 H
- </beans>
复制代码 struts.xml:8 b* ~+ ~! E* W6 ~+ o
- <?xml version="1.0" encoding="UTF-8"?>; k3 B8 K- I* Q- F2 y3 v& `4 \1 _
- <!DOCTYPE struts PUBLIC( s" Y, Y& J2 X3 I
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 a8 F+ y2 f* U! w - "http://struts.apache.org/dtds/struts-2.3.dtd">
# m& w6 A- r X3 `+ G - <struts>
! D+ P- i/ m" j3 X1 e9 F4 P - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->8 m9 R; \5 W* N4 W1 @; ?0 X
- <constant name="struts.objectFactory" value="spring" />
' J" t" v/ U" |' Q3 G - 7 o0 O; d4 B; C0 U2 T1 f$ ]
- <package name="base" extends="struts-default" namespace="/" abstract="true">
( k3 ~- V4 n7 L& z - # Z& R7 V8 _4 N% { R. K
- </package>4 Z+ ^2 ?2 J: v) Q0 [ V
- 7 T4 x- d e. N+ G
- <include file="config/struts-bookInfo.xml" />
; z$ s7 p8 G, g. I- I; J+ e- y - </struts>
复制代码 * n2 ~! J! n: L* x" s- T' _
S2SH三大框架采用注释整合项目案例; ~. u! X# n) O7 |! Q/ a- l' h
解压码:# V/ M9 s2 j5 F! E
3 |" A9 x4 W. R3 R
" D# `/ G+ G2 A7 w
2 ?7 @1 T4 {4 L) | |
-
总评分: 帮币 + 1
查看全部评分
|