TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
spring+hibernate+struts2集成,注解方式配置。) l" \, u; g& l, L, J3 B( G
! s4 t. {+ @; w v4 q- E( V/ ?7 SBookInfoAction.java:
# k2 U+ J4 B8 K' R+ N. A W- /**3 ~. ^: I" I& q+ R
- *
" `6 h( q; p( \ d - */7 m1 q, D& D* N# X% V
- package com.s2sh.annotation.book.action;
' @2 f! o' n- h2 O - import java.util.List;' [, Y3 L+ G/ M
- import javax.annotation.Resource;
2 s' Z& Z# F; b2 d# P - import org.springframework.stereotype.Controller;
# a; H) E- r a/ `- A/ S- D - import com.opensymphony.xwork2.ActionSupport;
W) h9 j$ k3 d2 A. v. y- C1 I - import com.s2sh.annotation.book.service.BookInfoService;) q4 \. @; d- ], ?
- import com.s2sh.annotation.entity.BookInfo;
6 T7 ]! m! F h1 {2 R8 m - import com.s2sh.annotation.entity.PageBean;: M+ V- _( `4 Z+ @' x
) z" G) f _3 _9 c- /**4 d9 {# b' `3 y; l) N% F" a
- * @author Administrator! A( ?' F: G) [$ ^* y* x
- *1 T# U# S3 B% X- H
- */
& @/ [8 i u. U2 G% r4 g- k - @Controller // 标注控制层对象1 {. M5 U2 `7 Y8 ~1 n$ @
- public class BookInfoAction extends ActionSupport {$ T* F, N% F* N, `& f
T7 @; a( @) ^" u- private static final long serialVersionUID = 1L;
) n( T8 {3 y) r1 i. j5 Q! u/ X$ Y -
) [7 \* \* S/ Z" O - @Resource(name="bookInfoServiceImpl")
% q$ k! {) M$ j( z1 b! S - private BookInfoService bookInfoService;
8 a1 O! Y ^3 r8 C -
" B7 J) o) M( d' m! t$ `* F - private List<BookInfo> books;
& y. l1 y4 p; ?" K2 P- M - private PageBean pageBean;
/ x2 V8 _1 n. w) @2 `7 o+ r8 D - private BookInfo book;
2 b" r. ]4 r7 n+ N, j( ?( m& j -
1 x L* E/ K. n3 ]. q3 V) i - ( e8 N! r; ~% x5 e1 _& R
- /**
! s9 Y! w8 W1 u( P% T! l% v - * @return the book
8 ]( a3 g L; s9 `9 j; F2 y - */6 J) r7 X; r& N0 j3 P" t' H$ D8 S
- public BookInfo getBook() {- G, h, T2 ?) _. j, ^6 [! v3 W
- return book;; y3 Z9 y7 J0 R; J% M& D8 U, Q
- }
' c: [& ]8 C- }4 O8 ^# h - /**3 T6 p+ y/ U" R8 ]; _9 W
- * @param book the book to set. _; U' ~/ r' b8 o* a) K, {" A
- */, h* J$ ~# b+ S8 s0 a$ r
- public void setBook(BookInfo book) {
: Y5 t" }& @8 X9 z4 F6 p - this.book = book;
9 r/ X4 z" S/ o; Q9 N. _- S/ F% J - }
0 Z! P& r5 v+ O# Q! [2 r( i( {( _
* p( h/ G$ C. f* d2 y- /**" J$ A+ h6 j9 k+ p+ F! p
- * @return the pageBean
. Z2 U% {6 j2 W' x/ j* [ v6 { - */
2 [' W1 V' R: k - public PageBean getPageBean() {" M) C0 F# a5 P! o1 C1 ~
- return pageBean;( J/ L( ^5 H3 f+ N! N" F, c
- }
% L9 Y- B4 Q- K- q - /**
5 P0 f% Z& M& v5 m$ n9 d - * @param pageBean the pageBean to set
4 T! l, ?8 U: t/ z! H5 T - */
! W' Y9 E2 L2 e8 y. C0 |5 v - public void setPageBean(PageBean pageBean) {
3 |. A Y+ Z) X6 V - this.pageBean = pageBean;
0 }7 H# O: ~9 L/ L& m% l - }9 R0 w) ? D. y
- 0 j5 q2 s- q6 m5 Z8 c) G9 w; Y
- /**
; M) h" X! A/ |0 |% O - * @return the books
0 ~+ n$ \$ t2 U% O! r - */
8 L$ b5 U. a) X* n) `$ h8 i - public List<BookInfo> getBooks() {
- p& n! p* H% z/ i! d - return books;
f. Y2 a6 G& R; k - }
$ `$ I; q' P B - Z- _; Q3 }2 v2 \* T/ P
- /**
' u$ ?6 I, `/ j7 T6 N - * @param books the books to set
0 |! [% g0 B! Q6 u - */
) S! J$ p t. `5 J - public void setBooks(List<BookInfo> books) {, d" v$ c6 `9 x. ~' i" W. l9 r4 Q
- this.books = books; [- ~7 Q) H/ i% j v
- }
, j" R7 `* n, d( e9 E7 V3 K
8 Q4 c# ]- p7 `- public String showBookInfoList() {( |& u" m# J; r
- pageBean.setPageSize(5);
3 q; x7 \: m- w V: d - this.setPageBean(bookInfoService.getPagingByBookInfo(pageBean, book));0 ?7 O$ }4 ]2 T$ I, q
- return SUCCESS;1 \+ w5 ?+ S% i& X
- }
/ {! l e' |3 l6 ?2 w
/ e# @9 M) k& X# _- }: s9 J2 }/ R. t/ A/ v+ v' L
复制代码 applicationContext-persistence.xml:8 T& q+ e" K2 N$ U
- <?xml version="1.0" encoding="UTF-8"?>
. ^5 @+ l3 l7 o9 L" d8 \ - <beans xmlns="http://www.springframework.org/schema/beans"6 M1 ]4 y2 i; h0 S/ M: b
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"* M! E' t! f) i- k8 N
- xmlns:context="http://www.springframework.org/schema/context"
8 D) K8 _! Q. K- `/ z$ e - xmlns:tx="http://www.springframework.org/schema/tx"
: q% Z" R* {* l% d" Y - xsi:schemaLocation="http://www.springframework.org/schema/beans ( Q& d; E' M* d+ d
- http://www.springframework.org/schema/beans/spring-beans.xsd
& `6 l2 h/ ?: n7 z# J- B% q1 R - http://www.springframework.org/schema/context
+ p b- d1 N d& u1 y" L- Y - http://www.springframework.org/schema/context/spring-context.xsd
/ N) q) {1 L/ M8 E - http://www.springframework.org/schema/tx
* N$ [8 _6 V; `; h8 _" e" t8 G - http://www.springframework.org/schema/tx/spring-tx.xsd">
" J. G t8 l1 e2 B2 T2 W# r - <!-- 使用注解方式装配属性文件 -->
. |1 w5 a3 v5 B0 _0 l( T/ ~+ ~ ` - <context:property-placeholder location="classpath:hibernate.template.properties" />
W* x- D, L; c3 ?; o - <!-- 配置数据源 -->4 F7 ~0 l* O! H5 D0 `2 h& |' v
- <bean id="springDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
8 s& V8 L1 D+ p+ M7 Y5 y! A - <property name="driverClassName" value="${hibernate.connection.driver_class}" />
6 R$ |( q) ~1 X( t - <property name="url" value="${hibernate.connection.url}" />/ a8 @8 \5 B9 }
- <property name="username" value="${hibernate.connection.username}" />; d! H$ [& U7 n$ ]7 Z
- <property name="password" value="${hibernate.connection.password}" />
( B3 b! }/ T9 A' d - </bean>8 V6 @0 S( }. k3 w
- q# l5 a$ R' u: E3 V1 B
- <!-- 整合sessionFactory对象到spring容器进行管理 -->
# \6 J( U$ ?# L, P2 @ - <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">4 ^0 ?* M; d! q/ Q& ?
- <!-- 把spring容器中的数据源注入到dataSource属性中 -->: f; \0 N& K8 K* m _4 l* V
- <property name="dataSource" ref="springDataSource" />7 ]; n/ d" Z2 U& U) ^
- <!-- 扫描装配需要管理的实体类 -->
" g# D& F! S( t& y& _) O - <property name="packagesToScan">
- t" [$ N7 c E% G. Q/ l - <list>
0 k: a% y# x5 z - <value>com.s2sh.annotation.entity</value>
- X& k# W1 o4 a' D0 D( T, F G - </list>
' X }6 P+ o5 D/ a: d) B - </property>
, P5 r8 n. b( Z& n7 b5 } - <!-- 配置额外属性 --> A$ y, P; M* _" a- m% D3 \+ \
- <property name="hibernateProperties">
5 h. _( A: x6 N' _9 f: H. y# G& C8 C - <props>6 f. G' z/ l- _6 v+ z$ ?. ^
- <prop key="hibernate.dialect">${hibernate.dialect}</prop>
7 B, f0 _3 {: ~ k - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
" ]: @/ X: P0 y7 X - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
; @- N- d& C6 z* H) }& l - <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>8 H/ Y! B( g; t! E) |
- </props>* f; s2 U Q3 O. E' R
- </property>0 M( d5 \$ R: L9 @/ N0 }
- </bean>+ h, u O" E7 s5 k7 }/ _
-
2 L* U# [; u1 ?% x8 C5 f8 K; B - <!-- 配置事务管理器 -->
! }9 F( s" e9 G7 Q- J - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">* d& u: `, q+ \2 k' M, q
- <property name="sessionFactory" ref="hibernateSessionFactory" />: ?2 D b' {- p5 B
- </bean>8 ~9 L# K5 x4 D0 _
-
3 @2 @1 p( M2 K6 h; z6 |. a& W - <!-- 使用注解形式来管理事务 -->. y, a. h+ x) h) g3 s
- <tx:annotation-driven transaction-manager="transactionManager"/>5 u4 A1 k0 {- X
-
$ F; c! [4 P! J. W8 l' G - <!-- 启动自动扫描方式将需要管理的组件纳入到spring容器进行管理 -->
S# H% b1 r" F/ c b8 y) L9 h+ i - <context:component-scan base-package="com.s2sh.annotation" />. H5 Q7 O2 n3 N
- </beans>
复制代码 struts.xml:
5 v X) X* O4 u' Q9 w% ` ~% v3 C; l: t- <?xml version="1.0" encoding="UTF-8"?>* w- Z ~3 M5 D d, I! v7 j
- <!DOCTYPE struts PUBLIC
+ ^ n- `, l' X; N2 P) f" F' ` - "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"& p/ ~5 V. ^& F3 E; y/ U/ R% U3 j% h
- "http://struts.apache.org/dtds/struts-2.3.dtd">
8 L( m8 _4 k9 D7 y7 \) D - <struts>3 V0 ^; f4 l+ B ]+ c' M) |
- <!-- 把struts创建Action对象的权力交给Spring容器进行管理 -->
4 y: b/ Z2 _, o' r) d. a - <constant name="struts.objectFactory" value="spring" />- K% f8 _# [! Y4 S9 d5 l" y4 h; ^
-
/ J, y3 ~5 [" T9 L: ] - <package name="base" extends="struts-default" namespace="/" abstract="true">5 p5 C2 w6 a) k3 l% v7 H
-
- x0 D. T6 \) d" [2 C) Z r9 ~ - </package>
% L! m! `6 M, | -
' m* \, s( L9 s* _2 m* j+ Q- D y) m - <include file="config/struts-bookInfo.xml" />
. F) f) Z9 R$ D- E3 | - </struts>
复制代码 ' y+ r; o' Y3 c
S2SH三大框架采用注释整合项目案例
3 q( C% g7 L7 B2 l6 `7 N解压码:0 q: ?$ D$ T3 B% M
1 v5 \; F0 L7 s7 C* B
1 } {( N* n. N; f3 c) v- U# o6 x. K$ ]/ O
|
-
总评分: 帮币 + 1
查看全部评分
|