通过SpringJDBC的JdbcTemplate实现数据库的CRUD(增删改查)

1.概述

JDBC提供了一套模板JdbcTemplate,用于Spring与JDBC的整合,使用模版类能够极大的简化原有JDBC的编程过程,可以实现一条语句实现CRUD。

使用时需要在spring的配置文件中配置

    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>

2.代码实现

2.1applicationContext,..xml中的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///test"/>
        <property name="user" value="root"/>
        <property name="password" value="abcd1234"/>
    </bean>
    <!--配置JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

2.2实体类User,用来封装查询出的数据

package cn.wyy.domain;

public class User {
    private int id;
    private String name;
    private int age;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.3创建测试类用来测试使用模板后对数据的增删改查

下面代码中用到了Junit中的前置方法@Before,后后置方法@After,因为在每次执行测试开始时都需要,对数据库进行容器初始化,获取bean,在测试结束后都需要,关闭容器释放资源。所以添加了前置方法后后置方法就简化了代码,更加便于操作。

package cn.wyy.test;

import cn.wyy.domain.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * SpringJDBC - JdbcTemplate一行代码搞定增删改查
 */
public class Test01 {
    private ApplicationContext context = null;
    private JdbcTemplate jdbcTemplate = null;

    @Before
    public void before(){
        //1.初始化Spring容器
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取bean
        jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
    }

    @After
    public void after(){
        //3.关闭容器
        ((ClassPathXmlApplicationContext)context).close();
    }

    /**
     * 查询
     */
    @Test
    public void test04(){
        //Map<String, Object> map = jdbcTemplate.queryForMap("select * from user where id = ?", 2);
        //System.out.println(map);

        //List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from user where id < ?", 3);
        //System.out.println(list);

        User user = jdbcTemplate.queryForObject("select * from user where id = ?", new BeanPropertyRowMapper<User>(User.class), 2);
        System.out.println(user);
    }

    /**
     * 删除
     */
    @Test
    public void test03(){
        jdbcTemplate.update("delete from user where id = ?",9);
    }

    /**
     * 修改
     */
    @Test
    public void test02(){
        jdbcTemplate.update("update user set age = ? where id = ?",88,9);
    }

    /**
     * 新增
     */
    @Test
    public void test01(){
        jdbcTemplate.update("insert into user values (?,?,?)",9,"zz",99);
    }
}

数据库中的数据如下

执行查询操作,结果如下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值