TA的每日心情![](source/plugin/dsu_paulsign/img/emot/shuai.gif) | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。7 ]* U' [4 S1 q- Y5 ]/ @1 }, m7 @: j& @
1 V/ y: A8 p- r( dBookInfoAction.java:
, y- o% o9 O9 [- Q- /**$ Z6 \8 e) G0 U( }7 N/ S
- *
8 g' Q% f# @: m: M6 D, ~ - */6 c" s3 W8 _3 x0 v& H! P' j. ~0 j
- package com.s2sh.annotation.book.action;
# J! H6 F0 ^6 ]) D - import java.util.List;
1 f7 V9 Y) \& _2 r - import javax.annotation.Resource;! W1 w S7 G+ E" F; m5 O$ B" S
- import org.springframework.stereotype.Controller;, W7 M, e. o7 W, S6 r: ~: `
- import com.opensymphony.xwork2.ActionSupport;( P$ ?7 i& u4 n: Y+ ?
- import com.s2sh.annotation.book.service.BookInfoService;
- C0 b' A6 T0 t7 k# f - import com.s2sh.annotation.entity.BookInfo;
2 P1 ^: g) C. d( Q8 r - import com.s2sh.annotation.entity.PageBean;3 j1 Z, t, H8 h5 H
- & Q/ ~" z: z- d) U- w$ {
- /**
7 S! r0 l, I5 `0 ^ - * @author Administrator
: E1 D) m& l# I - *2 S9 u- }$ o& |5 L$ D/ s* Z% N# Y# }3 W' N
- */! ?/ u$ i- b6 ^6 ~4 B% k# n1 @
- @Controller // 标注控制层对象
# w3 V( g- I+ z' Q - public class BookInfoAction extends ActionSupport {7 E& l3 u0 H: l- d. C9 H
- 3 g( X4 a, z& F9 F; u
- private static final long serialVersionUID = 1L;
1 J+ p$ C3 a% N+ y9 W$ [ -
7 L+ Z# I7 [6 }1 E* r; R& \ - @Resource(name="bookInfoServiceImpl"); W0 W1 w9 R! g
- private BookInfoService bookInfoService;+ o7 y n0 K3 F: f6 c
- , M1 \9 b6 m8 ]2 e6 H
- private List<BookInfo> books;9 s3 j! C& p" ? F
- private PageBean pageBean;
' F' k, c0 A0 b3 V/ H# Y1 D" B - private BookInfo book;$ c$ a/ V4 }+ n
- - S) a8 r6 [& i$ e" M
- . P a; [3 [- {7 m! K* C9 D: w1 m$ C
- /**2 f* J+ e1 d7 H4 n# o% [
- * @return the book
( P! b/ F( X, D8 E0 q9 D4 y, B - */
% O( G% W# N n5 Y) m, G - public BookInfo getBook() {0 |) S& R: _7 y8 b3 X
- return book;
4 k+ v5 N4 j* P8 m# i9 v" }9 Y - }) o: W. |! D2 m
- /**
7 I! x g% r1 ? - * @param book the book to set6 g$ b1 D$ d6 `
- */ w; x4 G- ~4 O! @- R* b, n+ A
- public void setBook(BookInfo book) {. z! {( r8 e. y$ t! m& H1 ]9 j; g. [
- this.book = book;+ p2 J# ~1 @% ~3 B
- }
2 S1 {3 A( i& C4 a c - 7 F& [5 z2 h# ]8 m
- /**
3 o. Z! L+ D( }5 f/ j# _% ?7 J - * @return the pageBean
Q0 c/ C7 o$ ~5 V2 Y7 S9 s9 O - */ d7 t5 G( g/ Y: ?$ Y1 l
- public PageBean getPageBean() {
& U" X. E$ ^8 {6 m& j - return pageBean;2 S* p* g: G; f
- }
# P! L: U( e2 {0 ~5 A; x - /**
z; t& U/ I& G% q4 u1 U g - * @param pageBean the pageBean to set
# N0 x9 `' Y, Z7 t% r: Y - */
* X( r/ {. f" `& b - public void setPageBean(PageBean pageBean) {; y N& ^; ~4 [5 V. p7 r) N
- this.pageBean = pageBean;+ a( D e5 A& o
- } d% l( I [2 Y; y) O* e: s9 P
- $ u9 G5 z6 ]8 k" s
- /**
% X) x8 ^1 P n& y8 ~ - * @return the books& @; @3 z, G! C$ O
- */
- }' a1 `8 b- f/ t2 F - public List<BookInfo> getBooks() {
9 }1 ~+ z; U) n# e - return books;
; g4 ?( l! F% S# b# s0 O0 U - }7 ~0 {1 [$ ^& k7 D' d* l
- . Q2 I& |! J1 _. a7 F% u! X3 p
- /**3 J6 i5 W" ], \! d+ ^* K7 s
- * @param books the books to set
" x' d9 u- J# s5 J+ e - */% f h( v: A R
- public void setBooks(List<BookInfo> books) {
* M3 a! h( w$ P+ e; \2 e - this.books = books;
% u8 V: e% z2 ]# q1 g2 u& g0 m* ` - }/ v I5 `( Z0 Q) [- z
6 ~$ N7 ^( x- K- public String showBookInfoList() { E& H6 b$ }5 A2 B' J
- pageBean.setPageSize(5);
% s2 Z, ~1 K) f - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
* K, N9 f5 o! i5 S3 s" m6 A - return SUCCESS;7 \! h! P) L' n2 g& i7 _0 z
- }
4 \7 F X! @+ Z8 v: e0 P2 F - ! X% K7 P& f, E+ s5 c
- }1 D. P5 m+ y% o, k( f2 d1 x
复制代码 applicationContext-persistence.xml:
$ p+ r1 q; w" A" M- \5 H& j- <?xml version="1.0" encoding="UTF-8"?>$ b/ G; L+ R, d% g2 I- `" B
- <beans xmlns="http://www.springframework.org/schema/beans"
9 Z2 [2 q3 \& |& f1 P - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"9 S- D. G# I5 }7 i
- xmlns:context="http://www.springframework.org/schema/context". M5 N- _5 p) A, X+ [& `8 B
- xmlns:tx="http://www.springframework.org/schema/tx"6 f8 \2 Q* G4 Y d
- xsi:schemaLocation="http://www.springframework.org/schema/beans
6 l9 V8 L- a, s7 P* c3 \! Q - http://www.springframework.org/schema/beans/spring-beans.xsd ) l! q; I& |1 r; y0 ]
- http://www.springframework.org/schema/context
* S; w* P0 ~) u0 z, [! |4 \* ?" Z - http://www.springframework.org/schema/context/spring-context.xsd ]. ?6 T& J( P5 w8 A* G
- http://www.springframework.org/schema/tx
& @7 Z. ~$ h5 l, j. E- `' y - http://www.springframework.org/schema/tx/spring-tx.xsd">
& k, ]; M+ ^! v, F - <!-- 使用注解方式装配属性文件 -->0 h5 I+ _4 N& ^% o8 x/ ~, M. n8 L
- <context:property-placeholder location="classpath:hibernate.template.properties" />: s# L; c, ^3 c* F) ~
- <!-- 配置数据源 -->9 d( Z: x# J. N. p* p' a
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
p1 ]6 L: v3 A; [ - <property name="driverClassName" value="${hibernate.connection.driver_class}" />
; M/ P# |( D' ~7 o, E - <property name="url" value="${hibernate.connection.url}" />3 H) ?- Z" T! L, X" T
- <property name="username" value="${hibernate.connection.username}" />) n N3 w9 P' F- F, ]
- <property name="password" value="${hibernate.connection.password}" />
6 w4 u5 f+ {# I0 O* U6 M - </bean>
1 P( [% K( i) j -
: u5 i/ ?- B8 _* B - <!-- 整合sessionFactory对象到spring容器进行管理 -->& I2 k |/ B! n5 |- p% X
- <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
# Y$ _9 R* H+ T c6 V9 s5 v% O - <!-- 把spring容器中的数据源注入到dataSource属性中 -->
7 z& z7 Q) v8 F0 S6 i - <property name="dataSource" ref="springDataSource" />
( m1 h3 P& u& P( m - <!-- 扫描装配需要管理的实体类 -->
9 Q/ M( j$ r1 Y+ s - <property name="packagesToScan">
' m8 o. P# |; B# ]9 d' i' ] - <list>
' @8 B9 }& n$ R& ]5 V - <value>com.s2sh.annotation.entity</value>5 l& G' p: \/ ^6 {
- </list> u+ C$ F! N( ~$ X
- </property>" z' u% _4 O( D& K, R- Q9 x
- <!-- 配置额外属性 -->
1 E. T, e5 `3 P- s - <property name="hibernateProperties">
c7 G5 G2 G( _2 W1 j - <props>. h( I" J1 d4 q) |4 O1 ?
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
0 h; L! ?/ W5 @" _: ~ - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
" K5 @1 c1 [- u - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
g J( v1 J% V8 n+ p3 } - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
& V1 B4 U* {4 T3 _+ V# p - </props>
( \. a; h" i5 x. o - </property>1 N# f/ o( |3 a, n+ R4 l
- </bean>
$ C4 N: f7 a' b R- a$ \$ Q. K -
5 q- u- k" R' O2 w2 {: u - <!-- 配置事务管理器 -->) _! I% e( w2 F% e! n
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">' T/ y/ K0 D6 N! ^ a( M! I1 Q
- <property name="sessionFactory" ref="hibernateSessionFactory" />
6 ^+ ~1 B- o, T# R - </bean>
# ]( E# P/ B" l* F' z% ? -
6 E* ^0 y4 B+ U: G4 p7 h7 | - <!-- 使用注解形式来管理事务 -->
( F6 r3 b; G) Z. O; z0 c L' E0 F8 f1 U - <tx:annotation-driven transaction-manager="transactionManager"/>( F+ Q/ b& V- F7 _ g
- + [0 \& T9 E0 o$ S0 o
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->( e+ ]7 h$ n' N$ L% J
- <context:component-scan base-package="com.s2sh.annotation" />/ n' b/ X0 ?. S; O. ]% u
- </beans>
复制代码 struts.xml:# Q; g4 q5 P0 Y1 z! ^( D. s
- <?xml version="1.0" encoding="UTF-8"?>3 H' b- ?9 r8 x. F
- <!DOCTYPE struts PUBLIC
& q9 X7 F& T: T3 ^ ^( x3 |; h. P8 H - "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"- b' D2 C/ L9 d0 [7 W) ?) H
- "http://struts.apache.org/dtds/struts-2.3.dtd">3 M$ R5 Z: o P' n# E C! v9 L* j* K
- <struts>
( P9 Y* k6 l4 D - <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
- @( }8 r, |. l. C) o* g - <constant name="struts.objectFactory" value="spring" />
% Q* Y% X4 Q! k0 D, D9 T -
: `, F0 `9 l# }4 \/ j" t% x; t* e - <package name="base" extends="struts-default" namespace="/" abstract="true">
9 V" J8 f0 a! S, _$ O# N5 w - 0 `; s5 g# ~2 j( i% s
- </package>
/ _ |5 {& P, w% B) d$ I2 ?) _ -
7 E$ e) r4 u3 L9 i - <include file="config/struts-bookInfo.xml" />
& d. L M+ ?1 m2 _, J - </struts>
复制代码 * K7 |" T2 T+ c4 \: j
S2SH三大框架采用注释整合项目案例* y1 i- T( V0 T0 D/ W& F
解压码:6 Y; }$ s$ Z8 e) ^# w6 N- W6 M* f5 m
9 x8 v8 O( B1 C1 C( {; @( i |
; v& x2 \! Q O9 U# m- K9 J/ A+ {8 @$ x5 t* Q1 V; O' l
|
-
总评分: 帮币 + 1
查看全部评分
|