spring第三课.docx
《spring第三课.docx》由会员分享,可在线阅读,更多相关《spring第三课.docx(17页珍藏版)》请在冰豆网上搜索。
![spring第三课.docx](https://file1.bdocx.com/fileroot1/2023-8/19/44095be2-7cea-4a50-9402-b755e92c1d44/44095be2-7cea-4a50-9402-b755e92c1d441.gif)
spring第三课
第三课spring与Hibernate的集成
1、准备数据库及表
createdatabasemydb
go
usemydb
go
createtableacount(
aidintidentityprimarykey,
anamevarchar(20),
amoneymoney
)
altertableacountaddconstraintck_amoneycheck(amoney>=10)
--验证
insertintoacountvalues('张飞',1000.00)
insertintoacountvalues('赵云',1000.00)
2、引入spring组件包和Hibernate组件包,并配置数据源和SessionFactory
3、生成POJO类和映射文件以及Spring的DAO类
生成的配置文件
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http:
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
p="http:
//www.springframework.org/schema/p"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd">
class="mons.dbcp.BasicDataSource">
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
value="jdbc:
sqlserver:
//localhost:
1433;databaseName=mydb">
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
org.hibernate.dialect.SQLServerDialect
true
com/po/Acount.hbm.xml
4、编写业务类关联Spring的DAO
packagecom.business;
importcom.dao.*;
importcom.po.Acount;
publicclassAcountBusiness{
privateAcountDAOadao;
publicAcountDAOgetAdao(){
returnadao;
}
publicvoidsetAdao(AcountDAOadao){
this.adao=adao;
}
publicvoidupdate(doubleamoney){
Acounta1=adao.findById
(1);
Acounta2=adao.findById(3);
//张飞的余额增加
a1.setAmoney(a1.getAmoney()+amoney);
//赵云转账给张飞,赵云余额减少
a2.setAmoney(a2.getAmoney()-amoney);
//执行
adao.merge(a1);
adao.merge(a2);
}
}
5、配置业务类
--注入业务类-->
6、给业务类配置事务管理
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http:
//www.springframework.org/schema/beans"
xmlns:
tx="http:
//www.springframework.org/schema/tx"
xmlns:
aop="http:
//www.springframework.org/schema/aop"
xmlns:
context="http:
//www.springframework.org/schema/context"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
p="http:
//www.springframework.org/schema/p"
xsi:
schemaLocation="
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-2.5.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
http:
//www.springframework.org/schema/aop
http:
//www.springframework.org/schema/aop/spring-aop-2.5.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd
">
class="mons.dbcp.BasicDataSource">
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
value="jdbc:
sqlserver:
//localhost:
1433;databaseName=mydb">
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
org.hibernate.dialect.SQLServerDialect
true
com/po/Acount.hbm.xml
--注入业务类-->
--配置Hibernate事务管理-->
--配置事务的传播属性-->
adviceid="txavd"transaction-manager="txmanager">
attributes>
methodname="update*"propagation="REQUIRED"/>
attributes>
advice>
--配置切入点-->
config>
pointcutexpression="execution(*com.business.*.*(..))"id="busspoint"/>
advisoradvice-ref="txavd"pointcut-ref="busspoint"/>
config>
7、测试
packagecom.test;
importcom.business.*;
importorg.springframework.*;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassTestBuss{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
ApplicationContextctx=newClassPathXmlApplicationContext("/applicationContext.xml");
AcountBusinessabuss=(AcountBusiness)ctx.getBean("abuss");
abuss.update(1000.00);
}
}
此代码的运行结果和上面使用jdbc管理事务的结果是一样,都是spring容器的管理。