搭建和配置Spring与jdbc整合的环境
2015-07-03 17:39
741 查看
1、使用Spring+JDBC集成步骤如下:
1)配置数据源,如:
3)注解方式进行配置事物
@Service @Transactional
1、)xmlns:tx="http://www.springframework.org/schema/tx"
2、)http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
3、)http://www.springframework.org/schema/tx
采用@Transactional注解方式使用事物
2、Spring集成的jdbc编码和事务管理
1)、如果我们没有用@Transactional进行注解的形式,那么方法中的语句会在各自的事物进行执行,无法保证在同一个事物执行,可能会出现一些问题
2)、加上这个注解,告诉注解器,这个类里面的方法都是应该在同一事物进行管理
1)配置数据源,如:
<bean id="dataSource" class="orgrivae.apache.commons.dbcp.datasources" destroy-method="close"> <property name="driverClass" value="org.gjt.mm.mysql.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&characterEncoding=UTF-8"/> <property name="user" value="root"/> <property name="password" value="root"/> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <property name="initialPoolSize" value="1"/> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="1"/> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="300"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="60"/> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="5"/> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <property name="idleConnectionTestPeriod" value="60"/> </bean>2)配置事物:配置事物时,需要在xml配置文件中引入用于声明事物的tx命名空间,事物的配置方式有两种:注解方式和基于xml配置方式
3)注解方式进行配置事物
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--使用基于注解方式配置事务 --> <tx:annotation-driven transaction-manager="txManager"/>bean中注解声明
@Service @Transactional
Publci class PersonServiceBean implemnets PersonService{ ..... }在进行注解方式配置事物的时候,首先要在xml中规定好命名空间,主要是下面三个:
1、)xmlns:tx="http://www.springframework.org/schema/tx"
2、)http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
3、)http://www.springframework.org/schema/tx
采用@Transactional注解方式使用事物
2、Spring集成的jdbc编码和事务管理
1)、如果我们没有用@Transactional进行注解的形式,那么方法中的语句会在各自的事物进行执行,无法保证在同一个事物执行,可能会出现一些问题
2)、加上这个注解,告诉注解器,这个类里面的方法都是应该在同一事物进行管理
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- JDBC 数据库常用连接 链接字符串
- JDBC连接Access数据库的几种方式介绍
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- JDBC 程序的常见错误及调试方法
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- Java中使用JDBC操作数据库简单实例
- Java加载JDBC驱动程序实例详解
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- JSP中使用JDBC访问SQL Server 2008数据库示例
- 解析jdbc处理oracle的clob字段的详解
- JDBC数据库的使用操作总结