TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。# ?2 G1 k/ }4 M/ |+ G! F% B- P
' x, v( U& v H* L6 ?6 x2 \
BookInfoAction.java:' _& P" [' z. D8 |& M
- /**! m; A# o1 m) a* @( S% |2 L2 ^
- *
* R. L$ y' s: a - */
* W- F c8 ^; J$ q6 a Z# V - package com.s2sh.annotation.book.action;
5 z; S$ x6 x7 q - import java.util.List;
+ h: h8 j* T5 O/ x- F$ z - import javax.annotation.Resource;
9 D4 L/ d6 U( E* g4 _' p - import org.springframework.stereotype.Controller;
' H/ Y7 a8 `$ |: i - import com.opensymphony.xwork2.ActionSupport;
* X8 ~* m5 Z0 q' b - import com.s2sh.annotation.book.service.BookInfoService;, @! X0 d# ~7 p1 X4 s3 G
- import com.s2sh.annotation.entity.BookInfo;
# J+ U" h& T/ } - import com.s2sh.annotation.entity.PageBean;3 \3 m( l: P) d2 B
6 A9 r. n/ L7 D* Q; d3 W- /**
! p* L7 L+ F# C, s& Y% N; A - * @author Administrator, Y) p4 Y8 Q* a0 n! k) ?
- *8 s2 [; @' v* E/ y# |
- */
, E7 N2 o8 S- U( q9 q; J - @Controller // 标注控制层对象
j1 ~" W! e' Y j8 y7 ? - public class BookInfoAction extends ActionSupport {
. Z3 @+ R) }. e) j) Q
+ Z8 p0 \% g2 t3 s4 T8 k- private static final long serialVersionUID = 1L;- c3 }8 j9 v! y4 y( k/ x/ |" I
-
9 ^+ A" T- P i& C2 X - @Resource(name="bookInfoServiceImpl")
2 S. Z! M6 b' O$ G$ \* f - private BookInfoService bookInfoService;
8 V3 L# k1 Y" o5 B - 0 B2 X. P% E4 E3 E' z* b
- private List<BookInfo> books;
# e! k& {- X2 K0 J4 _ - private PageBean pageBean;3 T( F$ o* C, I8 j
- private BookInfo book;
' \& t9 b& `2 l9 S, j. v -
( n6 f/ o3 R% X3 V. m: @$ D$ D( W -
" j, f4 G+ ]3 t2 W9 U- y0 V - /**
2 \! t! t* `! C' }& W2 v' L7 J# g) X. x - * @return the book9 i |6 y' N4 ?8 Y: t/ q3 G
- */2 T/ m) Q" q; ?
- public BookInfo getBook() {1 S) H5 U& F* C$ _
- return book;, W8 g7 I/ {# I' Y4 [5 w
- }
1 G+ L2 y% E$ s' { - /**
, V8 U4 `% j' f' r. M0 T' V2 Q% R) z - * @param book the book to set
* z$ g9 }) n) s5 A! E3 m7 c6 i; T - */. x( S# C; c' F; S# t
- public void setBook(BookInfo book) {4 e' J8 b6 p( m5 h( `
- this.book = book;, P: y: E8 b. ?( _9 M/ M
- }
. |& c/ j5 [: m: g; H7 a2 D7 Y
! H4 z6 q1 ^ G5 e" N- /**
8 G! A, \0 ?! R% | - * @return the pageBean( A/ I, h3 x4 ~9 F
- */, y" \( A5 n Y1 r, f% h1 P# x4 A
- public PageBean getPageBean() {0 u# H- \. d" H6 p
- return pageBean; q& V9 S6 d3 G
- }
+ b! ^* q, H D c) [/ J. y - /**1 B: _( M# X1 p
- * @param pageBean the pageBean to set( V# b( o6 f5 p
- */9 W' s \5 _2 Z& V+ g
- public void setPageBean(PageBean pageBean) {- S; f% g! @$ q) Z0 [& T" j
- this.pageBean = pageBean;$ q( z# m9 ^5 o$ y+ Q6 ]' L
- }
9 }! }3 L4 Q9 i' ]3 w - * p# i) p u) z: q% s
- /**
5 P; ^/ K- s) D - * @return the books1 B9 W$ X7 _6 _/ X* K
- */2 S6 \9 K, `8 U4 Y& _! |; X: @, o
- public List<BookInfo> getBooks() {
/ `( Q Q+ Q# V - return books;
# A8 O6 v" q- E& c6 @* p - }# W0 ?- Q# [4 U% l
- ?# H- O3 l0 Y
- /**
& t9 l" c+ ~; E& X3 v, b5 P+ _+ a' W - * @param books the books to set/ w2 a' Q. I9 N2 k
- */( H& C( R0 s6 r/ m" [& E. S( ~
- public void setBooks(List<BookInfo> books) {: {; C+ N* D1 R1 S7 N) f
- this.books = books;
4 E# \& n% M4 k) d. x3 H7 s - }, t* X6 D. v+ T0 v" B
- 3 i: j) W, B2 T7 _: f, r: |) f
- public String showBookInfoList() {
3 m6 M, m6 i) d - pageBean.setPageSize(5);& ^- w3 k: V3 L: I+ v! i
- this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));- a( b6 L, b6 {8 p8 V% S
- return SUCCESS;
% |6 c( I, c, s8 q - }
# d! H5 ?* F* o
, t' ?5 Y: V% x! }; }$ }+ f- }
" o: U$ p7 c* O4 ?' j: C
复制代码 applicationContext-persistence.xml:1 Z8 T% }# D/ q
- <?xml version="1.0" encoding="UTF-8"?>
" j) k% L. @/ w+ M7 s# x* e - <beans xmlns="http://www.springframework.org/schema/beans"
1 E% d' l$ A! C# Z! r$ { - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"( A# H( i' s, w$ C
- xmlns:context="http://www.springframework.org/schema/context"
6 x- X- t2 A1 ^8 O, Z8 Z" n - xmlns:tx="http://www.springframework.org/schema/tx"3 P1 ?5 L/ {, [( t& J) Q, n, H; B
- xsi:schemaLocation="http://www.springframework.org/schema/beans 0 b1 Y1 c8 h @$ a$ |. O
- http://www.springframework.org/schema/beans/spring-beans.xsd 7 K' ^" k+ ?1 D" d
- http://www.springframework.org/schema/context
3 v4 \8 o2 M+ W/ s' ^ - http://www.springframework.org/schema/context/spring-context.xsd
8 P' O0 x1 e& D6 i9 i - http://www.springframework.org/schema/tx
$ C5 I4 q& [. T0 O. C0 r V) e0 ` - http://www.springframework.org/schema/tx/spring-tx.xsd">% _) E6 O/ l: t' Q
- <!-- 使用注解方式装配属性文件 -->
# S3 D* D3 j0 s* F; u9 _3 |- S - <context:property-placeholder location="classpath:hibernate.template.properties" />$ r& v8 R. i( N! v
- <!-- 配置数据源 -->8 q) L# {7 {; i, e" x5 e7 b8 K$ J
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> T3 H. i8 N- C5 f, C _$ e
- <property name="driverClassName" value="${hibernate.connection.driver_class}" />' l; u8 @5 P4 ^8 a5 v
- <property name="url" value="${hibernate.connection.url}" />: [6 N) v1 \3 e7 J) E% h2 s
- <property name="username" value="${hibernate.connection.username}" />
% Y; d. n5 H7 j5 O5 T" p+ X: R - <property name="password" value="${hibernate.connection.password}" /># C1 j- ~1 ?7 {# q: t0 Y
- </bean>
: K& S; V! Q4 B7 R1 l - # C! m. `: ~: K K, g) e F; t
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
! ^) L9 e7 i3 x9 ^4 o( n: C1 @ - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">$ j: z% B) h3 R, ]( N3 J- E% o
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->- d$ P# R4 a$ ]& A6 D- X# w
- <property name="dataSource" ref="springDataSource" /> y& `( ~$ w8 |: r& t2 o
- <!-- 扫描装配需要管理的实体类 -->/ H/ \4 |9 ~# k1 S
- <property name="packagesToScan">" @! U7 |* Z. b: U- @4 X, W3 O) r1 R
- <list>" ]4 u% z8 z2 P% s) Y9 \
- <value>com.s2sh.annotation.entity</value>
! p$ V4 j. m2 [' o' X - </list>
8 E: I( n, x8 G- \! \ - </property>/ D9 B4 y# C' `
- <!-- 配置额外属性 -->
- I7 ]6 {& B3 O6 y - <property name="hibernateProperties">8 u! n4 V1 d: s$ X
- <props>' r% U: B2 W& B2 I' N0 e
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>0 ]4 j, _: u% z, O' U' p% v- `
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
7 Q1 H5 m" r$ N% m3 q I" S7 w - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>. w, d! ?9 s$ q: O9 B
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>0 p* R% U a# A; J
- </props>" S( ^$ X2 o2 v- }, `% F
- </property>
4 m& e y" o0 m/ B - </bean>
8 d9 V. g# H2 O0 n; h* t - 7 w% g+ \0 s! Z. P
- <!-- 配置事务管理器 -->
! k9 v5 T7 V9 x+ }$ Y - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
5 m7 U, U* D: U7 [, |! X; T - <property name="sessionFactory" ref="hibernateSessionFactory" />
: i E0 s+ V% b) R7 w - </bean> F0 e( n# z" m! }2 l2 Z8 N
-
% e" J; y: b+ I& { - <!-- 使用注解形式来管理事务 -->
0 J$ F+ S. Y8 y. @9 D - <tx:annotation-driven transaction-manager="transactionManager"/>
- v5 H, X+ ?' M -
f2 [4 c w7 [& ~ N ^* }. n - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->; h- ^0 H# F) h% H( }( B q3 [! L
- <context:component-scan base-package="com.s2sh.annotation" />
, u0 B4 h y, F& F - </beans>
复制代码 struts.xml:( ?9 y( W$ t( U: c* P
- <?xml version="1.0" encoding="UTF-8"?>) S2 A5 ~/ \2 Z5 A0 }* V8 ?
- <!DOCTYPE struts PUBLIC3 E5 W6 W; l& ?# u" _
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" E9 t1 Z) g& J! K1 d
- "http://struts.apache.org/dtds/struts-2.3.dtd">
; F8 x. ?, u' k3 [: l - <struts>1 i" A$ ^* s2 r) Q. Y' W3 w# i
- <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->) l; v, ?8 q% w
- <constant name="struts.objectFactory" value="spring" />
" {& b }$ Z' h -
$ L* U" ]% ]1 ^6 A - <package name="base" extends="struts-default" namespace="/" abstract="true">
* Q# h# a/ S$ k5 f -
0 b5 f) y2 C' o. E( m - </package>
' U% C8 l6 ^1 p9 t; `# {( F - , G$ ~- V) T J
- <include file="config/struts-bookInfo.xml" />
0 [) }/ D4 Q6 M: E; q! n9 @ - </struts>
复制代码 3 E4 r/ \: K! D; f3 V- O
S2SH三大框架采用注释整合项目案例* n ]2 {. p t) z6 c; C- t- D7 R; k
解压码:" T" z, c: d( Z5 P) k4 K! F" |& T
4 |: c/ o `+ n% w' [$ O
' x+ a$ F4 Y3 B) N. X
x7 Q/ d6 H& a/ ^0 h |
-
总评分: 帮币 + 1
查看全部评分
|