Title: Struts2 Hibernate Spring
1Struts2HibernateSpring
SpringHibernate
SpringStruts2
Struts2HibernateSpring
2????????
- Action??Web??,??????view,uptate,delete???,?Spring
???? - Manager????,?????????,????CRUD???????????????????
???,?email - DAO???????,Hibernate?????????CRUD??????
- ????POJO,?Hibernate??
3??????
- 1????
- 2?Myeclipse?????Hibernate??
4(No Transcript)
5(No Transcript)
6????????DB Driver(???????,????Spring????Hibernate?
????)
7????Create SessionFactory class???,Finish??
8 9??????????,????WEB????
(???????????)
10(No Transcript)
11(No Transcript)
12?????applicationContext.xml,????hibernate.cfg.xml
??????????????????JAR?asm-2.2.3.jar
?????
13?????????
gtgtgt
????????
??????
??????
??!
14- ??applicationContext.xml?????????
??????
???web.xml???????? lt!--??Spring???? --gt
ltcontext-paramgt ltparam-namegtcontextConfigLoca
tionlt/param-namegt ltparam-valuegtclasspath/ap
plicationContext.xmllt/param-valuegt
lt/context-paramgt ltlistenergt
ltlistener-classgt org.springframework.web.
context.ContextLoaderListener
lt/listener-classgt lt/listenergt ??????/WEB-INF/?
??????????????/WEB-INF/applicationContext.xml
??????????????!
15- ?applicationContext.xml???????
lt!-- ?????(mysql) --gt ltbean id"dataSource"
class"org.apache.commons.dbcp.BasicDataSource"gt
ltproperty name"driverClassName"
value"com.mysql.jdbc.Driver"gt
lt/propertygt ltproperty name"url"
valuejdbcmysql//localhost3306/?????useUnicode
trueampcharacterEncodingutf-8"gt lt/propertygt
ltproperty name"username" value"root"gtlt/propert
ygt ltproperty name"password"
value"root"gtlt/propertygt lt/beangt
????commons.dbcp??????????????commons-dbcp.jar? c
ommons-pool.jar(??????????)
16lt!-- ??Hibernate sessionFactory--gt ltbean
id"sessionFactory" class"org.springframework
.orm.hibernate3.LocalSessionFactoryBean"gt
ltproperty name"dataSource"gt ltref
bean"dataSource" /gt lt/propertygt
ltproperty name"mappingResources"gt
ltlistgt ltvaluegt(???Hibernate?????xml
??)lt/valuegt lt/listgt lt/propertygt
lt!-- ??????? --gt ltproperty
name"hibernateProperties"gt ltpropsgt
ltprop key"hibernate.dialect"gt
org.hibernate.dialect.MySQLDialect
lt/propgt ltprop key"show_sql"gttruelt/prop
gt lt/propsgt lt/propertygt lt/beangt
17lt!-- ???? --gt ltbean id"transactionManager"
class"org.springframework.orm.hibernate3.Hibernat
eTransactionManager"gt ltproperty
name"sessionFactory"gt ltref
local"sessionFactory" /gt lt/propertygt lt/beangt
18- ?Myeclipse????Hibernate?????????????(POJO)
????? ??,???
19?????????
???????,????????
??
20????????
21??ID Generator???!????
22Hibernate?,ltidgt??????ltgeneratorgt??????Java????,???
??????????????,?????????net.sf.hibernate.id.Identi
fierGenerator?????????????,???????????????????????
,Hibernate??????????????????????(Key
Generator)???   Â
1)Â Â Â Â assigned???????????,??Hibernate??? Â Â
??????save()??????????????? ltgeneratorgt???????????
???? Â 2)Â Â Â Â Â hilo??hi/lo ???????????,?????????
??????????? Â Â Â Â Â Â Â ?????/?????????long, short ??
int???????????????(?????? hibernate_unique_key
?next_hi)????????? ?/??????????????????????????
 3) seqhilo?hilo ??,??hi/lo ???????????,?????????
