Spring框架学习04

写在前面

今天主要学了Jdbc Template和在spring中对事务的管理。

Jdbc Template

什么是JdbcTemplate

JdbcTemplate是spring对最基础的JDBC的一个封装框架,类似于DBUtils,在使用上也十分地类似。

JdbcTemplate的使用

下面是一段最基本的JdbcTemplate的使用:

    public static void main(String[] args) {
        //准备数据源:spring的内置数据源
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/eesy");
        ds.setUsername("root");
        ds.setPassword("abc456");
        //1.创建JdbcTemplate对象
        JdbcTemplate jt = new JdbcTemplate();
        //给jt设置数据源
        jt.setDataSource(ds);
        //2.执行操作
        jt.execute("insert into account(name,money) values('ccc',1000)");
    }

可以看到,基本使用方法与DBUtils基本完全一致。更新和删除方法也比较相似,不再看了。这里重点说一下查询:

        List<Account> accounts = jt.query("select * from account where money >?",new BeanPropertyRowMapper<Account>(Account.class),1000f);
        for(Account account:accounts){
          System.out.println(account);
}

Jt的query方法与QueryRunner不同,返回类型是由不同的重载方法来实现的,而不是通过传入的值类型来控制的。但本质上并没有什么不同。
在实际开发中,我们肯定不能这么简单的搞。我们需要把接触数据库底层的方法放到Dao包下然后让Spring帮我们注入JdbcTemplate对象。以下是配置:

    <!--配置JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
        <property name="username" value="root"></property>
        <property name="password" value="abc456"></property>
    </bean>

JdbcTemplate的学习很简单,下面我们来看看spring中的事务管理:

spring中的事务管理

首先要导入对应的包:

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

然后进行相关配置:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 配置业务层 -->
    <bean id="accountService" class="com.liuge.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <!-- 配置账户的持久层-->
    <bean id="accountDao" class="com.liuge.dao.impl.AccountDaoImpl">
<!--        <property name="jdbcTemplate" ref="jdbcTemplate"></property>-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/eesy"></property>
        <property name="username" value="root"></property>
        <property name="password" value="abc456"></property>
    </bean>
    <!--spring中基于XML的声明式事务控制配置步骤
        1.配置事务管理器
        2.配置事务的通知
                此时我们需要导入事务的约束 tx的名称空间和约束,同时也需要aop
                使用tx:advice标签配置事务通知
                    属性:
                        id:给事务通知起一个唯一标志
                        transaction-manager:给事务通知提供一个事务管理器引用
        3.配置AOP中的通用切入点表达式
        4.建议事务通知和切入点表达式的对应关系
        5.配置事务的属性:
                是在事务的通知tx:advice标签的内部

     -->
    <!-- 配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 配置事务的通知
            isolation="":用于指定事务的隔离界别,默认值是DEFAULT,表示使用数据库的默认隔离机制
            no-rollback-for="":用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常类时事务回滚,没有默认值,表示任何异常值都回滚
            propagation="" 用于指定事务的传播行为,默认值是REQUIRED,表示一定会有事务,增删改的选择,查询可以选择SUPPORTS
            read-only=""  :用于指定事务是否只读,只有查询方法才能设置为true,默认值是false
            rollback-for="":用于指定一个异常,当产生该异常时,事务回滚,没有默认值,表示任何异常都回滚
            timeout="":用于指定事务的超时时间,默认值为-1,表示永不超时,如果指定了数值,以秒为单位
        -->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>
    <!-- 配置aop-->
    <aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.liuge.service.impl.*.*(..))"/>
        <!-- 建立切入点表达式和事务通知的的对应关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>
</beans>

spring中基于注解的事务控制

既然有XML配置,同样的也有注解的配置。如下:

    <!--spring中基于注解的声明式事务控制配置步骤
        1.配置事务管理器
        2.开启spring对注解事务的支持
        3.在需要事务支持的地方使用@Transaction注解


     -->
    <!-- 配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 开启spring对注解事务的支持-->
    <tx:annotation-driven></tx:annotation-driven>

在类中添加对应的注解配置:

@Service("accountService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)

之后就可以使用事务了。

扫描二维码关注公众号,回复: 11170357 查看本文章

总结

到现在为止Spring框架的学习已经完毕了。这几天基本学习了Spring框架的几个常用的东西。下一步就是SpringMVC和SSM整合了,希望假期的时间够用吧。

猜你喜欢

转载自www.cnblogs.com/wushenjiang/p/12819378.html
今日推荐