JdbcTemplate的基本使用

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: JdbcTemplate的基本使用

文章目录

@TOC


一、JdbcTemplate的开发步骤

在这里插入图片描述

1.导入坐标

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

2.创建数据表和实体类

j_account数据表
在这里插入图片描述

package com.study.domain;

public class Account {
    private String name;
    private double money;

    public Account() {
    }

    public Account(String name, double money) {
        this.name = name;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

3.创建JdbcTemplate对象及操作数据库

package com.study.test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;

public class JdbcTemplateTest {

   @Test
    public void test() throws PropertyVetoException {
       //创建数据源对象
       ComboPooledDataSource dataSource = new ComboPooledDataSource();
       dataSource.setDriverClass("com.mysql.jdbc.Driver");
       dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/spring");
       dataSource.setUser("root");
       dataSource.setPassword("root");

       JdbcTemplate jdbcTemplate = new JdbcTemplate();
       //设置数据源对象,知道数据库在哪
       jdbcTemplate.setDataSource(dataSource);
       //执行操作需
       int row = jdbcTemplate.update("insert into j_account values(?,?)", "jack", 4500.0);
       System.out.println(row);


   }
}

二、Spring产生JdbcTemplate对象

将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DataSource注入到JdbcTemplate模板对象中。

1.配置

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
   <!-- jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

2.操作数据库

 @Test
   public void test1() throws PropertyVetoException {
      ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
      JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
      int row = jdbcTemplate.update("insert into j_account values(?,?)", "jery", 4500.0);
      System.out.println(row);


   }

三、Spring产生JdbcTemplate对象(抽取jdbc.properties)

1.jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root

2.加载jdbc.properties

 <!--加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
     <!-- 数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
   <!-- jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

四、基本操作

package com.study.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    public void test(){
        jdbcTemplate.update("update j_account set money=? where name=?",23456.0,"tom");
    }
     @Test
    public void testDelete(){
        jdbcTemplate.update("delete from j_account  where name=?","tom");
    }
     @Test
    public void testQueryAll(){
        List<Account> query = jdbcTemplate.query("select * from j_account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(query);  //[Account{name='jack', money=4500.0}, Account{name='jery', money=4500.0}]
    }
     @Test
    public void testQueryOne(){
        Account jack = jdbcTemplate.queryForObject("select * from j_account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "jack");
        System.out.println(jack);  //Account{name='jack', money=4500.0}
    }
    @Test
    public void testQueryCount(){
        Long account = jdbcTemplate.queryForObject("select count(*) from j_account ", Long.class);
        System.out.println(account);  //2

    }
}
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
XML Java 数据库
【Spring】通过JdbcTemplate实现CRUD操作
【Spring】通过JdbcTemplate实现CRUD操作
47 0
|
8月前
|
SQL Java 数据库连接
Spring中JdbcTemplate和JdbcDaoSupport简单使用
Spring中JdbcTemplate和JdbcDaoSupport简单使用
73 0
Spring中JdbcTemplate和JdbcDaoSupport简单使用
|
9月前
|
XML Java 数据库连接
使用Spring JDBC中的JdbcTemplate对数据进行增删改查操作教程~
使用Spring JDBC中的JdbcTemplate对数据进行增删改查操作教程~
|
11月前
|
XML Java 数据库连接
Hibernate基本使用
Hibernate基本使用
53 0
|
SQL XML Java
Mybatis占位符#和$的区别?源码解读(二)
Mybatis占位符#和$的区别?源码解读(二)
106 0
Mybatis占位符#和$的区别?源码解读(二)
|
XML Java 关系型数据库
SpringBoot 整合 JdbcTemplate|学习笔记
快速学习 SpringBoot 整合 JdbcTemplate
82 0
SpringBoot 整合 JdbcTemplate|学习笔记
|
XML SQL Java
SpringBoot + Mybatis系列之CURD基本使用姿势-注解篇
上面一篇博文介绍了mybatis + xml配置文件的使用方式,在上文中介绍到,xml文件是可以省略掉的,直接使用java注解来实现CURD,接下来我们看一下,如何使用注解来实现等同的效果
281 0
SpringBoot + Mybatis系列之CURD基本使用姿势-注解篇
|
SQL 监控 druid
SpringBoot2.x入门教程:引入jdbc模块与JdbcTemplate简单使用
这篇文章是《SpringBoot2.x入门》专辑的「第7篇」文章,使用的SpringBoot版本为2.3.1.RELEASE,JDK版本为1.8。这篇文章会简单介绍jdbc模块也就是spring-boot-starter-jdbc组件的引入、数据源的配置以及JdbcTemplate的简单使用。为了让文中的例子相对通用,下文选用MySQL8.x、h2database(内存数据库)作为示例数据库,选用主流的Druid和HikariCP作为示例数据源。
334 0
SpringBoot2.x入门教程:引入jdbc模块与JdbcTemplate简单使用
|
Java 关系型数据库 MySQL
springboot学习-使用JdbcTemplate操作MySQL数据库
springboot学习-使用JdbcTemplate操作MySQL数据库
252 0
springboot学习-使用JdbcTemplate操作MySQL数据库