??Sequence?,?????Sequence????,?Oracle? Â Â Â Â Â Â Â
?????/??????????long, short ?? int??????,?????????
(sequence)????
234) increment????????????????????????????????,????
??????,?????????????????1????????????????????????
?????????,????????????????,?????????????,?????????
???,??????????????,?????????????long,
short??int???? ??????????????????????????????
????????? Â 5) identity???????????????DB2 SQL
Server MySQL????????? Â Â Â Â Â Â Â Â Â ?DB2,MySQL, MS
SQL Server, Sybase?HypersonicSQL????????????
???????long, short ??int???? Â 6)
sequence????????sequence ???????Oralce
??Sequence??DB2,PostgreSQL, Oracle, SAP DB,
McKoi?????(sequence), ??Interbase??????(generator)
????????long, short?? int????
247) native?Hibernate?????????????identity hilo
sequence????????????8) uuid.hex???128-bit?UUID?
????????????, ???????????(???IP??)?UUID??????32?16
????????? Â Â Â Â Â Â Â Â ?Hibernate??128 ??????????16
????(??????32 ??????)??????5abefd3890cat33llsica3
ee43bk222 Â Â Â Â Â 9) uuid.string?uuid.hex
??,????????????(??16)?????????????(?PostgreSQL) 1
0)guid ?MS SQL Server ? MySQL ?????????GUID????
 11)select ????????????????????????????????  12)
