当前位置: 首页 > news >正文

JdbcTemplate基本使用

JdbcTemplate概述

它是spring框架中提供的一个对象,是对原始繁琐的JdbcAPI对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和MbernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。

JdbcTemplate开发步骤

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

② 创建数据库表和实体

③ 创建JdbcTemplate对象

④ 执行数据库操作

@Test
    // 测试JdbcTemplate的开发步骤
    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(?, ?)", "lisi11", 1000);
        System.out.println(row);
    }

//-----------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">


<!--    加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

<!--    数据源对象-->
    <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.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

<!--    jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

//----------------------------------------------

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

 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 = ?",10001, "wangwu");
    }

    @Test
    public void testDelete(){
        jdbcTemplate.update("delete from account where money = ?", 10001);
    }
}

 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 = ?",10001, "wangwu");
    }

    @Test
    public void testDelete(){
        jdbcTemplate.update("delete from account where money = ?", 10001);
    }

    //查询多个
    @Test
    public void estQuery(){
        List<Account> accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }

    //查询一个
    @Test
    public void testQueryOne(){
        List<Account> accountList = jdbcTemplate.query("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "lisi");
        System.out.println(accountList);

    }

    //聚合查询
    @Test
    public void testQueryCount(){
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }
}

相关文章:

  • [Bond的杂货铺] CKS 证书也到货咯
  • Python标准库json完全指南:高效处理JSON数据
  • 动态规划——两个数组的dp问题
  • [C++面试] 初始化相关面试点深究
  • macos下 ragflow二次开发环境搭建
  • PIXOR:基于LiDAR的3D检测模型解析
  • Skyline配置指南-微信小程序
  • 【Unity网络编程知识】C#的 Http相关类学习
  • 阿里云原生AI网关Higress:架构解析与应用实践
  • OpenCV——图像融合
  • 2025 年陕西消防设施操作员考试攻略:历史文化名城的消防传承与创新​
  • 基于FreeRTOS和LVGL的多功能低功耗智能手表
  • 2024 蓝桥杯 Java 研究生组
  • 基于STM32与应变片的协作机械臂力反馈控制系统设计与实现---5.2 工业机械臂系统性能测试全方案(专业工程级)
  • 增长黑客:激活实验助力增长
  • [Scade One] Swan与Scade 6的区别 - signal 特性的移除
  • 边缘计算网关:开启物联网新时代的钥匙
  • Llama 4全面评测:官方数据亮眼,社区测试显不足之处
  • 蓝桥杯c++每日刷题(洛谷)
  • mpu6050读who_am_i寄存器一直读0x7c
  • 中山网站制作专业/百度人工服务热线电话
  • node.js做网站开发/软文的目的是什么
  • html5网站模板移动端/优化网络的软件下载
  • 如何做网站代理/发外链的平台有哪些
  • 做外贸网站平台/公司网站优化方案
  • wordpress基础主题站/网站宣传和推广的方法有哪些