JdbcTemplate基本使用

本文详细介绍了如何在Spring框架中使用JdbcTemplate进行数据库操作,包括查询、更新、删除等,并展示了创建JdbcTemplate对象、设置数据源以及实际操作的例子。
摘要由CSDN通过智能技术生成
1.1 JdbcTemplate概述

它是Spring框架中提供的一个对象,是对原始繁琐的Jdbc APl对象的简单封装。spring框架为我们提供了很多的操作模版类

1.2 JdbcTemplate开发步骤
  1. 导入spring-jdbc和spring-tx坐标
  2. 创建数据库表和实体
  3. 创建JdbcTemplate对象
  4. 执行数据库操作
常用操作更新和查询 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class JdbcTemplatateCRUDTest {
    @Autowired
    public JdbcTemplate jdbcTemplate;
    //查询多少个
    @Test
    public void testQueryCount(){
        Long query = jdbcTemplate.queryForObject("select count(*) from account",long.class);
        System.out.println(query);
    }
    //查询单个
    @Test
    public void testQueryAone(){
        Account query = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "zhangsan");
        System.out.println(query);
    }
    //查询全部
    @Test
    public void testQueryAll(){
        List<Account> query = jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(query);
    }
    //更新
    @Test
    public void testUpdate(){
        int row = jdbcTemplate.update("update account set money=? where name=?", 10000, "zhangsan");
        System.out.println(row);
    }
    //删除
    @Test
    public void testDelete(){
        int row = jdbcTemplate.update("delete from account where name=?","tom");
        System.out.println(row);
    }
 JdbcTemplate使用
    <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>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
 
    @Test
    public void test2() throws Exception{
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
        int row = jdbcTemplate.update("insert into account3 values (?,?);", "李四", 5000);
        System.out.println(row);
    }
    @Test
    public void test1() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");;
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
 
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
 
        int a=jdbcTemplate.update("insert into account values (?,?);","张三",5000);
        System.out.println(a);
    }
知识要点:

导入Spring-jdbc和Spring-tx坐标

创建数据库表和实体

创建jdbcTemplate对象:
       JdbcTemplate jdbcTemplate=new JdbcTemplate();
       jdbcTemplate.setDataSource(dataSource);

执行数据库操作:

更新操作:
      jdbcTemolate.update(sql,parms)  
查询操作:
     jdbcTemplate.query(sql,Mapper,params)
     jdbcTemplate.queryForObject(sql,Mapper,params)                 

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值