TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
, m# Q8 N0 a6 j, r& o
4 M' g, k _4 r* j! A, U; a: OBookInfoAction.java:( Y8 F1 `1 m P5 A8 U
- /**) ?5 c" H+ t0 y* l5 _/ I! C+ O
- * 9 n' b% y' X, g, ^
- */
+ J1 w- L% w1 Z1 P - package com.s2sh.annotation.book.action;4 S5 I6 U$ ?0 k T- W
- import java.util.List;$ h9 c1 B- U0 y& n {9 r/ U
- import javax.annotation.Resource;
' o9 a0 T) a9 E2 j* h - import org.springframework.stereotype.Controller;
' @6 h0 ?% Z1 n, C, M/ j8 o- f/ h' A - import com.opensymphony.xwork2.ActionSupport;
# k9 u/ C% ]9 z/ B$ ?# h6 {- F - import com.s2sh.annotation.book.service.BookInfoService;
/ ~. q) t- ^5 H8 y' `5 t$ G- B Z - import com.s2sh.annotation.entity.BookInfo;; S8 }( X [( j, m, P3 }+ Q
- import com.s2sh.annotation.entity.PageBean;) d. P( T# y' b. Z, ~* X
V& n8 f% z" V4 N/ R) d- /**: B: r2 @# l7 T2 U$ e' f
- * @author Administrator
, e: Z( I0 B( ^- A - *
# S% e% Y& j' y" {9 b; R - */
0 @) {% S! Z: v: R9 m& G- o - @Controller // 标注控制层对象
" d% a: |: F3 k8 {6 O9 j1 J - public class BookInfoAction extends ActionSupport {5 v+ k/ R, Z$ h' a5 ^; R% _
9 i2 L& K( H# F. o3 @- private static final long serialVersionUID = 1L;
: d0 h4 h: _8 }- R9 U: [( r -
. M/ M7 Y2 q v# @2 G$ W - @Resource(name="bookInfoServiceImpl")- L; |8 w0 l1 k) g' r
- private BookInfoService bookInfoService;5 V# `: |; j9 ^
-
7 W8 s" n2 L# l" s: T% \( |% g2 Q) \. V - private List<BookInfo> books;
: k# f: ]1 R# Z1 n - private PageBean pageBean;) u5 A2 ^8 d n0 D
- private BookInfo book;
2 c7 D$ E9 s4 H1 s2 T3 B ~7 a5 U -
5 P6 l& O) J0 m; V -
& S( ~& L& y9 Q! N - /**6 K& Y% _0 i8 H+ t6 F
- * @return the book
$ W" N3 F. @% w - */
_7 a2 J% e; @& d7 F- D - public BookInfo getBook() {, T) S8 `8 |$ O) [
- return book;
* q/ g2 {7 b& i4 c9 A - }3 E+ I5 |) U- M5 H
- /**! B) x6 y3 Y$ L) i' ~
- * @param book the book to set N- `; }( E. K$ u& x
- */ {# f, ^; A3 g! W/ m5 G
- public void setBook(BookInfo book) {! v [$ j$ f( ~
- this.book = book;
+ A# B. O6 U$ V6 z" ]" V; ` - }3 ]2 E, L4 u1 H: y6 h4 }* e
5 G+ B: I. N8 s$ z% t% w( {- /**
/ t! l* T* x" g# ? - * @return the pageBean
. H" ^$ `% ?- o+ P7 @ - */
" e; z% @- E+ ^0 U; @ - public PageBean getPageBean() {
; @ t! g- o* }! W7 f h - return pageBean;1 p+ x7 P( x9 F3 R
- }
) p1 P( l$ l3 p, Q - /**- p* b# i, g: g% p! s: S7 w' }5 D6 ?; A
- * @param pageBean the pageBean to set5 V" P, F2 U: N4 X7 e/ s# b2 V8 x
- */
6 p; z3 a8 j. ]5 f - public void setPageBean(PageBean pageBean) {
* F$ F$ ^! s( l0 f - this.pageBean = pageBean;. V1 u% W9 J5 P+ Q- j( i
- }. o5 k+ A. y" y7 O& L
- ! u% O6 }8 V' H9 h0 z
- /**8 c* s7 \. C9 ]' I
- * @return the books. B- m, A t- i1 j( H2 W4 A0 n
- */' a/ h( }' n$ G3 k( J& F2 s. R/ K
- public List<BookInfo> getBooks() {
6 O0 q( D/ a& C- d - return books;3 o; g$ @! o* T% T6 |
- }9 S! |& u; {, R7 G. Y* ?
% J) S) f Q+ @1 m+ \/ d. _% ]$ z- /**; o, [0 T8 P( z/ P1 s
- * @param books the books to set
7 X" Z# L% U6 Y4 z2 [7 Y: y& c - */
' P& q7 w3 g$ \# I9 d+ u - public void setBooks(List<BookInfo> books) {# ]* a* C' F+ c7 l0 H: F
- this.books = books;' o+ i* D( w9 _/ j( b
- }
$ H# x3 L9 H, F5 u& X5 Y - " b8 @0 I4 Y/ ]3 E5 m( g
- public String showBookInfoList() {
6 R! w9 n% Z( ?, M& H- s - pageBean.setPageSize(5);: H; g# A+ f2 a5 V+ L
- this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book)); ~: D# L9 X* R1 _( }
- return SUCCESS;- U0 \( O R3 c( i
- }0 g! m8 `8 ?7 {( t H5 _2 c
- / k; m i! d+ g) h) {
- }) a. W/ l3 e! F5 j6 F
复制代码 applicationContext-persistence.xml:7 v8 B W% O5 _: B
- <?xml version="1.0" encoding="UTF-8"?>* U' F7 t6 f3 n" f# \
- <beans xmlns="http://www.springframework.org/schema/beans"/ Y0 P% t8 z, i: C" L& d4 R1 |' m
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"8 B8 d% D( I9 p3 \/ Q6 |
- xmlns:context="http://www.springframework.org/schema/context"* J$ f% r: Z* X8 U3 K2 t
- xmlns:tx="http://www.springframework.org/schema/tx") c, c8 _* U. k z2 C! d. w/ b: ~! f
- xsi:schemaLocation="http://www.springframework.org/schema/beans + I/ Z! V s# X
- http://www.springframework.org/schema/beans/spring-beans.xsd
. j: j! s4 u; ^: o* Q - http://www.springframework.org/schema/context 8 q0 w$ ~9 B6 B: I, w
- http://www.springframework.org/schema/context/spring-context.xsd 2 u2 l$ X' [! v6 X$ E' \
- http://www.springframework.org/schema/tx * D+ G/ d& U( U& A D( \
- http://www.springframework.org/schema/tx/spring-tx.xsd">
) L) @! K" L8 i. E - <!-- 使用注解方式装配属性文件 -->
1 N# K' n9 W) Z3 b; W2 R - <context:property-placeholder location="classpath:hibernate.template.properties" />
7 G; A" c+ |5 Q" k - <!-- 配置数据源 -->
: x/ k* M ?* P9 L! d6 ] - <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">: e# Y1 r5 l% T i8 _
- <property name="driverClassName" value="${hibernate.connection.driver_class}" />3 V0 N3 D; U3 ^9 k% Z. l
- <property name="url" value="${hibernate.connection.url}" />
% @* s9 `9 D; K) s V - <property name="username" value="${hibernate.connection.username}" />) q( I3 K% z; O+ u& v5 I
- <property name="password" value="${hibernate.connection.password}" />
% W/ N! t. K5 w9 E+ h" n - </bean>* e# a8 ~ o, |! D4 J* `
- & x6 W( @! O. I( c& n
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
- z& x7 I ~; d" Z% ] ~ - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"># i/ b( x0 R: P9 ~6 y2 B
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->% W- R, I. e+ F
- <property name="dataSource" ref="springDataSource" />
( j% G5 N E2 C# [ - <!-- 扫描装配需要管理的实体类 -->! l& m! O0 e. R$ E. ~
- <property name="packagesToScan">2 s* Y) w! C$ c) Z9 ^; U
- <list>. ^0 h& g9 e' ]* N) Y+ E/ o
- <value>com.s2sh.annotation.entity</value>' q+ f1 W+ f7 D% g, q- Y
- </list>* |' B/ d) F3 j/ E
- </property># H9 U* \5 |- h0 V
- <!-- 配置额外属性 -->
4 f; w9 x. K* V: w - <property name="hibernateProperties">
* S( }' C* Y% V/ ?# U) F+ ] - <props>4 D5 H- s/ n) ^1 `! N$ \) K; R
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
1 L) Y% t* m7 G4 `! c! i - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>$ f% t* a$ s! k# Q6 z
- <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>. @7 \5 e. v, a6 r: c8 D9 H
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>. L3 i" N. K$ f7 R$ H S
- </props>6 S2 O7 |& z w; _* m
- </property>! ~/ b4 |2 o p5 @
- </bean>2 @2 Y/ h9 k# t
- , u" W$ V' _$ ]0 ~
- <!-- 配置事务管理器 -->
* `. O% ^) d3 t - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">9 E8 d6 A" i: [! Y, z# a
- <property name="sessionFactory" ref="hibernateSessionFactory" />" N* b* @1 y+ x, A6 m9 l. ]0 b
- </bean>
' F% T: i& ?$ g" f: ?' z8 }% i - 9 H8 x8 F0 [' W X
- <!-- 使用注解形式来管理事务 -->$ l0 i& `* D3 @* }" z- F4 X! z. T
- <tx:annotation-driven transaction-manager="transactionManager"/>1 p( s5 [ ~: j+ S: i0 f
-
9 B" U) V/ v5 `! m( b - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->
6 ~# I" q! D3 O6 p4 U% E7 D - <context:component-scan base-package="com.s2sh.annotation" />/ ?! D2 e2 h) a! n( o1 e
- </beans>
复制代码 struts.xml:
$ r; ^4 d' g& p2 Z- <?xml version="1.0" encoding="UTF-8"?>5 Z/ S& U. z* c, r
- <!DOCTYPE struts PUBLIC0 A/ j) L+ |- u. z2 P
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"3 s2 f' \5 t4 H7 {& W( |# S# W' D
- "http://struts.apache.org/dtds/struts-2.3.dtd">3 V P' }$ y j% V
- <struts>6 T7 j3 N& `& b# h: e. W/ p% i. e. }
- <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
" v! a& l9 n" a( d- y: @/ r - <constant name="struts.objectFactory" value="spring" /># V" B5 x F; h
- % A, W" R) ^0 _+ a# B! A& Q( R$ X( t0 A
- <package name="base" extends="struts-default" namespace="/" abstract="true">
5 N& A8 L$ q/ |/ J* O! ~1 w8 ?9 C; t9 X - : ~! F- i( B$ e) O
- </package>- S' h; N. I3 S1 u% c' f! p
- + u; {$ ^* S" F5 z0 k5 b
- <include file="config/struts-bookInfo.xml" />/ k+ A& d5 v1 P+ x, R6 F* g9 Y
- </struts>
复制代码
. R, _: C% {: B9 s0 ~S2SH三大框架采用注释整合项目案例$ _7 \- \" q% G; n7 \) J
解压码:
/ d% i) E: W# w
2 [5 k5 x" b- k+ h- ]2 \. ~" K! h, W- l- l, z
; m8 |7 S, j* J$ m# v3 }1 p% q |
-
总评分: 帮币 + 1
查看全部评分
|