zoukankan      html  css  js  c++  java
  • 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"/>



        

  • 相关阅读:
    凸包学习笔记
    2019ICPC南昌网络赛总结
    结对编程作业
    实验 6 :OpenDaylight 实验 ——OpenDaylight 及 Postman 实现流表下发
    实验 5:OpenFlow 协议分析和 OpenDaylight 安装
    实验 3:Mininet 实验——测量路径的损耗率
    实验 4 : Open vSwitch 实验——Mininet 中使用 OVS 命令
    第一次个人编程作业
    实验 2 :Mininet 实验 —— 拓扑的命令脚本生成
    实验 1 :Mininet 源码安装和可视化拓扑
  • 原文地址:https://www.cnblogs.com/weihaiyang/p/7113546.html
Copyright © 2011-2022 走看看