TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。
$ J( h ~, v- }: E1 C K& k$ {( p' i: K% [- D0 |) b0 n
BookInfoAction.java:
6 b& K H, F" X9 h. m- /**( L$ U5 a1 N9 L4 ~$ i0 y! g4 e
- *
4 E: |3 E2 n4 P. v/ c9 J# d - */
, j0 B: l- @' L, T+ G - package com.s2sh.annotation.book.action;3 H% T' N* }. C- C1 A
- import java.util.List;! V: U- D* ?3 i: t; F) y
- import javax.annotation.Resource;
. C/ F; J5 s* R - import org.springframework.stereotype.Controller;
" k- K% t# Y- Z1 o3 r& h6 ` - import com.opensymphony.xwork2.ActionSupport;
T$ C4 a% m2 ] - import com.s2sh.annotation.book.service.BookInfoService;: J4 d5 M, T4 ]$ z9 B2 ~
- import com.s2sh.annotation.entity.BookInfo;. I- d0 v [1 v: _0 A
- import com.s2sh.annotation.entity.PageBean;
8 f5 B x2 [% o4 ^0 | X0 g+ q - / |4 D& w1 s* [" y7 p
- /**" w! `3 n& o0 b v
- * @author Administrator
, i5 _/ F! `- S+ Z, P5 O - *+ u7 E2 S2 b/ F" m! t4 ]: r
- */
" g g, d8 D0 _- K% x - @Controller // 标注控制层对象
2 b" |; F: m+ ~0 M' S; f - public class BookInfoAction extends ActionSupport {
& P) p* X2 b+ W% H - 8 D: @4 ]6 N$ \ }! M% h
- private static final long serialVersionUID = 1L;
- x- e. d9 t) ] _7 t - " _+ h. w* ~) P' l; \( _
- @Resource(name="bookInfoServiceImpl")% j, l/ p' G* e# e: r- q
- private BookInfoService bookInfoService;. e; l! c. X# Q
- 8 M) i$ j7 `5 v( J$ Z; x
- private List<BookInfo> books;
1 X: t* h* U0 i& Z - private PageBean pageBean;
" o8 A: L2 l0 d* _1 O. n - private BookInfo book;2 [9 i* a7 Z$ F
- 8 g9 Y1 n& Y6 j" {2 g* S
- $ k" O+ K# V# X% p0 J4 y/ [ e
- /**
8 g3 K- e2 h$ f - * @return the book- w, k1 M2 F8 p
- */' W8 d8 e, h" ?; y3 y
- public BookInfo getBook() {9 w. U3 h T& H( e
- return book;
6 ^0 ]2 i3 t s b/ J/ v: z - }9 j) R8 @1 u4 u8 x- C
- /** q2 G) q4 A7 Y3 i% N9 r
- * @param book the book to set/ W' @9 A. v* M' q
- */- [: O7 d4 p t# b
- public void setBook(BookInfo book) {
0 z4 Z5 o/ Z8 ^7 o - this.book = book;
, o6 G- R L' i - }) Q$ V+ H+ W2 z
- " K2 b" z' v" g- s4 i
- /**
* a* w% I1 D i8 s( ]; @: Q0 k - * @return the pageBean
! B$ x% s' y8 T3 a+ Y* F7 Y3 S - */
; Z* t& h; t- Z. M+ W - public PageBean getPageBean() {2 s, l& C( M7 H0 E
- return pageBean;
8 s- p7 M* D3 a( o! |1 M- } - }
2 e/ Q' j8 ?( m! O$ k$ g - /**
+ k* d9 g/ N. X1 }/ Z$ a - * @param pageBean the pageBean to set
& v5 n7 s) F6 J! X! j* M1 s - */
, {6 T7 Q$ J+ ?2 o# ^* m - public void setPageBean(PageBean pageBean) {* S2 t% O. }! H# }$ P6 b
- this.pageBean = pageBean;' ~2 F6 |1 E& o0 \% b
- }: p) x6 s. ?# D1 y5 E
4 H6 U5 X' }- M! y: x/ H' W- /**' t* B% J0 l( C+ Q
- * @return the books
6 k+ q( t8 q1 |& K8 D - */
7 y$ V. I C: b+ Q8 |" k3 l - public List<BookInfo> getBooks() {
9 h/ M% ]+ c3 Z6 u& v! L7 b - return books;1 o6 U# S8 a5 f2 T1 R' ]$ I
- }, y) I) L) @; u
& n! R5 N6 s: L( q6 q ?1 O1 V- /**
: J! h1 E3 G( @3 d - * @param books the books to set( C2 `3 P; k- A" a, ~1 Q
- */% x$ H% U3 I; T* ^
- public void setBooks(List<BookInfo> books) {& D" v+ D2 A* v' m) H4 [/ W
- this.books = books;
, l1 n" D1 S8 H - }6 V3 M0 J) p1 k3 [% F
- 6 C! i d) X$ C! B) I1 I
- public String showBookInfoList() {( G8 F0 g( @! j' C
- pageBean.setPageSize(5);
: u3 k0 C, g: f - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
* `. R0 V' N/ A* C; f+ S - return SUCCESS;
. k$ @4 A# Q+ M6 q5 K - }6 a0 g+ Y+ m# @# p
- ) v+ W1 F- ]2 T% h- f3 U, w' E
- }0 Y$ X4 @' [/ y! _( B
复制代码 applicationContext-persistence.xml:
" z1 Y( K T& c( y4 A. C+ D- <?xml version="1.0" encoding="UTF-8"?>
+ ^7 ^# Y$ n. g$ L - <beans xmlns="http://www.springframework.org/schema/beans" R3 T# [ z) K5 H! [! N
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 L4 y5 n! V" R" l, ] - xmlns:context="http://www.springframework.org/schema/context" M/ Y% Y+ _6 s% p4 B
- xmlns:tx="http://www.springframework.org/schema/tx", b5 P; G' a3 x
- xsi:schemaLocation="http://www.springframework.org/schema/beans
8 }/ w" t9 _5 J2 n. s. R - http://www.springframework.org/schema/beans/spring-beans.xsd
( r4 z! ?4 W Q9 k( |! `, u2 D8 ? - http://www.springframework.org/schema/context 0 X! w/ s# U( u9 F" N2 W c+ ?3 ~8 j3 H
- http://www.springframework.org/schema/context/spring-context.xsd 0 q G' l% q$ {! Z6 [
- http://www.springframework.org/schema/tx
! @3 N- {# y+ G# `' P8 f+ E( V - http://www.springframework.org/schema/tx/spring-tx.xsd">
; P2 g2 | _0 X9 A B - <!-- 使用注解方式装配属性文件 -->% O- \, h' D9 a7 a9 F- n9 P
- <context:property-placeholder location="classpath:hibernate.template.properties" />
( h2 ?- N O% R1 Y* o7 ` - <!-- 配置数据源 -->
0 r \0 m; g" d1 r - <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
* I7 c \' P6 |- V5 m - <property name="driverClassName" value="${hibernate.connection.driver_class}" />8 h; M: {4 P7 b5 T' h! f
- <property name="url" value="${hibernate.connection.url}" />' z% S* u3 V p6 r* `' U0 i0 H$ Z
- <property name="username" value="${hibernate.connection.username}" />$ c& y0 S3 G6 C1 Z7 o. O
- <property name="password" value="${hibernate.connection.password}" />
q2 i! z* H' e - </bean>) y V2 v e ?& |, s
- + ?2 s+ N' a0 \6 w* E' |! |2 p; ]" [
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
, l- E$ O; c2 Q, c4 O7 N9 Z - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
/ Q, \. C( |" u+ W* W9 x - <!-- 把spring容器中的数据源注入到dataSource属性中 -->, m X+ m8 W/ t( Y0 t H. a- ?
- <property name="dataSource" ref="springDataSource" />$ d; b/ I1 f( i
- <!-- 扫描装配需要管理的实体类 -->2 k9 \% z6 Q3 k3 L9 ~+ [! A! w4 `8 h
- <property name="packagesToScan">
- | s8 d3 [2 j+ K; W) a - <list>0 H! c' x) e5 f* G4 H& l
- <value>com.s2sh.annotation.entity</value>6 o+ d# l3 b: Q4 W% V! O# h' r: u I
- </list>% V( N7 W; R" u$ j* C
- </property>
& S( o/ P! e) j4 K* |* R - <!-- 配置额外属性 -->5 p! X% s" M/ K+ T" O8 D6 e
- <property name="hibernateProperties">
) D* A9 p& d5 @2 E: y+ d' b - <props>
" u- c& q% D6 X3 q$ D# m - <prop key="hibernate.dialect">${hibernate.dialect}</prop>2 I7 Q- V8 j3 i; e
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
& O. _# }3 y: T, a( x4 ` - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>: g4 o' C* K" p+ j3 B U: ]8 C
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
# @1 ?8 k# D; p4 T% V, p } - </props>( V: d1 R! Y) \$ \/ z5 V2 m* v
- </property>
$ v) e- I0 a2 z, R# M9 Z - </bean>
& z# T" o. G: l' Y, d -
6 J+ o8 F$ O: a% s - <!-- 配置事务管理器 -->3 S T( ]3 M0 K; ^/ D) f
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
0 N$ j W ]- c/ a. a) g - <property name="sessionFactory" ref="hibernateSessionFactory" />" g, g& s$ D8 K- Z7 C2 h0 u
- </bean>
8 _+ y+ g) G9 y$ S2 e f: P -
8 [: W1 W. v( q9 `1 O6 ? - <!-- 使用注解形式来管理事务 -->6 p: E2 t4 g8 Y; L) U2 K5 ]! m
- <tx:annotation-driven transaction-manager="transactionManager"/>
! y# p+ Q7 r' e7 e+ H -
6 ]& M. n8 C' w/ C5 y* T - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->0 X" N# E0 P! X& Y6 _
- <context:component-scan base-package="com.s2sh.annotation" />
5 x: r4 e5 E! m7 v6 x& [3 c, ] - </beans>
复制代码 struts.xml:
5 H. L. w1 c8 r& v" U+ e- m- <?xml version="1.0" encoding="UTF-8"?>
6 k' I: k! U: y' X1 D" R- \! u - <!DOCTYPE struts PUBLIC, ~; _5 O% `( _( K6 u
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN": Q5 X/ C; Q( J7 R+ ~7 i& J+ y: J$ w
- "http://struts.apache.org/dtds/struts-2.3.dtd">% G3 P# A2 k0 v5 F1 P
- <struts>/ R9 C: c# U& H$ d% D2 M$ ?3 c
- <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->7 Q) X! G- B1 [8 w% L/ ~6 g2 D; ?
- <constant name="struts.objectFactory" value="spring" />
7 v- f5 k0 M+ m4 K( T; N% i - ! O8 j; e2 V2 z! {# L2 X3 [8 I
- <package name="base" extends="struts-default" namespace="/" abstract="true">. c# a0 A1 _! I0 K" ^7 G ^2 K
-
5 [7 R$ y M3 e - </package>& U5 |) | f3 k4 R$ I
-
~3 L, Q+ \- m3 m! ~; j6 | - <include file="config/struts-bookInfo.xml" />
: ]9 q$ G( N, Q8 R5 {! ]8 ~8 ^ - </struts>
复制代码 1 E+ ]. x) ?+ o/ {8 x
S2SH三大框架采用注释整合项目案例
# x9 Q9 ~% U7 R0 r/ J解压码:
6 }. d0 ]( _3 r5 Y
- ]9 q9 A$ m. A5 a: Q' E1 M) E5 o
9 H5 H% F4 a) a; D. c0 Q* l6 H4 {% v; {+ K- f/ D+ h
|
-
总评分: 帮币 + 1
查看全部评分
|