JdbcTemplate的使用

15 篇文章 0 订阅
2 篇文章 0 订阅

1. 概述

它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装。spring 框架为我们提供了很多 的操作模板类。

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

2. JdbcTemplate 对象的创建 

 public JdbcTemplate() {  } 
 
 public JdbcTemplate(DataSource dataSource) {   setDataSource(dataSource); 
  afterPropertiesSet();  } 
 
 public JdbcTemplate(DataSource dataSource, boolean lazyInit) {   setDataSource(dataSource); 
  setLazyInit(lazyInit);   afterPropertiesSet();  } 
除了默认构造函数之外,都需要提供一个数据源。既然有set方法,所以 我们可以 在配置文件中配置这些对象

3. 编写 spring 的配置文件 

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="template">
        <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:///spring"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123123"></property>
    </bean>


</beans>

4. JdbcTemplate 的增删改查操作 

  1.添加数据

public class JdbcTemplateDemo2 {  
public static void main(String[] args) {   
//1.获取 Spring 容器  
 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 
  //2.根据 id 获取 bean 对象  
 JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
  //3.执行操作   
jt.execute("insert into account(name,money)values('eee',500)");
  }
 } 

2. 查询数据

public class JdbcTemplateDemo3 {  
public static void main(String[] args) { 
 
  //1.获取 Spring 容器  
 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 
  //2.根据 id 获取 bean 对象  
 JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
  //3.执行操作   //保存 
  jt.update("insert into account(name,money)values(?,?)","fff",5000); 
 }
 }

3. 更新数据

public class JdbcTemplateDemo3 { 
 public static void main(String[] args) { 
 
  //1.获取 Spring 容器  
 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 
  //2.根据 id 获取 bean 对象 
  JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
  //3.执行操作   //修改  
 jt.update("update account set money = money-? where id = ?",300,6); 
    }
 } 

4.删除操作 

public class JdbcTemplateDemo3 {  
public static void main(String[] args) { 
 
  //1.获取 Spring 容器  
 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 
  //2.根据 id 获取 bean 对象  
 JdbcTemplate jt = (JdbcTemplate) ac.getBean("jdbcTemplate"); 
  //3.执行操作 
  //删除 
  jt.update("delete from account where id = ?",6); 
 }
 } 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值