TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。* H1 _4 O/ W k# @; e
1 W, {- [1 l+ R# |1 lBookInfoAction.java:
! I% }8 F! l- ]* @/ G- /**
0 A8 P- ~/ `& g3 ^1 v! c - * # O, H( o) b2 n: x1 O/ d+ w0 V
- */
1 t" _4 i J- A \ - package com.s2sh.annotation.book.action;% n' X% \' C5 m$ q
- import java.util.List;
& {# [3 @% U( m. g - import javax.annotation.Resource;
# j* W5 x, o2 H2 s* z6 d - import org.springframework.stereotype.Controller;
0 \* R2 L7 F; ], o S' @) @& g - import com.opensymphony.xwork2.ActionSupport;
+ W Q1 j x2 c* @- _ - import com.s2sh.annotation.book.service.BookInfoService;) \# D2 q) L- O# m% @' E( K* C$ L% |
- import com.s2sh.annotation.entity.BookInfo;- U& D8 Q( V6 r; ~6 n! s2 s
- import com.s2sh.annotation.entity.PageBean;3 l5 Z) ]5 L' B1 y5 h8 L5 z
- 7 [3 n7 U, Y- ^6 w( w# n' B( I
- /**
( m, b F1 h2 Z' {0 L' K ?% I" S! w - * @author Administrator
5 J; O4 k: z# }0 G9 a - * k2 k1 \% S) t8 m, M: B/ \' k
- */
. E# P. g0 ?; K, ` - @Controller // 标注控制层对象
; D' {1 ~. L' { L - public class BookInfoAction extends ActionSupport {, Z* @& Q G3 }0 {, Z, w
( Z/ s. X9 s9 k( A. c# K* N- private static final long serialVersionUID = 1L;+ y( x+ z6 f& k, a( j/ N
- , @8 R, t; _3 P6 c: G
- @Resource(name="bookInfoServiceImpl")
7 X$ ~# T1 M# Q6 h" ~) y! K - private BookInfoService bookInfoService;
3 w; \3 [8 L- N& i# e" @ -
/ F% ~& v% R# Y7 B/ H( ^: w4 g - private List<BookInfo> books;9 l7 U/ K6 z* {" w5 E% e' y
- private PageBean pageBean;
) r- y+ S' F! ^: p9 {- W - private BookInfo book;
& R- O5 G* d6 ], e- |: U( _! l8 d - ; G1 Q j! W' x4 ^/ i C7 \; c- ^0 O
- 7 ?6 Y' f" _" ~3 p: F
- /**
/ n/ i. v+ n" R# M, I3 D* G) D - * @return the book" s+ `3 h5 z7 S8 D n* ^- {+ U
- */% Y, R. e, A2 u+ R6 B
- public BookInfo getBook() {
2 Q8 h" @! f$ x R2 x - return book;
& U' \3 ^( L h0 F - }
& Q l9 ]; A9 _9 y - /**% ^- X" e: T" b" E# X: g4 k
- * @param book the book to set
4 W* J: s+ Q4 ?, p - */
& x: H( U/ g( n: B" a - public void setBook(BookInfo book) {
, Z) b0 u+ X+ A p/ Y& { - this.book = book;) E5 E6 L' m; |
- }) v3 E( N6 C7 g) J0 D
0 v6 X6 O6 \6 c' z1 O! q- /**
- d3 Z) Y$ f2 I6 a2 J# F% P - * @return the pageBean
4 Y. C* A& i6 X" w7 F/ y' v3 d: d0 e - */& n1 f" A2 }! e5 ]% z- q4 D
- public PageBean getPageBean() {
- w3 f/ m4 S3 O - return pageBean;
# j$ }$ V% V( D1 s* d3 h2 K0 I8 J; P; B - }
! e6 z% d8 y/ ^) v - /**, z+ m1 R- D3 f9 K$ u2 [& u1 u
- * @param pageBean the pageBean to set
1 z$ J+ Z" V" T$ p - */
6 G4 u; S L! r2 r! S6 ~ - public void setPageBean(PageBean pageBean) {
7 ~8 X0 s+ U; C+ C. \' U) g/ W: G - this.pageBean = pageBean;1 ]2 C. y0 b1 r Y0 x/ @
- }6 M7 P) `) M0 z
4 h' e# a6 Y7 h) Q6 _- /**( G+ x+ ~8 m) e- F+ m+ W/ I
- * @return the books
, y, u% R* q i: ^# a: l - */
) ]8 D4 f- \0 }# ?, b" E - public List<BookInfo> getBooks() {+ n, q" j7 c7 B+ S- q& m8 O( s
- return books;
/ e N0 P. J- M h - }6 Y6 h& z# \% u9 ~
: m5 c; w) n% y. p2 Y( h- /**
! A4 |& j! E# ^; l$ N8 z - * @param books the books to set
0 n3 W3 \* f0 |' b - */; x+ E- Q3 I0 e4 S8 {" O
- public void setBooks(List<BookInfo> books) {
5 n- {' V9 o' N3 j$ z - this.books = books;
5 f- x& q. I# h' z( U8 O- d - }% f" k: u' S- }
. X0 f5 u: U: z+ s- public String showBookInfoList() {
1 E5 x+ P# ?) u7 o - pageBean.setPageSize(5); c* D7 Q/ d1 Z/ Q- \ L+ z. I# y
- this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
* X! |& Q" A P: E3 v - return SUCCESS;
7 o% U- A; y! M! { e, N - }2 M: U5 v! O# u+ B+ E
- 9 p7 S+ @) ]. C" q1 |( m; T
- }
' U) e/ @+ @3 m/ } X
复制代码 applicationContext-persistence.xml:
, j& {' S' o7 f% M- <?xml version="1.0" encoding="UTF-8"?>' q. g0 }7 d+ d7 ?# \% M" r( ?
- <beans xmlns="http://www.springframework.org/schema/beans"' b/ j# P, c+ b
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance") ~; v2 H2 L/ H) {
- xmlns:context="http://www.springframework.org/schema/context"% t& ], X5 U: N/ ^
- xmlns:tx="http://www.springframework.org/schema/tx"
: f p9 G, z% X - xsi:schemaLocation="http://www.springframework.org/schema/beans & a+ ]; K U- Y& ?# ?( u
- http://www.springframework.org/schema/beans/spring-beans.xsd
9 x/ c, Z$ @5 `( h" A0 Y - http://www.springframework.org/schema/context
- ?1 f$ ]; [' m8 X, d - http://www.springframework.org/schema/context/spring-context.xsd
( C2 T! J |' x$ Y! f { - http://www.springframework.org/schema/tx & E* {( u: w. U |& c
- http://www.springframework.org/schema/tx/spring-tx.xsd">
% P* z7 i9 L$ F Z$ _! R2 x$ ]1 q! r - <!-- 使用注解方式装配属性文件 -->
# q; ?0 L3 U& G+ o - <context:property-placeholder location="classpath:hibernate.template.properties" />
* i. e8 h8 q4 z1 R' a# E5 l. H - <!-- 配置数据源 -->/ x/ p+ p6 ?- T- Z# e8 ]+ Y
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">' N* Z# A$ h5 A) a9 ]1 ?2 I
- <property name="driverClassName" value="${hibernate.connection.driver_class}" />3 W" U/ o/ N9 N+ _
- <property name="url" value="${hibernate.connection.url}" />2 z$ a- U8 B% i
- <property name="username" value="${hibernate.connection.username}" />
' S p, G. `6 z - <property name="password" value="${hibernate.connection.password}" />
& T0 i% Q' P7 s2 o - </bean>
& G% n* f1 t4 E; v$ m$ _ - ( o2 e% g" `/ J4 Y- a& y1 z
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
2 P1 v1 p9 i; c, f; | - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">$ M, F/ n. I; g% n
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->: ^: |: ?9 P% ^& p2 p3 \5 L0 b
- <property name="dataSource" ref="springDataSource" />
! j# p$ ]# _6 J) S5 U6 E8 J - <!-- 扫描装配需要管理的实体类 -->6 M3 {1 }( y- h: u0 ]9 B
- <property name="packagesToScan"> Z4 z/ Q' j1 h
- <list>
6 |4 L/ e( Z# t6 f% R - <value>com.s2sh.annotation.entity</value> G Q7 G! L7 u9 e9 {
- </list>
: q3 R* w$ }# D - </property>8 w/ C% Q4 \2 |7 S/ K
- <!-- 配置额外属性 -->& @9 g4 k0 |( R2 }. j- d
- <property name="hibernateProperties">1 P+ W1 g5 Y; u
- <props>
; z4 q" `* Y5 Y3 k- \ - <prop key="hibernate.dialect">${hibernate.dialect}</prop>; e+ J7 E, W- D5 R6 c' K& p
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
1 ]* ~. B, F* ]- p& H6 _% W - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>: Y) O* v% b) ?4 V! y) w
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
. @( H8 ^. ]+ K - </props>
7 C: o- E; q3 i+ ~! Z0 x3 m - </property>. }, `- ~# P' K8 i8 W
- </bean># L" w8 ]1 ?1 o* i6 {4 M
- 2 X3 u8 ~$ d& j. D
- <!-- 配置事务管理器 -->
" ]' T, E- J( @ - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">2 H9 ?0 U9 V7 r5 u) j
- <property name="sessionFactory" ref="hibernateSessionFactory" />
. @) i* @% x7 _) p2 X# p - </bean>1 C( ?3 U- Y' d; _1 t
-
: N$ i: j. U- Q' F6 ]$ y+ J! x - <!-- 使用注解形式来管理事务 -->
G) J. O$ i; ^* Y* J - <tx:annotation-driven transaction-manager="transactionManager"/>' |2 u+ ~+ ~) V z, L; {# u2 Q
- 5 Q& `5 A2 P: O5 R5 q# F
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 --># O9 \- y) n1 d7 @8 h
- <context:component-scan base-package="com.s2sh.annotation" />9 a$ I( d* l9 X0 F T" ?
- </beans>
复制代码 struts.xml:# D* z# j. d4 O6 D1 W" @
- <?xml version="1.0" encoding="UTF-8"?>$ R" ]$ f* K% N4 A& X
- <!DOCTYPE struts PUBLIC* Z M) o9 K" Y& s
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
! _4 Q: f; q5 f - "http://struts.apache.org/dtds/struts-2.3.dtd">
- n, D0 S! G+ E2 ^ - <struts>' x: e( U" ]# Y& h* e: m$ k
- <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->; D0 t1 | p( e" o' C7 C
- <constant name="struts.objectFactory" value="spring" />3 E/ U# y B0 x) g0 u5 Z7 `1 a4 _
- * A2 I% m0 s$ m5 w/ a/ `: M; L1 a
- <package name="base" extends="struts-default" namespace="/" abstract="true">
+ G- k' h# a7 b; K' } Y1 ]- y -
W6 I5 S. ] a Z: Z - </package>
5 S0 l& O. A9 Z7 ]) I& l6 M$ k/ ? - % [- p/ X8 j# }- r7 b" f+ r& d6 I
- <include file="config/struts-bookInfo.xml" />
- Y" [6 w. R( t3 H; x+ V - </struts>
复制代码
$ z" f# Z5 G+ [ k( ZS2SH三大框架采用注释整合项目案例
9 e& Z7 F9 f% q# i. i8 l }9 X1 B解压码:. p9 S) n d$ H5 ]; o, z
( y& p( {0 s4 g w, x) ^5 Y0 M b# v$ X) `3 e$ i" ?
% H5 L: g% A& u
|
-
总评分: 帮币 + 1
查看全部评分
|