spring(3)

一。spring整合jdbc

@Test
    public void fun1() throws Exception{

        //0 准备连接池
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql:///hibernate_32");
        dataSource.setUser("root");
        dataSource.setPassword("1234");
        //1 创建JDBC模板对象
        JdbcTemplate jt = new JdbcTemplate();
        jt.setDataSource(dataSource);
        //2 书写sql,并执行
        String sql = "insert into t_user values(null,'rose') ";
        jt.update(sql);

    }

将连接池的配置交给 Spring 管理:

【配置内置连接池】

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>      
  <property name="url" value="jdbc:mysql:///spring_day02"/>      
  <property name="username" value="root"/>      
  <property name="password" value="123"/>     
</bean> 

【将模板配置到 Spring 中】

 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">      <property name="dataSource" ref="dataSource"/>     
</bean> 

【编写测试类】

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") 
public class SpringDemo2 {    
  @Resource(name="jdbcTemplate")  
  private JdbcTemplate jdbcTemplate;    

  @Test  public void demo1(){ 
  jdbcTemplate.update("insert into account values (null,?,?)", " 凤姐 ",10000d);      } 
}

另外有dbcp连接池 c3p0连接池

将数据库连接的信息配置到属性文件中:

文件加载:

两种方式:

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      <property name="location" value="classpath:db.properties"/>     
</bean> 

<!-- 指定spring读取db.properties配置 -->
<context:property-placeholder location="classpath:db.properties"  />

二。spring的aop

  1. 不考虑事务隔离引发的安全问题:

脏读 :一个事务读到了另一个事务的未提交的数据

不可重复读 :一个事务读到了另一个事务已经提交的 update 的数据导致多次查询结果不一致.

虚幻读 :一个事务读到了另一个事务已经提交的 insert 的数据导致多次查询结果不一致

  1. 解决读问题:设置事务隔离级别
    未提交读 :脏读,不可重复读,虚读都有可能发生

    已提交读 :避免脏读。但是不可重复读和虚读有可能发生

    可重复读 :避免脏读和不可重复读.但是虚读有可能发生.

    串行化的 :避免以上所有读问题

    Mysql 默认:可重复读 Oracle 默认:读已提交

    1. spring的声明式事务

      xml形式:

      事务管理器:

      <!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
      <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
      <property name="dataSource" ref="dataSource" ></property>
      </bean>

      配置事务通知:

      <!-- 配置事务通知 -->
      <tx:advice id="txAdvice" transaction-manager="transactionManager" >
      <tx:attributes>
          <!-- 以方法为单位,指定方法应用什么事务属性
              isolation:隔离级别
              propagation:传播行为
              read-only:是否只读
           -->
          <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
          <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
          <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
          <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
          <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
          <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
          <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
          <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
          <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
      </tx:attributes>
      </tx:advice>

      配置织入:

      <!-- 配置织入 -->
      <aop:config  >
      <!-- 配置切点表达式 -->
      <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="txPc"/>
      <!-- 配置切面 : 通知+切点
              advice-ref:通知的名称
              pointcut-ref:切点的名称
       -->
      <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
      </aop:config>

      注解形式:

      <!-- 开启使用注解管理aop事务 -->
      <tx:annotation-driven/>

      使用注解

      @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
      public class AccountServiceImpl implements AccountService {
      ...
      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值