当前位置:首页 > 开发 > 编程语言 > Spring > 正文

spring入门实例-DAO,事务

发表于: 2012-07-27   作者:blackproof   来源:转载   浏览:
摘要: spring入门实例-DAO,事务   实例: 配置文件: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation

spring入门实例-DAO,事务

 

实例:

配置文件:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean name="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">

		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>

		<property name="url">
			<value>jdbc:oracle:thin:@192.168.32.227:1521:orcl</value>
		</property>

		<property name="password">
			<value>lpromis</value>
		</property>

		<property name="username">
			<value>lpromis</value>
		</property>
	</bean>
	
	<bean id="transactionManager"
	class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource"></ref>
		</property>
	</bean>
	
	<bean id="userDAO"
	class="com.myspring.bussiness.CustomerDAO">
		<property name="dataSource">
			<ref bean="dataSource"></ref>
		</property>
		<property name="transactionManager">
			<ref local="transactionManager"></ref>
		</property>
	</bean>

</beans>

 实例中的事务使用的是DataSourceTransactionManager实现的,并且向其中注入了datasource

bean代码:

public class CustomerDAO
{
	/**
	 * datasource
	 */
	private DataSource dataSource;
	public void setDataSource(DataSource dataSource)
	{
		this.dataSource = dataSource;
	}
	public void setTransactionManager(PlatformTransactionManager transactionManager)
	{
		this.transactionManager = transactionManager;
	}
	/**
	 * 事务管理类,初始化已将datasource作为参数
	 */
	private PlatformTransactionManager transactionManager;

	
	/**
	 * 普通使用事务的方法 
	 * 定义事务
	 * 记录事务点
	 * 提交事务点/回滚事务点
	 */
	public void insertCustomer()
	{
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		TransactionStatus status = transactionManager.getTransaction(def);
		try
		{
			JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
			jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname1')");
			jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname2')");
			jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025','testname3')");
			transactionManager.commit(status);
		}
		catch (Exception e)
		{
			transactionManager.rollback(status);
			System.out.println("*********roll back");
		}
	}
	
	/**
	 * 事务模板,无返回值
	 */
	public void insertCustomerTamplate()
	{
		TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
		transactionTemplate.execute(new TransactionCallbackWithoutResult(){

			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0)
			{
				// TODO Auto-generated method stub
				JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
				jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname4')");
				jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000024','testname5')");
				jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025,'testname6')");
			}
		});
	}
	
	/**
	 * 事务模板,带有返回值
	 */
	public void queryCustomerTamplate()
	{
		TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
		Object result = transactionTemplate.execute(
				new TransactionCallback<Object>()
				{
					@Override
					public Object doInTransaction(TransactionStatus arg0)
					{
						JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
						jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000023','testname4')");
						jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000024','testname5')");
						jdbcTemplate.update("insert into t_lpromis_yxgl_khxx(id,khqc) values('000025','testname6')");
						List<Map<String,Object>> rows = jdbcTemplate.queryForList("select eee from t_lpromis_yxgl_khxx");
						return rows;
					}
				});
		if(result instanceof List)
		{
			List re = (List) result;
			for (int i = 0; i < re.size(); i++)
			{
				Map<String,Object> one = (Map<String, Object>) re.get(i);
				System.out.println(one.get("id"));
			}
		}
	}
	
	public static void main(String[] args)
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("com/myspring/bussiness/transactionManager.xml");
		CustomerDAO cd = (CustomerDAO) context.getBean("userDAO");
		cd.queryCustomerTamplate();
	}
}
 以上用 JdbcTemplate实现的jdbc的一系列操作,以及设置事务,其中的sql语句故意错误,验证事务rollback效果

spring入门实例-DAO,事务

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号