foreign ????????????????????ltone-to-onegt??????? Â
25 ????,??uuid.hex????????????????????????????
?????,?OracleDB2SQLServerMySql ?,?????????????(Aut
o-Increase ????Sequence)??????????????????,??gener
ator-classnative???????????????,?????????????????
?????,????insert??????????????
????????????,????????????????????(??????????,?????
??????????????),????????????????,???????????????,?
?????????????????,??,??Insert??????????????????,??
??????????????,?????????????,????Insert???????,???
?uuid.hex ????????
26Finish??!
27??????????DAO?? ???????! ?????,???????(?CRUD)
package com.DAO import java.util.List import
com.model.Movie public interface MovieDAOi
public void add(Movie m) public List
showmovies() public void delete(Long
id) public void update(Movie m)
public Movie getMovie(Long id)
28??????????DAO?? ??DAO?????Spring?Hibernate????
package com.DAO.Hibernate import
java.util.List import org.springframework.orm.hib
ernate3.support.HibernateDaoSupport import
com.DAO.MovieDAOi import com.model.Movie public
class MovieDAO extends HibernateDaoSupport
implements MovieDAOi
?????????????? getHibernateTemplate()
???????HibernateTemplate?????.???getSession()?clos
eSessionIfNecessary()??,??????????????HibernateTem
pate??????Hibernate???
29public class MovieDAO extends HibernateDaoSupport
implements MovieDAOi public void add(Movie
m) getHibernateTemplate().save(m) public
List showmovies() return getHibernateTemplat
e().find("from movie") public void
update(Movie m) getHibernateTemplate
().update(m) public void delete(Long
id) getHibernateTemplate().delete(getHibernat
eTemplate().get(Movie.class, id)) public
Movie getMovie(Long id) Movie
m(Movie)getHibernateTemplate().get(Movie.class,
id) return m
30????????????,???????
package com.service import java.util.List import
com.model.Movie public interface
MovieManager public void addMovie(Movie
m) public List showMovies()
public void deleteMovie(Long id) public
void updateMovie(Movie m) public Movie
getMovieMovie(Long id)
31??service??,????????DAO??,????set???spring?DAO ???
?????
public class MovieManagerImpl implements
MovieManager private MovieDAOi movieDao
(DAO????) public void
setMovieDao(MovieDAOi movieDao)
this.movieDao movieDao
32- ??applicationContext.xml??Spring?????
lt!--??DAO --gt ltbean id"moviekindDao"
class"com.DAO.Hibernate.MovieKindDAO"gt
ltproperty namesessionFactorygt bean????
ltref beansessionFactory/gt ????ben
lt/propertygt lt/beangt ltbean id"movieDao"
class"com.DAO.Hibernate.MovieDAO"gt
ltproperty name"sessionFactory"gt ltref
bean"sessionFactory" /gt lt/propertygt
lt/beangt
33 lt!-- ??service? --gt ltbean
id"moviekindManager" class"com.service.im
pl.MovieKindManagerImpl"gt ltproperty
name"moviekindDao"gt ltref
bean"moviekindDao"/gt lt/propertygt
lt/beangt ltbean id"movieManager"
class"com.service.impl.MovieManagerImpl"gt
ltproperty name"movieDao"gt ltref
bean"movieDao" /gt lt/propertygt lt/beangt
????applicationContext.xml?sessionFactory?Hibernat
e?????? ltlistgt ltvaluegtcom/model/Movie.hbm.xmllt/
valuegt ltvaluegtcom/model/MovieKind.hbm.xmllt/valu
egt lt/listgt ?????Spring?Hibernate???
34SpringStruts2
?????????????,???-plugin???jar????????????????????
????,???????????-plugin jar??????spring??,????stru
ts2-spring-plugin-2.x.x.jar
???web.xml?????Struts2?Spring? ??src/???struts.pr
operties?struts.xml???????
35 lt!--??Spring???? ??????? --gt ltcontext-paramgt
ltparam-namegtcontextConfigLocationlt/param-namegt
ltparam-valuegtclasspath/applicationContext
.xmllt/param-valuegt lt/context-paramgt
ltlistenergt ltlistener-classgt
org.springframework.web.context.ContextLoaderListe
ner lt/listener-classgt lt/listenergt lt!--
Spring????action???? (??struts2??????,???????)--gt
ltfiltergt ltfilter-namegtlazyLoadingFilterlt/fi
lter-namegt ltfilter-classgtorg.springframework.orm
.hibernate3.support.OpenSessionInViewFilterlt/filte
r- classgt lt/filtergt ltfilter-mappinggt
ltfilter-namegtlazyLoadingFilterlt/filter-namegt
lturl-patterngt.actionlt/url-patterngt lt/filter-mapp
inggt
36lt!-- ????Struts2?CleanUp? Filter --gt ltfiltergt
ltfilter-namegt struts-cleanup
lt/filter-namegt ltfilter-classgtorg.apache.s
truts2.dispatcher.ActionContextCleanUplt/filter-cla
ssgt lt/filtergt ltfilter-mappinggt
ltfilter-namegtstruts-cleanuplt/filter-namegt
lturl-patterngt/lt/url-patterngt
lt/filter-mappinggt org.apache.struts2.dispatcher.ng
.filter.StrutsPrepareAndExecuteFilter lt!--
????Struts2? Filter --gt ltfiltergt
ltfilter-namegtstrutslt/filter-namegt
ltfilter-classgt org.apache.struts2.dispatcher.Filte
rDispatcher lt/filter-classgt lt/filtergt
ltfilter-mappinggt ltfilter-namegtstrutslt/filter-n
amegt lturl-patterngt/lt/url-patterngt
lt/filter-mappinggt
37??com.opensymphony.xwork2.ActionSupport?ActionSupp
ort?
?????? private MovieKindManager
moviekindManager//service????set???Spring??
private List mklist
//????????List private MovieKind mk
//????????(?Hibernate???) private Long
kind_id //???????ID ?????????????get?set
????Struts2????
38package com.action import java.util.List import
com.model.MovieKind import com.opensymphony.xwork
2.ActionSupport import com.service.MovieKindManag
er public class MovieKindAction extends
ActionSupport private MovieKindManager
moviekindManager private List mklist
private MovieKind mk private Long kind_id
public String add() moviekindManager.addMovi
eKind(mk) return SUCCESS public String
update() return SUCCESS public String
delete() moviekindManager.deleteMovieKind(kind_i
d) return SUCCESS public String init()
this.mkmoviekindManager.getMovieKind(kind_id)
return SUCCESS public String showmk()
this.mklistmoviekindManager.showMovieKind()
return SUCCESS
39lt!-- ??struts.objectFactory?????Spring???
--gt ltconstant name"struts.objectFactory"
value"spring"gtlt/constantgt lt!-- ??action
--gt ltpackage name"movie" extends"struts-default"
namespace"/movie"gt ltaction name"addmk"
class"addmkBean" method"add"gt ltresult
type"redirect-action"gtlistmk.actionlt/resultgt
lt/actiongt ltaction name"listmk"
class"addmkBean" method"showmk"gt
ltresultgtlistmk.jsplt/resultgt lt/actiongt lt/package
gt ??Action?classaddmkBean ??????,??????,??Spr
ing ??action???????!?????classaddmkBean
???! methodadd ???????????add()
ltresultgtlistmk.jsplt/resultgt ??????
40- ??applicationContext.xml?action????
lt!-- ??action--gt ltbean id"addmkBean"
class"com.action.MovieKindAction"
scope"prototype"gt ltproperty
name"moviekindManager"gt ltref
bean"moviekindManager" /gt lt/propertygt
lt/beangt (bean idaddmkBean ?struts.xml????actio
n?classaddmkBean?????
41addmk.jsp ltbodygt ?????????
ltsform action"addmk"gt ltstextfield
name"mk.name" label"????"gtlt/stextfieldgt
ltssubmit/gt lt/sformgt lt/bodygt actionad
dmk?struts.xml????action????action??(??web.xml??st
ruts2???????action) namemk.name??mk?action????
? private MovieKind mk ?????.name?MovieKind mk
???????,???????,Struts2???????(???????)
42listmk.jsp ltbodygt ltsiterator
value"mklist"gt ltsproperty value"name"/gt
ltbr /gt lt/siteratorgt lt/bodygt valuemklist?
action???List??mklist???????????????valuename
???mklist ????????
43- ?applicationContext.xml???????(aop?)
??tx?????????xml??????????? lt?xml version"1.0"
encoding"UTF-8"?gt ltbeans xmlns"http//www.spring
framework.org/schema/beans" xmlnsxsi"http//www.
w3.org/2001/XMLSchema-instance" xmlnsaop"http//
www.springframework.org/schema/aop" xmlnstx"http
//www.springframework.org/schema/tx" xsischemaLo
cation" http//www.springframework.org/schema/bea
ns http//www.springframework.org/schema/beans/sp
ring-beans-2.0.xsd http//www.springframework.org/
schema/tx http//www.springframework.org/schema/tx
/spring-tx-2.0.xsd http//www.springframework.org/
schema/aop http//www.springframework.org/schema/a
op/spring-aop-2.0.xsd "gt
44?????? lt!-- ??????,??add,delete,updata?????,?????
??required --gt lttxadvice id"txAdvice"
transaction-manager"transactionManager"gt
lttxattributesgt lttxmethod name"add"
propagation"REQUIRED"/gt lttxmethod
name"delete" propagation"REQUIRED"/gt
lttxmethod name"update" propagation"REQUIRED"/gt
lttxmethod name"" read-only"true"/gt
lt/txattributesgt lt/txadvicegt lt!-- ????????????,
??com.service???????????????????,????txadvice???-
-gt ltaopconfiggt ltaoppointcut id"allManagerMetho
d" expression"execution( com.service..(..))"
/gt ltaopadvisor advice-ref"txAdvice"
pointcut-ref"allManagerMethod"/gt lt/aopconfiggt
45( com.service..(..))????????? ??? ??
???????(??????????!) ??? ??
?com.service????class ??? ??
?com.service????class????? ??? .. ??
?????0?????? ???com.service????class?????????????
????????????
??,???Struts2HibernateSpring?????
??????????!