【Spring Data Access】SimpleJdbcInsert 使用方法

SimpleJdbcInsert使用介绍

SimpleJdbcInsert主要被用来简化插入操作,

下面我们通过一个简单的示例来熟悉一下使用:


import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangjian
 */
public class SimpleJdbcInsertSample {
    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
        JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class);
        //初始化数据库
        jdbcTemplate.execute("drop table if exists user  ");
        jdbcTemplate.execute("create table user(id int auto_increment primary key, name varchar(40), age int)");

        DataSource dataSource = ac.getBean(DataSource.class);
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(dataSource)
                //必须操作: 这里我们配置当前作用的table
                .withTableName("user")
                //可选操作: 这里我们可以显示的配置,我们insert语句中的column的名称,未被列出的column则不会插入值,
                //         取各自的默认值
                .usingColumns("name", "age")
                //可选操作: 这里我们配置table 自动生成的column名称,如果没有,则不需要配置,
                //         此处配置是我们想了解自动生成key的信息
                .usingGeneratedKeyColumns("id");

        //方式一
        //准备参数
        Map<String, Object> insertUser = new HashMap<>(2);
        insertUser.put("name", "bob");
        insertUser.put("age", 26);
        //调用插入,如果不需要了解自动生成字段的值,则直接调用execute方法,
        Number bobId = simpleJdbcInsert.executeAndReturnKey(insertUser);
        System.out.println("bob id: " + bobId.longValue());

        //方法二
        // 不需要调用executeAndReturnKey方法,而且传入的值可以是Map,也可以是SqlParameterSource
        Number aliceId = simpleJdbcInsert.executeAndReturnKey(new BeanPropertySqlParameterSource(new User("alice", 18)));
        System.out.println("alice id: " + aliceId.longValue());

        //输出所有用户
        List<User> findUsers = jdbcTemplate.query("select * from user",
                (rs, rowNum) -> new User(rs.getLong(1), rs.getString(2)));
        findUsers.forEach(System.out::println);

        //清理环境
        jdbcTemplate.execute("drop table user");
    }
}

上面的执行结果如下:
在这里插入图片描述
首先我们要了解是SimpleJdbcInsert的创建,我们需要配置DataSource, 同时我们也得显示指定操作对应的table信息,所以如下是最基本的配置:

SimpleJdbcInsert(dataSource)
        .withTableName("user");

注意到上面的代码片段和示例中的区别是示例中指定了具体的 column名称(通过usingColumns方法),对于上面并没有显示的给出操作的column信息,SimpleJdbInsert则是通过driver提供的metainfo去查询对应table的column信息。

最后,我们看到指定自动生成字段的名称,通过usingGeneratedKeyColumns完成,如果你的table没有对应的字段,或者你不需要了解这些字段的信息,则可以选择忽略这个方法的调用。

附: 上面示例其他关联类的定义

Config.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @author jiangjian
 */
@Configuration
@ComponentScan
@PropertySource("classpath:jdbc.properties")
public class Config {
    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        return dataSource;
    }

    /**
     * 这里定义JdbcTemplate的作用是设置一些数据环境,和SimpleJdbcInsert使用没有关联关系
     **/
    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }
}

jdbc.properties

spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&autoReconnectForPools=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

User.java

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

    public User() {
    }

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

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

    public Long getId() {
        return id;
    }

    public void setId(Long 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 +
                '}';
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值