栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring -- Spring JdbcTemplate基本使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring -- Spring JdbcTemplate基本使用

1. JdbcTemplate概述


2. JdbcTemplate开发步骤


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

pom.xml



    
        spring_jdbc_tl
        com.tian
        1.0-SNAPSHOT
    
    4.0.0

    spring_jdbc_tl

    
        
        
            c3p0
            c3p0
            0.9.1.2
        
        
        
            mysql
            mysql-connector-java
            8.0.25
        
        
        
            junit
            junit
            4.12
            test
        
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
        
            org.springframework
            spring-jdbc
            5.0.5.RELEASE
        
        
            org.springframework
            spring-tx
            5.0.5.RELEASE
        
    

    
        8
        8
    



2.2 创建数据库表和实体

执行下面的SQL语句:

CREATE DATAbase test;
use test;

CREATE TABLE IF NOT EXISTS `account`(
`name` VARCHAR(10) NOT NULL  COMMENT "姓名",
`money` DOUBLE(10,2) NOT NULL COMMENT "余额"
 )ENGINE =INNODB DEFAULT CHARSET=utf8;

创建实体类:


Account.java

package com.tian.pojo;

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 +
                '}';
    }
}

2.3 创建JdbcTemplate对象 执行数据库操作(插入操作)

JdbcTemplateTest.java

package com.tian.test;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;

public class JdbcTemplateTest {
    @Test
    //测试JdbcTemplate开发步骤
    public void test1() throws PropertyVetoException {
        //创建数据源对象 C3p0连接池
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("317525");

        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //设置数据源对象  知道数据库在哪
        jdbcTemplate.setDataSource(dataSource);
        //执行操作 后面的2个参数是自动匹配sql字段(前面的?是占位符)
        int row = jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
        System.out.println(row);
    }
}

运行结果:


3. Spring产生JdbcTemplate对象(测试插入操作)


applicationContext.xml




    
    

    
    
        
        
        
        
    

    
    
        
    

jdbc.properties

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

3.1 测试代码

还是刚刚那个测试类中,我们多家一个方法加以测试:

    @Test
    //测试Spring产生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(?,?)", "Tom", 3210);
        System.out.println(row);
    }

测试结果:


4. 更新操作 查询操作

详细使用请看代码

JdbcTemplateCRUDTest.java

package com.tian.test;

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

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class) // 让SpringJUnit4ClassRunner这个类去帮我们进行测试
@ContextConfiguration("classpath:applicationContext.xml")//加载spring核心配置文件
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    
    @Test
    public void testQueryCount() {
        // 因为返回的是基本类型参数 所以不需要BeanPropertyRowMapper帮我们封装数据
        Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(count);
    }

    
    @Test
    public void testQueryOne() {
        // 第一个参数是sql 第二个参数可以帮我门封装返回的数据(Account是返回的数据类型)
        // 第三个参数(是一个可变长参数) 是前面占位符(?)对应的参数信息
        Account account = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper(Account.class), "tom");
        System.out.println(account);
    }

    
    @Test
    public void testQueryAll() {
        // 第一个参数是sql 第二个参数可以帮我门封装返回的数据(Account是返回的数据类型)
        List accountList = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper(Account.class));
        System.out.println(accountList);
    }

    //    更新操作
    @Test
    public void testUpdate() {
        jdbcTemplate.update("update account set money=? where name=?", 10000, "tom");
    }

    // 删除操作
    @Test
    public void testDelete() {
        jdbcTemplate.update("delete from account where name=?", "tom");
    }
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351539.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号