TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。/ G8 k1 A" p$ t: o2 D3 t
5 j/ Y& C, b' Z. r% S( I
BookInfoAction.java:
- q1 f% }+ r4 [9 X- /**
+ k* ?4 k3 R3 S - *
/ L1 q9 K5 I, q$ N2 E - */
7 D- Y* O9 M8 z/ y; P3 W9 y - package com.s2sh.annotation.book.action;; z/ K4 n6 B7 G
- import java.util.List;& T! |6 I% Q' @+ c% i, O
- import javax.annotation.Resource;7 G( u2 n, Q5 g. z
- import org.springframework.stereotype.Controller;
3 B: y0 S7 M% P - import com.opensymphony.xwork2.ActionSupport;! j" C% y) b7 S* u7 X$ m% Y6 S1 B
- import com.s2sh.annotation.book.service.BookInfoService;
8 ]" ~( x e b7 H0 n - import com.s2sh.annotation.entity.BookInfo;8 R$ b7 w, J& g' e I
- import com.s2sh.annotation.entity.PageBean;: c- ~: g0 Y* m& Z
: F$ z& i% V7 |% V$ ^+ |- /**
) H5 X2 P n$ P P) \; a - * @author Administrator; E- o0 I9 L5 O; O2 W& s" Z
- *
) ?! [ O* o( n. s - */
) @: C$ g3 F- q1 ` - @Controller // 标注控制层对象
( D* S- H! k. F# H3 J- L - public class BookInfoAction extends ActionSupport {3 D$ d9 L4 a6 R2 G" j3 r
- $ V5 M7 Q2 {2 q( F
- private static final long serialVersionUID = 1L;* E- P4 ]3 \% F; e1 `4 T8 I
- " s {/ n( X' k. _+ ?/ _- N: R
- @Resource(name="bookInfoServiceImpl")
. @" Z0 m* P g* e - private BookInfoService bookInfoService;
0 v/ l, i0 e+ @* x; a -
w& G/ z! |' d+ Q - private List<BookInfo> books;
0 x6 B, `6 \- U - private PageBean pageBean;
8 t- ]! r/ a- c' d - private BookInfo book;5 f1 Z6 A; W. m( A' C' [" A9 ?
- ) v0 o" M5 r: ?! q" e
-
( c8 P6 @* ~) e7 [: u+ K - /**
" k+ M& ]7 X3 f( v - * @return the book
( S6 j# g) P" Q" v - */ L: l3 Z4 N3 x8 ^3 ^6 r" t
- public BookInfo getBook() {2 ?) G7 ]2 }: k
- return book;% l1 H( q; O" b
- }
$ @( ^" U# y& L - /**5 U5 { r0 h0 }, g; V1 {
- * @param book the book to set6 I2 v. p, z4 S! [7 [ q
- */
7 c( }+ R; ?8 y2 W7 r6 { - public void setBook(BookInfo book) {3 c: u0 x1 g5 u/ X8 y) X. s) Q. n
- this.book = book;
; r$ ?2 K7 X* N8 K9 f - }
( Y* o c5 o8 Y2 q - 0 x, b- Q4 t9 I0 }; [8 s; ~' G
- /**
9 ~4 A' P% n5 q - * @return the pageBean
" s! _& A5 |3 I; r - */
( M- t1 X: [( U - public PageBean getPageBean() {
& z0 C9 o& L8 y& w2 s- D9 } - return pageBean;$ @& @) v5 W! _6 W9 \1 N
- }1 s$ d) i; u8 [
- /**
' ?- Z$ Y) I/ |, @. c0 J2 L) ]* W$ r2 F - * @param pageBean the pageBean to set
* E' f! R; S5 R# ^ - */" x* B- S. `3 a$ d1 H
- public void setPageBean(PageBean pageBean) { C0 M% Y, `, @! n. D3 i+ L
- this.pageBean = pageBean;8 m( ]: Q' @, v6 A
- }
2 r/ ^) ?5 k* B) G3 F7 @: S$ D7 r - # c; ]% d4 e4 w, I1 S0 w" K2 A% V
- /**
! J4 y! Z+ ^. ^5 R - * @return the books
* W8 A4 ~& d; J7 E! j2 Q - *// c/ Q: s8 u. x# [; Z1 l. m
- public List<BookInfo> getBooks() {
1 K% l! t) M& ~% V - return books;
k" \% B1 n$ V' v - }3 Y1 B% y g2 s2 L
3 [3 B! k( K$ _; O- /**
) e& |( c5 W F - * @param books the books to set
( R; t7 c8 t" P - */3 B" W f: y% b0 ^- F2 ]1 E" A# Z
- public void setBooks(List<BookInfo> books) {
0 }% u: V1 n+ L+ p3 i - this.books = books;
5 _" P r7 \( Q+ Z" q0 e - }1 Y; f, F" y2 e# F" M. `5 ]# V" n
( s2 y" D3 ]* ~% D- public String showBookInfoList() { h- }9 U6 e5 C) x# X
- pageBean.setPageSize(5);
6 Q& c1 x' u+ B. k* G* i: q% ~! R - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));
1 s7 G' ^8 h# h8 d - return SUCCESS;7 h1 _, {8 u% g7 v
- }
8 x' U% ~: P; ~7 m/ W0 [! V - 9 S& O% X) x4 X& M2 V4 c6 D
- }& I( s; B7 K) \: x$ s" n% P T# }
复制代码 applicationContext-persistence.xml:
1 s- s5 c$ w8 T3 s- <?xml version="1.0" encoding="UTF-8"?>
* W0 r# _% }( d$ ]# y0 P" B/ | - <beans xmlns="http://www.springframework.org/schema/beans"
4 F9 o# Y% I4 A - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
% h$ `! Z' c0 l% k2 c' w - xmlns:context="http://www.springframework.org/schema/context"
6 ~* [( K, C3 ^1 i9 o - xmlns:tx="http://www.springframework.org/schema/tx"
% \- a! b1 l2 @9 i( y0 @ - xsi:schemaLocation="http://www.springframework.org/schema/beans
! w" B! Q6 K! y/ Q - http://www.springframework.org/schema/beans/spring-beans.xsd
2 w3 E6 g/ q6 R - http://www.springframework.org/schema/context : A* M5 N# x0 H! X3 k
- http://www.springframework.org/schema/context/spring-context.xsd 5 `0 q w9 K' `9 q/ _
- http://www.springframework.org/schema/tx
6 z+ {: {, i1 d4 y1 e% j - http://www.springframework.org/schema/tx/spring-tx.xsd">4 x" O! Z- m9 J# @+ u& p0 B
- <!-- 使用注解方式装配属性文件 -->
# P0 y+ T7 ~" \8 H# _ - <context:property-placeholder location="classpath:hibernate.template.properties" /> L: e. U1 c E9 S+ |# I, @
- <!-- 配置数据源 --> J2 W/ y _9 F# s# x/ I
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- s) W) H' D& b- \ - <property name="driverClassName" value="${hibernate.connection.driver_class}" />
3 j" I% ?- }4 {4 V4 S - <property name="url" value="${hibernate.connection.url}" />6 q* z* B e2 W0 K1 J$ q
- <property name="username" value="${hibernate.connection.username}" />+ _2 h* w8 A3 D9 h% G$ I! B) @- |
- <property name="password" value="${hibernate.connection.password}" />/ P# U; o0 ]; I/ `; W: h
- </bean>; g7 P: {4 S; {. F
-
, f" h# ?' c# F - <!-- 整合sessionFactory对象到spring容器进行管理 -->- G+ _7 e3 _, K8 K) P% @! U7 y( F" c
- <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
3 Q* P3 M. n- @" ]8 c V' R - <!-- 把spring容器中的数据源注入到dataSource属性中 -->' e: g7 e+ O" P% [3 g1 W
- <property name="dataSource" ref="springDataSource" />! t% ?6 ~% S" u. D- T* i5 w( O
- <!-- 扫描装配需要管理的实体类 -->: J& U! D& }3 E. v, L2 p
- <property name="packagesToScan">3 i9 T. v n \, D! B0 a# ?) ^
- <list># f7 P" f8 ?( E( D6 h. J& b
- <value>com.s2sh.annotation.entity</value>" W/ R% T& ~# n( ^4 C3 e- K
- </list>0 Q: v/ ~3 N) H" z( P/ A
- </property>
9 p" V. b: L& \ - <!-- 配置额外属性 --> S/ W7 u: Z6 @
- <property name="hibernateProperties">
0 |' X1 _1 }" K0 w - <props>
0 w2 s3 Q$ }( i9 o0 F/ U - <prop key="hibernate.dialect">${hibernate.dialect}</prop>; ], k9 u) i; B
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>7 O; }3 m7 _6 v5 L1 E i$ N( {1 `
- <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>3 @0 s+ c9 I. J( d, j1 n
- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
" I& u- ]9 v: p3 f5 l7 f# p - </props>7 Y8 r. Y" C! {7 R5 H
- </property>
0 w5 Q9 J# w- \ ~' B - </bean>2 V! A8 |* ~- x7 p4 o
-
4 r; w2 I, U2 s" Z - <!-- 配置事务管理器 -->
' l9 d4 d _- u: C3 D" F3 C - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">; u. v+ \ y# @8 x" Y* y2 L( C
- <property name="sessionFactory" ref="hibernateSessionFactory" />
6 s. G. T% L" H) b5 ^+ Q - </bean>
4 q( z8 c% B7 p! ~& M' o$ d" ] -
4 x2 T7 c! X& {' i* P* _, L( H j - <!-- 使用注解形式来管理事务 -->
% x8 H F. V& F( u+ U3 | - <tx:annotation-driven transaction-manager="transactionManager"/>
* B# K5 z3 K& c8 V( s - , v+ u7 R' H" J S; i" n
- <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->% P4 o3 { h3 K
- <context:component-scan base-package="com.s2sh.annotation" />) ~( o$ y- c$ J/ n3 J0 L
- </beans>
复制代码 struts.xml:
% X1 m& E$ @# P" F% v' |- <?xml version="1.0" encoding="UTF-8"?>
2 O- H# p: q, [$ T' t& T - <!DOCTYPE struts PUBLIC
* L9 q! {- A/ A; i2 C4 R7 s6 K - "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"3 u% d2 t7 r9 m% J+ J, ^( F8 U, v
- "http://struts.apache.org/dtds/struts-2.3.dtd">
8 k) a! Y% u5 ]8 \+ p2 w$ N/ W - <struts>4 X5 ^# a( b- ]5 d9 c. z
- <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->/ W3 F) h7 {7 }* r* b1 m
- <constant name="struts.objectFactory" value="spring" />
5 M, n f2 V* p& O! z7 D% N- x; x -
- t( b9 R0 f& D$ e6 Z, k - <package name="base" extends="struts-default" namespace="/" abstract="true">% L! {0 G" N0 [" J6 r$ ]
-
: p( e1 z5 K) ?2 K$ F9 H - </package>
8 t4 {! o( s: m# v! p" p - 1 {7 ^9 L- D) X$ S+ w* a& ~% r; N
- <include file="config/struts-bookInfo.xml" />$ T% K# y7 f* ~; x/ N
- </struts>
复制代码
6 k0 b* r1 R$ TS2SH三大框架采用注释整合项目案例+ P+ W- {* |1 }- E$ [5 H4 B3 B' u
解压码:
: {+ Y8 X* G& E7 S) q; T9 {2 n1 M( K8 C
7 L, c4 `7 m& p' D4 b" t
+ @4 q# e2 G% q1 p
|
-
总评分: 帮币 + 1
查看全部评分
|