Spring中的JdbcTemplate详细解析

 更新时间:2024年01月12日 09:56:31   作者:咕咕猫_  
这篇文章主要介绍了Spring中的JdbcTemplate详细解析,JdbcTemplate是Spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装,Spring框架为我们提供了很多的操作模板类,需要的朋友可以参考下

一、JdbcTemplate概述

它是Spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。

Spring框架为我们提供了很多的操作模板类。

例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据的RedisTemplate,操作消息队列的JsmTeSmplate等等

二、JdbcTemplate开发步骤

① 导入spring-jdbc和spring-tx坐标

② 创建数据库表和实体

③ 创建JdbcTemplate对象

④执行数据库操作

更新操作:

jdbcTemplate.update (sql,params)

查询操作:

jdbcTemplate.query (sql,Mapper,params)

jdbcTemplate.queryForObject (sql,Mapper,params)

三、JdbcTemplate快速入门

① 导入坐标

        <!--导入spring的jdbc坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>
        <!--导入spring的tx坐标-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.5.RELEASE</version>
        </dependency>

② 创建accout表和Accout实体

CREATE DATABASE Test
 
CREATE TABLE ACCOUNT(
	NAME VARCHAR(255),
	money INT);
	
SELECT * FROM ACCOUNT
public class Account {
    private String name;
    private double 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 +
                '}';
    }
}

③ 创建JdbcTemplate对象

④ 执行数据库操作

public class JdbcTemplateTest {
 
    @Test
    //测试JdbcTemplate快速入门
    public void test1() throws PropertyVetoException {
        //创建数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("ggm");
 
        //创建模板对象
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //设置数据源对象
        jdbcTemplate.setDataSource(dataSource);
        //执行操作
        int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
        System.out.println(row);
    }
 
}

四、Spring产生JdbcTemplate对象

我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将 数据源DataSource注入到JdbcTemplate模版对象中,配置如下:

    <!--配置数据源对象-->
    <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"/>
    </bean>

从容器中获得JdbcTemplate进行添加操作

    @Test
    //测试Spring产生Jdbc模板对象
    public void test2() throws PropertyVetoException {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
        int row = jdbcTemplate.update("insert into account values(?,?)", "zhangsan", 6000);
        System.out.println(row);
    }

五、JdbcTemplate的常用操作

  • 修改操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    //修改操作
    @Test
    public void testUpdate() {
        jdbcTemplate.update("update account set money=? where name=?", 10000, "tom");
    }
}
  • 删除和查询全部操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    //删除操作
    @Test
    public void testDelete() {
        jdbcTemplate.update("delete from account where name=?", "tom");
    }
 
    //查询多个对象
    @Test
    public void testQueryAll() {
        List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }
}
  • 查询单个数据操作操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
    //查询单个对象
    @Test
    public void testQueryOne() {
        Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
        System.out.println(account);
    }
 
    //聚合查询
    @Test
    public void testQueryCount() {
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }
}

到此这篇关于Spring中的JdbcTemplate详细解析的文章就介绍到这了,更多相关Spring之JdbcTemplate内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JAVA实现图书管理系统项目

    JAVA实现图书管理系统项目

    相信每一个学生学编程的时候,应该都会写一个小项目——图书管理系统。为什么这么说呢?我认为一个学校的氛围很大一部分可以从图书馆的氛围看出来,而图书管理系统这个不大不小的项目,接触的多,也比较熟悉,不会有陌生感,能够练手,又有些难度,所以我的小项目也来了
    2021-10-10
  • Springboot-Starter造轮子之自动锁组件lock-starter实现

    Springboot-Starter造轮子之自动锁组件lock-starter实现

    这篇文章主要为大家介绍了Springboot-Starter造轮子之自动锁组件lock-starter实现详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Java判断对象是否为空(包括null ,

    Java判断对象是否为空(包括null ,"")的方法

    这篇文章主要介绍了Java判断对象是否为空(包括null ,"")的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Java内部类详解

    Java内部类详解

    内部类在 Java 里面算是非常常见的一个功能了,在日常开发中我们肯定多多少少都用过,这里总结一下关于 Java 中内部类的相关知识点和一些使用内部类时需要注意的点。
    2020-02-02
  • java long转String +Codeforces110A案例

    java long转String +Codeforces110A案例

    这篇文章主要介绍了java long转String +Codeforces110A案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Java 中分形图的几种方法详解

    Java 中分形图的几种方法详解

    这篇文章主要介绍了Java 中几种分形的方法详解的相关资料,需要的朋友可以参考下
    2017-07-07
  • java文件上传技术深入剖析

    java文件上传技术深入剖析

    这篇文章主要为大家详细介绍了java文件上传技术深入剖析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Java 8函数式接口之BinaryOperator使用示例详解

    Java 8函数式接口之BinaryOperator使用示例详解

    这篇文章主要大家介绍了Java 8函数式接口之BinaryOperator,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Java如何实现http接口参数和返回值加密

    Java如何实现http接口参数和返回值加密

    这篇文章主要介绍了Java如何实现http接口参数和返回值加密问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • MybatisPlus使用Wrapper实现条件查询功能

    MybatisPlus使用Wrapper实现条件查询功能

    这篇文章主要介绍了MybatisPlus使用Wrapper实现查询功能,使用它可以实现很多复杂的查询,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-06-06

最新评论