Spring jdbc Template和Spring 事务管理

使用jdbcTemplate完成增删改查操作(重点)

package com.it.jdbctemplate;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.it.domain.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class JdbcTemplateTest3 {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //修改
    @Test
    public void test1(){
        jdbcTemplate.execute("update t_user set sex='男' where id=2");
    }
    //添加操作
    @Test
    public void test2(){
        jdbcTemplate.execute("insert into t_user values(null,'赵六',20,'女')");
    }
    
    //删除操作
    @Test
    public void test3(){
        jdbcTemplate.execute("delete from  t_user where id =5");
    }
    //测试返回简单数据类型
    @Test
    public void test4(){
        String name = jdbcTemplate.queryForObject("select name from t_user where id=?", String.class,1);
        System.out.println(name);
    }
    //测试返回简单数据类型
    @Test
    public void test5(){
        Integer count = jdbcTemplate.queryForObject("select count(*) from t_user",Integer.class);
        System.out.println(count);
    }
    //使用BeanPropertyRowMapper
    @Test
    public void test6(){
//        User user = jdbcTemplate.queryForObject("select * from t_user where id=?",
//                new BeanPropertyRowMapper<User>(User.class),2);
        
        List<User> user = jdbcTemplate.query("select * from t_user", new BeanPropertyRowMapper<User>(User.class));
        System.out.println(user);
        
    }
    
    
}

 


掌握数据库连接池的使用和配置(重点)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        
        <!--引入外部的properties文件  -->
        <context:property-placeholder location="classpath:db.properties"/>
	<!-- <bean id="driveManageDatoSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql:///springtest"></property>
	<property name="username" value="root"></property>
	<property name="password" value="123"></property>
	</bean> -->
	<!-- 设置c3p0连接池 -->
	<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="${jdbc.driverClass}"/>
	<property name="jdbcUrl" value="${jdbc.url}"/>
	<property name="user" value="${jdbc.username}"/>
	<property name="password" value="${jdbc.password}"/>
	
	</bean>
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="c3p0DataSource"></property>
	</bean>
	
</beans>    

  


jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///springtest
jdbc.username=root
jdbc.password=123

  


3.掌握事务的传播行为(重点)

Spring事务管理机制

3.1PlatformTransactionManager(平台事务管理器)

3.2TransactionDefinition(事务的定义信息)

传播 :它解决的是两个被事务管理的方法互相调用问题。它与数据库没有关系,是程序内部维护的问题

propagation required(传播请求) :  默认值 两个操作处于同一个事务,如果之前没有事务,新建一个事务

propagation requires new (传播新请求) : 两个操作处于不同的事务

propagtion nested  : 它是一种嵌套事务,它是使用SavePoint来实现的。事务回滚时可以回滚到指定的 savepoint

注意:它只对DataSourceTransactionManager有作用

3.2TransactionStatus(事务状态信息)

  

4.了解基于xml配置声明式事务管理(了解)

    <!--创建事务管理器  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="c3p0DataSource"></property>
    </bean>
    <!--配置通知  -->
    <tx:advice id="txAdvice"  transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method  name="account" />
    </tx:attributes>
    </tx:advice>
    
    
    
    <!--配置切面  -->
    <aop:config>
    <aop:pointcut expression="execution(* com.it.service.IAccountService.account(..))" id="txPointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

 


5.掌握基于annotation声明式事务管理(重点)

@Transactional()
    public void account(String outname, String inname, double money) {
        //转出操作
        accountDao.accountOut(outname, money);
        System.out.println(10/0);//一定会出现异常
        //转入操作
        accountDao.accountIn(inname, money);
        
    }



<tx:annotation-driven transaction-manager="transactionManager"/>

 



    

posted @ 2017-07-03 23:25  海洋aaa  阅读(473)  评论(0编辑  收藏  举报