Spring JDBC(二)SimpleJdbcInsert
上一篇写了关于jdbcTemplate的一些基本使用,这一篇来聊聊SimpleJdbcInsert
SimpleJdbcInsert是springjdbc提供的一个简化插入操作的类,下面来看一下常用的api
创建SimpleJdbcInsert实例
创建一个用户表
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`password` varchar(255) DEFAULT NULL,
`user_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建实例
private SimpleJdbcInsert simpleJdbcInsert;
private DriverManagerDataSource dataSource;
@Before
public void init() {
dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/blogsrc?useUnicode=true&characterEncoding=UTF-8");
dataSource.setUsername("root");
dataSource.setPassword("zhao");
simpleJdbcInsert = new SimpleJdbcInsert(dataSource);
}
新增
通过withTableName绑定需要操作的数据库表,然后指定新增的参数
/**
* simpleJdbcInsert新增
*/
@Test
public void insertTest() {
Map<String, Object> parameters = new HashMap<String, Object>(3);
parameters.put("user_name", "小明");
parameters.put("password", "123456");
simpleJdbcInsert.withTableName("user").execute(parameters);
}
返回主键
通过usingGeneratedKeyColumns指定主键,executeAndReturnKey执行并返回主键,返回的主键为Number类型,如有需要,自行转换
/**
* simpleJdbcInsert新增,并返回主键
*/
@Test
public void insertAndReturnPrimaryKeyTest() {
Map<String, Object> parameters = new HashMap<String, Object>(3);
parameters.put("user_name", "小明");
parameters.put("password", "123456");
Number primaryKey = simpleJdbcInsert.withTableName("user")
.usingGeneratedKeyColumns("id")// 指定主键列名
.executeAndReturnKey(parameters);
System.out.println("主键为:" + primaryKey);
}
限制插入的列
usingColumns方法可以限制插入的列
/**
* simpleJdbcInsert新增,返回主键,并限制插入的列
*/
@Test
public void usingColumnsTest() {
Map<String, Object> parameters = new HashMap<String, Object>(3);
parameters.put("user_name", "小明");
parameters.put("password", "123456");
Number primaryKey = simpleJdbcInsert.withTableName("user")
.usingColumns("user_name")// 限制输入的列,因为限制只新增用户名,所以password的值不会进数据库
.usingGeneratedKeyColumns("id")// 指定主键列名
.executeAndReturnKey(parameters);
System.out.println("主键为:" + primaryKey);
}
SqlParameterSource
SqlParameterSource接口对sql参数进行了封装,两个常用的实现BeanPropertySqlParameterSource和MapSqlParameterSource,BeanPropertySqlParameterSource可以通过javabean构造,MapSqlParameterSource则可以用map构造
@Test
public void sqlParameterSourceTest() {
User user = new User();
user.setUserName("小明");
user.setPassword("123456");
// BeanPropertySqlParameterSource
SqlParameterSource sqlParm = new BeanPropertySqlParameterSource(user);
simpleJdbcInsert.withTableName("user").execute(sqlParm);
// MapSqlParameterSource
simpleJdbcInsert = new SimpleJdbcInsert(dataSource);
Map<String, Object> mapParameters = new HashMap<String, Object>();
mapParameters.put("user_name", "小明");
mapParameters.put("password", "123456");
SqlParameterSource sqlParmMap = new MapSqlParameterSource(mapParameters);
simpleJdbcInsert.withTableName("user").execute(sqlParmMap);
simpleJdbcInsert = new SimpleJdbcInsert(dataSource);
// 也可以通过MapSqlParameterSource addValue 添加参数
SqlParameterSource sqlParmMapAdd = new MapSqlParameterSource()
// addValues(map) 一次添加多个参数
.addValue("user_name", "小明")// 单个添加
.addValue("password", "123456");
simpleJdbcInsert.withTableName("user").execute(sqlParmMapAdd);
}
需要注意jdbcTemplate是线程安全的,所以可以一直使用同一个实例,但simpleJdbcInsert不是线程安全的,每次使用都要获取一个新的simpleJdbcInsert实例
完整的源码 https://github.com/zhaoguhong/blogsrc
Spring JDBC(二)SimpleJdbcInsert的更多相关文章
- Spring JDBC SimpleJdbcInsert类示例
org.springframework.jdbc.core.SimpleJdbcInsert类是一个多线程,可重用的对象,为将数据插入表提供了易用的功能.它提供元数据处理以简化构建基本insert语句 ...
- spring学习二:jdbc相关回顾以及spring下dao
目录: Part一:回顾java web部分的jdbc.事务.连接池和dbutils工具等 : Part二:spring的JdbcTemplate使用: Part三:spring的事务处理: Part ...
- Spring(十二)之JDBC框架
JDBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQ ...
- Spring JDBC
转载:博客主页:http://blog.csdn.NET/chszs 一.概述 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1)core即核心包,它包含了JDBC的核心功能.此包内 ...
- Spring JDBC SqlQuery类示例
org.springframework.jdbc.object.SqlQuery类提供了表示SQL查询的可重用操作对象. 使用到的 Student 表的结构如下 - CREATE TABLE Stud ...
- Spring学习进阶(四) Spring JDBC
Spring JDBC是Spring所提供的持久层技术.主要目的是降低使用JDBC API的门槛,以一种更直接,更简洁的方式使用JDBC API.在Spring JDBC里用户仅需要做哪些比不可少的事 ...
- Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...
- Spring04-SpringEL&Spring JDBC数据访问
一. SpringEL入门 Spring动态语言(简称SpEL) 是一个支持运行时查询和操作对象图的强大的动态语言,语法类似于EL表达式,具有诸如显示方法和基本字符串模板函数等特性. 1. 准备工作 ...
- ref:Spring JDBC框架
ref:https://blog.csdn.net/u011054333/article/details/54772491 Spring JDBC简介 先来看看一个JDBC的例子.我们可以看到为了执行 ...
- Spring JDBC SqlUpdate类示例
org.springframework.jdbc.object.SqlUpdate类提供了表示SQL更新的可重用操作对象. 使用到的 Student 表的结构如下 - CREATE TABLE Stu ...
随机推荐
- 视觉词袋模型(BOVW)
一.介绍 Bag-of-words model (BoW model) 最早出现在神经语言程序学(NLP)和信息检索(IR)领域. 该模型忽略掉文本的语法和语序, 用一组无序的单词(words)来表达 ...
- Internal类
C#中一个类中的成员有四种修饰级别: public:完全开放,谁都能访问. private:完全封闭,只有类自身可以访问. internal:只对相同程序集,或使用InternalVisibleToA ...
- HDU1175 连连看(DFS)
Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经 ...
- Codeforces Round #378 (Div. 2)-C. Epidemic in Monstropolis
C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...
- 0_Simple__fp16ScalarProduct
使用cuda内置无符号整数结构(__half2)及其汇编函数,计算两个向量的内积. 源代码: #include <cstdio> #include <cstdlib> #inc ...
- HTML学习笔记 w3sCss盒子模型应用案例(div布局) 第十一节 (原创) 参考使用表
* { margin: 0px; padding: 0px; } .top { width: 100%; height: 50px; background-color: antiquewhite; } ...
- 06.十分钟学会表达式语言EL
一. 概念:MVC设计模式一个主要好处就是让jsp中的代码越来越来少,而且规定只能出现三种代码:接收属性,判断语句,迭代输出.但是,在开发中,jsp输出至少还是需要接受VO对象的,这时候为了避免导入V ...
- Java 核心内容相关面试题【3】
目录 面向对象编程(OOP) 常见的Java问题 Java线程 Java集合类 垃圾收集器 异常处理 Java小应用程序(Applet) Swing JDBC 远程方法调用(RMI) Servlet ...
- 有关ActiveXObject的兼容性问题(浏览器的特有属性)
这个问题还得从一开始时候学习有关javascript中有关对文件的一些操作. 对于每个前端的人应该都清楚有关File对象,其中包括多种方法,就不一一描述了,比如说她是通过FileSystemObjec ...
- docker下编译mangoszero WOW60级服务端(一)
这几天看到暴雪准备开放怀旧服的新闻,突然想到几年前用大芒果window一键服务端自己搭建过服务,就想着在Linux环境下重新编译一套,毕竟Linux作为服务端,性能和稳定性都会高一些,于是在mac虚拟 ...