Spring JdbcTemplate和事务控制

1. JdbcTemplate的基本使用:

  @Test
    public void test1() throws PropertyVetoException {
        //创建数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        //加载配置文件
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");
        String driver = rb.getString("driver");
        String url = rb.getString("url");
        String username = rb.getString("username");
        String password = rb.getString("password");

        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);

        //根据数据源创建JdbcTemplate对象
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        //执行数据库操作
        int row = jdbcTemplate.update("insert into users values(?,?,?)", 5,"tom", "1234");
        System.out.println(row);
    }

配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=******

2.Spring JdbcTemplate

applicationContext.xml

<!--    加载配置文件(别忘加context命名空间)-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${user}"></property>
        <!--这里有个坑,不能使用${username},因为在这里spring使用username指示计算机的用户名-->
        <property name="password" value="${password}"></property>
    </bean>
<!--jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testinsert(){
        int row = jdbcTemplate.update("insert into users values (?,?,?)",  null,"zzj", "123");
        System.out.println(row);
    }
    @Test
    public void testUpdate(){
        int row = jdbcTemplate.update("update users set name=? where id=?",  "zzk", "2");
        System.out.println(row);
    }
    @Test
    public void testDelete(){
        int row = jdbcTemplate.update("delete from users where id =?",   "2");
        System.out.println(row);
    }
    @Test
    public void testQuery(){
        List<User> res = jdbcTemplate.query("select * from users", new BeanPropertyRowMapper<User>(User.class));
        System.out.println(res);
    }
    @Test
    public void testQueryforObject(){
        User user = jdbcTemplate.queryForObject("select * from users where id=?", new BeanPropertyRowMapper<>(User.class), "1");
        System.out.println(user);
    }
    @Test
    public void testCount(){
        Long sum = jdbcTemplate.queryForObject("select count(*) from users", Long.class);
        System.out.println(sum);
    }
}

3.Spring 事务控制

3.1编程式事务控制

相关对象
在这里插入图片描述在这里插入图片描述在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

3.2基于XML的声明式事务控制

在这里插入图片描述

    <!--配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--通知,事务的增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
            <tx:method name="transfer" isolation="READ_COMMITTED" propagation="REQUIRED" timeout="-1" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <!--配置事务的aop织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.Impl.*.*(..))"></aop:advisor>
    </aop:config>
3.3基于注解的声明式事务控制

在这里插入图片描述
需要在applicationContext.xml中添加的配置

    <!--配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--事务的注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值