TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。& _ v8 h5 N4 m- f
1 G A( O; \* X, k) F
BookInfoAction.java:7 a6 k, e5 e: E2 t' L5 r- J
- /**' C+ x# X3 z1 _% r( R/ w
- * 7 N- E7 v; j, o% Q3 ^/ f+ O0 a, F
- *// c$ G4 N8 _( {3 T9 y: G Z% U: b
- package com.s2sh.annotation.book.action;2 i8 H: Y @8 B# u, D
- import java.util.List;
7 g8 v/ m' I9 W. z+ c - import javax.annotation.Resource;
6 j c% ~! n. t6 f - import org.springframework.stereotype.Controller;
$ v# C% t1 m2 C - import com.opensymphony.xwork2.ActionSupport;9 }1 G4 H" ]: }( n+ W' W. h' ~0 _8 V
- import com.s2sh.annotation.book.service.BookInfoService;
/ k- f. F. Y9 { Y# r& s - import com.s2sh.annotation.entity.BookInfo;
! h8 B( |% K4 {1 Y9 n - import com.s2sh.annotation.entity.PageBean;
9 [( I. l" b$ }. P
" J+ t8 @. X% C' j" F. ~( h6 x, ^- /**
, `0 `5 y7 Y( N3 l - * @author Administrator* Q. S0 q" V6 t$ d |& d
- *
: g7 y: E: K: ?+ I: O9 c" d& J - */
! q7 q9 r; D3 c* B - @Controller // 标注控制层对象
9 G1 X& b# [1 N - public class BookInfoAction extends ActionSupport {& N3 B& e. \; i
! X1 ~) j4 W& ]- }) h: W! r- private static final long serialVersionUID = 1L;
6 O7 w5 q" M5 Q+ ?/ ? - 0 h; [" V6 p M6 k9 x* w# w
- @Resource(name="bookInfoServiceImpl")
8 A, f) i& M" i; I - private BookInfoService bookInfoService;& F& p9 \: m& t; x5 R0 ~# H; `
-
9 J6 k; ^" {) z3 U1 y8 V: P - private List<BookInfo> books;
, w8 E& n- ^! H - private PageBean pageBean;9 n3 n1 m2 \# q5 z
- private BookInfo book;2 n# u. d( f1 S& P( K/ u( C+ P; Y
-
" P @7 q$ u6 | N0 `+ S3 ^5 |6 Y -
! N; E+ L' w+ d" P$ I - /**
% I& @ H* m& r9 \. l9 N - * @return the book
, _. D9 L) F$ t" @ - */% t1 o3 G0 n6 {6 d$ p
- public BookInfo getBook() {
* p5 t9 n/ C5 D% ^ - return book;1 Y9 N& t; S' a! u( a
- }8 R+ C- c; W/ r; Y6 R
- /**' [, y: O5 Q) C, ~# x) s6 {
- * @param book the book to set |" M) x& \3 G6 x
- */) x8 x* g) h1 W* {9 J5 z% O
- public void setBook(BookInfo book) {0 O; h. r r {6 X$ v; B; i
- this.book = book;
# _: o0 l7 i7 ~* K, g - }
8 l' F- n/ \4 U5 ^+ v - , K# e4 t" Q7 f) ~4 x: K5 B6 W2 Z
- /**) ]; {2 j6 {5 p3 ^/ P8 d( c
- * @return the pageBean
' y/ ?+ b3 i9 x - */
' J/ A1 ^; }9 K+ ?0 h" ^ - public PageBean getPageBean() {
: f& p" b( d6 ? ^- X' P - return pageBean;
1 ~/ l2 `5 @$ W R' ~7 i - } s, S# [5 k' W8 `
- /**0 i8 v) C- o& S2 n
- * @param pageBean the pageBean to set- H M9 x$ N* B* m; A5 ^, ?# J! S
- */
8 N0 d. c. H1 ?* B' s0 X( T/ ^) k - public void setPageBean(PageBean pageBean) {: T1 S6 p; Q1 d0 Y
- this.pageBean = pageBean;
; @& u9 o' f4 c$ S% m - }- G- C/ I: Y N5 q
* P3 w; x* M' @4 C; j6 j9 f5 ?* ~" r- /**
' ]( q7 X' k* E5 Z, r3 i - * @return the books9 N% T! ]8 _( @/ p( G E! v
- */
( z `9 @: s5 i: T - public List<BookInfo> getBooks() {+ U3 ^/ h, t Y! n4 @ |8 \
- return books;5 [ J' V& s" z& f+ \2 ~% R
- }) s, b, c/ t, g7 E0 I9 W
- % b8 P3 z- ]' B- O6 A8 p
- /**# {* ^- n5 N: W* h+ E% }( {
- * @param books the books to set5 v( u3 V- D2 s. ^' a
- */* n0 d2 x w. k0 G
- public void setBooks(List<BookInfo> books) {3 ^9 Q. O* a! A. A& C z
- this.books = books;
+ C) N* P: ~$ {( g0 D+ J - }
4 o- J# h2 t5 j% H" ~
4 L4 G6 X/ T- K- public String showBookInfoList() {- @- G4 w9 Z2 B+ R+ O4 E+ {
- pageBean.setPageSize(5);8 s) P* S+ z# C$ d9 c) c2 L
- this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));* }0 N' I2 t/ m2 S+ K; }9 r
- return SUCCESS;/ V" n5 _1 W3 F- X& T
- }
( R' d1 ]4 L: P6 H4 w# n
8 ]7 S- }$ D2 x0 b3 B! r. j- }
; U# }) c" F( [; T( n2 u. H
复制代码 applicationContext-persistence.xml:' ?" Q( i' y5 r j
- <?xml version="1.0" encoding="UTF-8"?>/ {" ~2 c' U: T
- <beans xmlns="http://www.springframework.org/schema/beans"9 k# x3 o5 F3 s$ ?4 V i
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
" y0 ^6 B6 ~, `, |% O. D- t# z - xmlns:context="http://www.springframework.org/schema/context"
7 X5 b' E* ^2 W - xmlns:tx="http://www.springframework.org/schema/tx"
" L; u/ i# M. e6 Y. ]( _! w# ?7 C - xsi:schemaLocation="http://www.springframework.org/schema/beans
2 x: P0 x1 C1 o; { - http://www.springframework.org/schema/beans/spring-beans.xsd
' o, _: i1 J5 ]$ v+ g - http://www.springframework.org/schema/context
3 O2 @- Z% m, a, B, Z - http://www.springframework.org/schema/context/spring-context.xsd 5 X( Z( @, j1 I5 y& L: ` ~
- http://www.springframework.org/schema/tx
8 C2 s; ~# g! `1 W - http://www.springframework.org/schema/tx/spring-tx.xsd">" g! I+ S) W- }9 D9 `
- <!-- 使用注解方式装配属性文件 -->9 @; \. x1 b0 q2 B+ U/ c
- <context:property-placeholder location="classpath:hibernate.template.properties" />
. [' @* `. P$ G6 H* s8 b7 t - <!-- 配置数据源 -->
' h9 ^! Q1 O: T. a - <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">0 O5 B8 G, d. t6 d
- <property name="driverClassName" value="${hibernate.connection.driver_class}" />
4 a. J# P% ?( z# X - <property name="url" value="${hibernate.connection.url}" /> l. k; ?: a$ V: g+ z) ?2 U# P
- <property name="username" value="${hibernate.connection.username}" />
- `7 U2 v( N" _3 Q - <property name="password" value="${hibernate.connection.password}" />4 j2 @6 i X& C3 g/ E1 Z9 ]
- </bean>1 N. w8 n: A) {: D
-
- h2 d9 w/ B, h: l) d7 H - <!-- 整合sessionFactory对象到spring容器进行管理 -->
/ p9 y) i7 K* D4 m, h9 L) h% ]; f - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
. u& B# H5 A8 X0 R- F1 O - <!-- 把spring容器中的数据源注入到dataSource属性中 -->
+ Q0 o1 G/ g1 V) d! l, E0 j6 U - <property name="dataSource" ref="springDataSource" />
; s, k3 a2 @4 H+ ~1 z+ C5 Y5 s - <!-- 扫描装配需要管理的实体类 -->
8 ]7 A+ Y. y' l, ~" Q7 H - <property name="packagesToScan">
8 u: c) g* m& |8 l: v: ]! f# q# E - <list>" R7 y3 X: `" r) d' o6 ?- Y
- <value>com.s2sh.annotation.entity</value>
! J# |& [. v% u; [, F% \ - </list>5 H: [. J$ A" v7 N9 E
- </property>
* h: O" ^' l, y8 U }( r6 p - <!-- 配置额外属性 -->. {5 }& K R# |' H0 n8 Z
- <property name="hibernateProperties">- _6 Y: b' q; g9 W
- <props>
( F9 N {4 p. u2 K - <prop key="hibernate.dialect">${hibernate.dialect}</prop>) { a" x; i8 \) D& M
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
* M2 W. |2 Q* I9 @9 h8 v - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
+ i( C; L3 J% e& T0 n - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
( G& ]' H% y4 J% u9 Z4 X- M. _ - </props>6 e3 ~$ t: m% V1 Y7 q
- </property>0 m w2 a& }: ^$ o
- </bean>
% h, p7 o9 |/ ?- a6 { -
; k3 s( E) X' |9 w1 `1 P0 j - <!-- 配置事务管理器 -->
7 m6 E R; z0 g5 c, i - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
2 c3 n+ E- T7 L, P - <property name="sessionFactory" ref="hibernateSessionFactory" />' W/ L3 L8 Y; j9 K6 P
- </bean>
7 Y1 Q2 J2 ]% K. z/ `, H -
9 J9 f" w. O1 |# i0 B8 N - <!-- 使用注解形式来管理事务 -->
8 ~; X: p4 w: J6 |" n; E* X7 l1 u0 S - <tx:annotation-driven transaction-manager="transactionManager"/>% q6 O, i: a- B# D' F8 |
-
# l+ ^: [0 U, g6 F4 R: L- V - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->% F% P1 \" [2 z
- <context:component-scan base-package="com.s2sh.annotation" />6 B$ V0 ^: P. x6 \8 R6 Z+ N
- </beans>
复制代码 struts.xml:
0 J) S+ H- P$ b4 r: H7 z% Y- <?xml version="1.0" encoding="UTF-8"?>
. I& h& P4 U3 M$ x& e5 N0 W9 b& h - <!DOCTYPE struts PUBLIC" F1 m% @' o- n5 D( C) |4 P
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"5 y9 B `# p( a# P' ?
- "http://struts.apache.org/dtds/struts-2.3.dtd">' n! n6 `" w* J
- <struts>
+ r, w$ H* o: N& x; K - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
# I) y5 p" r8 k, o - <constant name="struts.objectFactory" value="spring" />0 O2 E- u6 x' o/ B! x9 v j8 ~
- 8 ]3 E J7 \7 w" H: S4 F1 d1 k
- <package name="base" extends="struts-default" namespace="/" abstract="true">
6 x7 x% L: V. D9 K# z: t4 i - ! p+ F7 K* \1 j/ ]: b
- </package>/ h2 b5 m8 h" f/ k$ J
-
5 P& F1 W& C; }! b/ m6 s - <include file="config/struts-bookInfo.xml" />
- ]- R+ |0 S) w/ O2 b - </struts>
复制代码
* F2 X8 T9 |- l1 Q7 {2 wS2SH三大框架采用注释整合项目案例* S* T* i( F& |/ z* O
解压码:
+ _# i, [0 P. w1 O# `2 ]( F P! x) W2 X1 w) c7 j! ~
5 p' g0 R+ Y6 C1 I [, I
# y! ^. V" v5 _1 T' ^ n$ F+ s
|
-
总评分: 帮币 + 1
查看全部评分
|