前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 系列之jdbcTemplate的使用

Spring 系列之jdbcTemplate的使用

作者头像
一只胡说八道的猴子
发布2020-09-28 10:56:00
4040
发布2020-09-28 10:56:00
举报

Spring系列之 jdbcTemplate

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

啥是jdncTemplate?

t他是spring框架中提供的一个对象,是对原始的jdbcAPI对象的简单封装,spring框架为我们提供了很多操作,模板类,比如操作关系型数据库的jdbcTemplate,操作nosql数据库的Redis Template,操作消息队列的jmsTemplate等等

JdbcTemplate开发步骤

1.导入sprign-jdbc和spring-tx坐标 2.创建数据库表和实体 3.创建JdbcTemplate对象 4.执行数据库操作

1.导入sprign-jdbc和spring-tx坐标

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.32</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.9</version>
    </dependency>
</dependencies>

2.创建数据库表和实体

使用sqlyog创建一个表 语句

代码语言:javascript
复制
CREATE TABLE test1(
id INT,
NAME VARCHAR(10)
 );

创建实体

代码语言:javascript
复制
package com.pjh;
public class user {
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "user{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

JbdcTemplate快速入门*,不使用spring框架的时候

代码语言:javascript
复制
@Test
    public void test1() throws PropertyVetoException {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
    comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
    comboPooledDataSource.setUser("root");
    comboPooledDataSource.setPassword("1234");
    //创建jdbcTemplate对象
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(comboPooledDataSource);
    //执行语句
    jdbcTemplate.update("insert into test1 values(?,?)",10,"one");
}

结果

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

抽取配置文件

配置文件代码:

代码语言:javascript
复制
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3309/one
jdbc.name=root
jdbc.password=1234

测试函数操作

代码语言:javascript
复制
@Test
public void test3() throws PropertyVetoException {
    //读取配置文件
    ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
    //获取连接池
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    //设置参数
    comboPooledDataSource.setDriverClass(jdbc.getString("jdbc.driver"));
    comboPooledDataSource.setJdbcUrl(jdbc.getString("jdbc.url"));
    comboPooledDataSource.setUser(jdbc.getString("jdbc.name"));
    comboPooledDataSource.setPassword(jdbc.getString("jdbc.password"));
    //创建jdbcTemplate对象
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(comboPooledDataSource);
    jdbcTemplate.update("insert into test1 values(?,?)",13,"three");

}

使用spring创建JdbcTemplate对象 将数据源DataSource与JdbcTemplate的创建权交给Spring并在Spring容器内进行依赖注入 配置代码:

代码语言:javascript
复制
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3309/one"/>
    <property name="user" value="root"/>
    <property name="password" value="1234"/>
</bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource"/>
    </bean>

测试函数

代码语言:javascript
复制
@Test
    public void test2(){
    ClassPathXmlApplicationContext classPathXmlApplicationContext =
            new ClassPathXmlApplicationContext("applicationContext.xml");
    JdbcTemplate jdbcTemplate =(JdbcTemplate) classPathXmlApplicationContext.getBean("jdbcTemplate");
    jdbcTemplate.update("insert into test1 values(?,?)",11,"two");
}

结果 成功插入

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

这个也可以使用读取配置文件的方式 我们首先要导入context的约束路径与命名空间

命名空间: xmlns:context="http://www.springframework.org/schema/context" 约束路径:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

配置文件修改

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.name}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource"/>
    </bean>
   <context:property-placeholder location="classpath:jdbc.properties"/>
</beans>

测试代码

代码语言:javascript
复制
 @Test
    public void test4(){
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate =(JdbcTemplate) classPathXmlApplicationContext.getBean("jdbcTemplate");
        jdbcTemplate.update("insert into test1 values(?,?)",100,"pjh");
    }

结果 成功插入

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

通过注解的方式来得到JdbcTemplate 使用框架

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private  JdbcTemplate jdbcTemplate;
    @Test
    public void test7(){
        jdbcTemplate.update("insert into test1 values(?,?)",110,"GGB");

    }

不使用框架

代码语言:javascript
复制
public void test1() throws PropertyVetoException {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
    comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
    comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3309/one");
    comboPooledDataSource.setUser("root");
    comboPooledDataSource.setPassword("1234");
    //创建jdbcTemplate对象
    JdbcTemplate jdbcTemplate = new JdbcTemplate();
    jdbcTemplate.setDataSource(comboPooledDataSource);
    //执行语句
    jdbcTemplate.update("insert into test1 values(?,?)",10,"one");
}

由二者对比即可看出框架的巨大好处,上面那么长的代码现在只要几行即可解决

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

JDBCTemplate的常用操作

查询语句

查询数据库中的所有内容

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private  JdbcTemplate jdbcTemplate;
   @Test
public void test8(){
    String sql="select * from test1 where name=?";
    List<user> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<user>(user.class));
    for (user user : query) {
        System.out.println(user);
    }
}

结果

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

查询数据库中的某条内容

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private  JdbcTemplate jdbcTemplate;
   @Test
 @Test
    public void test9(){
        String sql="select * from test1 where id=?";
        List<user> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<user>(user.class), 10);
        for (user user : query) {
            System.out.println(user);
        }
    }
}

查询数据库记录的数量

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private  JdbcTemplate jdbcTemplate;
@Test
public void test90(){
    String sql="select count(*) from test1";
    Long aLong = jdbcTemplate.queryForObject(sql, Long.class);
    System.out.println("记录条数:"+aLong);
}
}

删除指定记录

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Autowired
private  JdbcTemplate jdbcTemplate;
@Test
public void test11(){
    String sql="delete from test1 where id=11";
    jdbcTemplate.update(sql);
}
}

以上就是Spring jdbc操作的一些知识,我会不断的学习,也会不断更新我的学习文章,主要有java和数据结构两个方面,有想要一起学习的伙伴可以私信或则关注我,共勉

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring系列之 jdbcTemplate
  • 啥是jdncTemplate?
  • JdbcTemplate开发步骤
  • JDBCTemplate的常用操作
  • 以上就是Spring jdbc操作的一些知识,我会不断的学习,也会不断更新我的学习文章,主要有java和数据结构两个方面,有想要一起学习的伙伴可以私信或则关注我,共勉
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档