SSM(五)—— JdbcTemplate

1、JdbcTemplate简介

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

步骤:

导入spring-jdbcspring-tx坐标

创建数据库表和实体

创建JdbcTemplate对象

执行数据库操作

2、入门

package com.spring.controller;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.beans.PropertyVetoException;

public class JdbcController {
    public static void main(String[] args) throws PropertyVetoException {
        //创建数据源对象
        ComboPooledDataSource  cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/my");
        cpds.setUser("root");
        cpds.setPassword("mysql");

        JdbcTemplate jt = new JdbcTemplate();
        //设置数据源,知道数据库在哪里
        jt.setDataSource(cpds);
        int n = jt.update("insert into account (`name`,`money`) values(?,?)","dd",1000);
        System.out.println(n);
    }
}

3、Spring产生jdbc

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/my"></property>
        <property name="user" value="root"></property>
        <property name="password" value="mysql"></property>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
package com.spring.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcController2 {
    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jt = ac.getBean(JdbcTemplate.class);
        int n = jt.update("insert into account (`name`,`money`) values(?,?)","dd",1000);
        System.out.println(n);
    }
}

抽取jdbc

4、CRUD操作

package com.spring.test;

import com.spring.domain.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)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void update(){
        jdbcTemplate.update("update account set name = ? where id = ?","ee",11);
    }

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


    //查询全部数据
    @Test
    public void select1(){
        List<Account> list = jdbcTemplate.query("select * from account",
                new BeanPropertyRowMapper<Account>(Account.class));

        System.out.println(list);
    }

    //查询单个数据
    @Test
    public void select2(){
        Account account = jdbcTemplate.queryForObject("select * from account where id = ?",
                new BeanPropertyRowMapper<Account>(Account.class), 3);

        System.out.println(account);
    }

    //查询一个数据
    @Test
    public void select3(){
        Long l = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        System.out.println(l);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值