spring第三课.docx

上传人:b****5 文档编号:30711958 上传时间:2023-08-19 格式:DOCX 页数:17 大小:1.31MB
下载 相关 举报
spring第三课.docx_第1页
第1页 / 共17页
spring第三课.docx_第2页
第2页 / 共17页
spring第三课.docx_第3页
第3页 / 共17页
spring第三课.docx_第4页
第4页 / 共17页
spring第三课.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

spring第三课.docx

《spring第三课.docx》由会员分享,可在线阅读,更多相关《spring第三课.docx(17页珍藏版)》请在冰豆网上搜索。

spring第三课.docx

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容器的管理。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1