SpringJDBCTemplate和事务控制

Spring_JDBCTemplate和事务控制

JDBCTemplate

在这里插入图片描述

​ 它是Spring提供的一个对象,是对原始的Jdbc Api对象的简单封装。Spring提供了很多操作模版类。

  • 操作关系型数据库:JdbcTemplate、HibernateTemplate
  • 操作nosql数据库:RedisTemplate
  • 操作消息队列:JmsTemplate

JdbcTemplate作用

​ 用于和数据库交互,实现对表的CRUD操作。

JdbcTemplate的获取

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans.xsd" >
  <!--  配置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/spring?useSSL=false"></property>
    <property name="username" value="root"></property>
    <property name="password" value="welcome1"></property>
  </bean>
</beans>
//获取容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//获取JdbcTemplate对象
JdbcTemplate jt = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);

JdbcTemplate的CRUD

public static void main(String[] args) {
   
  //获取容器
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
  //获取JdbcTemplate对象
  JdbcTemplate jt = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
  //执行操作
  //保存
  jt.update("insert into account(name,money) values (?,?)", "fff", 1000f);
  //更新
  jt.update("update account set name=?,money=? where id=?","update_test",4567f,7);
  //删除
  jt.update("delete from account where id=?",6);
  //查询所有
  //new BeanPropertyRowMapper<Account>(Account.class)想当于DBUtils中当BeanHandler。
  List<Account> accounts = jt.query("select * from account where id>?", new BeanPropertyRowMapper<Account>(Account.class), 3);
  for (Account account : accounts) {
   
    System.out.println(account);
  }
  //查询一个
  List<Account> accounts1 = jt.query("select * from account where id=?", new BeanPropertyRowMapper<Account>(Account.class), 3);
  System.out.println(accounts1.isEmpty() ? "没有内容" : accounts1.get(0));
  //聚合函数
  Long count = jt.queryForObject("select count(*) from account where id > ?",Long.class,2);
  System.out.println(count);
}

JdbcTemplate在DAO中的操作

/*** 持久层接口*/
public interface AccountDao {
   
  Account findAccountById(Integer id);//根据id查询账户
  Account findAccountByName(String name);//根据名称查询账户
  void updateAccount(Account account);//更新账户
}
/********************************************************************************************************************************************************************************************/
public class 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值