• Spring与DAO-day04


    该章节包含两部分内容:SpringJDBC模板,Spring的事务管理。而这两部分内容,就是对IoCAOP的典型应用。

    l Jdbc模板是对IoC的应用。

    事务管理是对AOP的应用。

    1.1 JDBC模板

    1.1.1 4.1.1数据库的基本操作

    (1) 基础环境配置

    Jarmysql驱动 四要素(驱动,URL name password

    A、 Service

     

    B、 Dao

     

    查询name

     

    查询student

     

    前提

     

     

    C、 Application.xml中配置数据源的4种方式
    a、 first

     

    b、 Second

     

    c、 three

     

    d、 four

     

    (2) 优化升级

    A、 1.模板可以省略

    1.1使用DBCP数据源

    Apache commons dbcp jar  basicDataSource

    Commons pool jar

     

     

    1.2 使用C3p0 mchange

     

     

    B、 2.注册属性文件

     

    2.2添加context约束

    property-placeholder

     

    (3) 异常

    Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.driver]

    警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentService' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dao' of bean class [com.abc.service.StudentServiceImpl]: Bean property 'dao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

    Service、中的dao忘记写set方法

    查找方法的参数使用数组,修改中的参数使用可变参数,可变参数只能在最后

    Jdbc模板多例,一个方法,为每一个进程创建一个对象,当线程结束时,会销毁这个线程,一个方法代表一个进程

    1.2 事务管理

    Spring事务管理,主要是要将事务由Dao层提升至Service层。

      

    1.2.1 Spring事务管理的API

    (1) 平台事务管理器接口

     

    该接口的功能主要是完成事务的提交与回滚。

    A、 常用的两个实现类

    l DataSourceTransactionManager

    当使用JDBCMyBatisiBatis)连接DB,则使用该实现类进行事务管理。

    l HibernateTransactionManager

    当使用Hibernate连接DB,则使用该实现类进行事务管理。

    B、 Spring默认的回滚方式

    发生运行时异常回滚,发生受查异常提交。该默认回滚方式,程序员通过配置是可以改变的。

    (2) 事务定义接口

    A、 五个事务隔离级别

     

    Mysql默认的事务隔离级别是:可重复读,即4级。

    Oracle默认的事务隔离级别是:读已提交,即2级。

    B、 七个事务传播  行为

     

    1.2.2 事物管理举例

    (1) 购买股票。

    A、 Beans

     

    B、 Dao

     

     

     

    C、 Service

     

      

    D、 Test

     

    E、 Xml

     

    F、 Jdbc.Properties

     

    (2) 使用Spring的事务代理工厂Bean管理事务

    Xml中的IOC

     

    AOP

     

    (3) 使用Spring的事务注解管理事务

     

    注解,在方法前添加

    @Transactional(

    isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,

    rollbackFor=BuyStockException.class)

     

    (4) 使用AspectJ管理事务(重点)

     

  • 相关阅读:
    Ext.form.RadioGroup
    Ext:ComboBox实战
    Ext.Ajax.request
    下拉菜单
    为Ext添加下拉框和日期组件
    点击grid单元格弹出新窗口
    好用的sql
    数据库连接池总是断开
    POI 自用API
    String 操作
  • 原文地址:https://www.cnblogs.com/csslcww/p/9551859.html
Copyright © 2020-2023  润新知