jdbctemplate

JDBC——Spring JDBC

对着背影说爱祢 提交于 2019-12-18 05:31:49
Spring JDBC Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发 步骤: 1. 导入jar包 2. 创建JdbcTemplate对象。依赖于数据源DataSource JdbcTemplate template = new JdbcTemplate(ds); 3. 调用JdbcTemplate的方法来完成CRUD的操作 update():执行DML语句。增、删、改语句 queryForMap():查询结果将结果集封装为map集合,将列名作为key,将值作为value 将这条记录封装为一个map集合 注意:这个方法查询的结果集长度只能是1 /** * 4.查询id为1001的记录,将其封装为Map集合 * 注意:这个方法查询的结果集长度只能是1 */ public void test4 ( ) { //1. 获取JDBCTemplate对象 JdbcTemplate template = new JdbcTemplate ( JDBCUtils . getDataSource ( ) ) ; String sql = "select * from emp where id = ? or id = ?" ; Map < String , Object > map = template . queryForMap ( sql ,

spring jdbcTemplate

假装没事ソ 提交于 2019-12-18 02:14:03
一、JdbcTemplate主要提供以下五类方法: execute方法: 可以用于执行任何SQL语句,一般用于执行DDL语句; update方法及batchUpdate方法: update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句; query方法及queryForXXX方法: 用于执行查询相关语句; call方法: 用于执行存储过程、函数相关语句。 JdbcTemplate类支持的回调类: 预编译语句及存储过程创建回调: 用于根据JdbcTemplate提供的连接创建相应的语句; PreparedStatementCreator :通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的PreparedStatement; CallableStatementCreator: 通过回调获取JdbcTemplate提供的Connection,由用户使用该Conncetion创建相关的CallableStatement; 预编译语句设值回调: 用于给预编译语句相应参数设值; PreparedStatementSetter: 通过回调获取JdbcTemplate提供的PreparedStatement,由用户来对相应的预编译语句相应参数设值;

multiple one-to-many relations ResultSetExtractor

怎甘沉沦 提交于 2019-12-17 17:54:13
问题 Let's say I have an object with two different one-to-many relations. Much like: Customer 1<->M Brands and Customer 1<->M Orders And let's say that the my object Customer has two lists related to those two objects. I've read this example: http://forum.springsource.org/showthread.php?50617-rowmapper-with-one-to-many-query which explains how to do it with a single one-to-many relationship. For your convenience here's the ResultSetExtractor override: private class MyObjectExtractor implements

spring 事务管理 1(使用spring的JdbcTemplate访问数据库)

自古美人都是妖i 提交于 2019-12-17 16:43:15
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 持久层和事务的关系 dao层脱离事务也能操作数据库,事务是保证dao层数据操作的完整性(即原子性、一致性、隔离性、持久性,也即所谓的 ACID) 事务可以保证一组操作要么全成功,要么全部失败,就是事务是一个不可分割的整体 使用spring 封装的jdbc访问数据库(未使用事务) 定义bean package sping.jdbc; import java.sql.SQLException; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service(value="jdbcWithoutTM") public class JdbcWithoutTM { @Resource(name="jdbcTemplate") private

org.springframework.jdbc.core.JdbcTemplate 使用注意事项

亡梦爱人 提交于 2019-12-17 15:59:24
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在使用JdbcTemplate.queryForMap(String )的时候发现一个问题: 错误信息如下: org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 0 通过查询官方API: https://docs.spring.io/spring/docs/2.0.x/javadoc-api/org/springframework/dao/EmptyResultDataAccessException.html Data access exception thrown when a result was expected to have at least one row (or element) but zero rows (or elements) were actually returned. 注意事项: 如果要是有 queryForMap(sql)和qeuryForInt(sql)和qeuryForLong(sql) 要注意当Map 为空的时候会抛出异常。 如果不想抛出这个异常,要么就用queryForList(sql)吧。 来源: oschina

How to use SELECT IN clause in JDBCTemplates?

依然范特西╮ 提交于 2019-12-17 12:19:31
问题 This is my first experience with JDBCTemplates and I ran into a case where I need to use a query that looks like this: SELECT * FROM table WHERE field IN (?) How do I do that? I already tried passing a list/array value but that didn't do the trick, I get an exception. My current code looks like this: Long id = getJdbcTemplate().queryForLong(query, new Object[]{fieldIds}); Spring Documentation states that there is no way of doing this besides generating the required number of "?" placeholders

Using a prepared statement and variable bind Order By in Java with JDBC driver

被刻印的时光 ゝ 提交于 2019-12-17 09:46:51
问题 I'm using jdbcTemplate to make JDBC connections to a mySQL DB prepared statements to protect myself as much as possible from SQL injection attacks in need to accept requests from the user to sort the data on any of a dozen different columns the following statement jdbcTemplate.query("SELECT * FROM TABLE1 ORDER BY ? ?", colName, sortOrder); Of course this doesn't work, because the variable bindings aren't supposed to specify column names just parameter values for expressions in the query. So..

NamedParameterJdbcTemplate vs JdbcTemplate

落花浮王杯 提交于 2019-12-17 07:14:10
问题 I'm a beginner to Spring3.x , I'm learning Spring DAO support. I want to know the difference between NamedParameterJdbcTemplate and JdbcTemplate. Which one is the best by means of performance. And when to go for NamedParameterJdbcTemplate and when to go for JdbcTemplate. Your answer will help a lot to the beginners like me. 回答1: When you use JdbcTemplate you give it SQL that has a ? placeholder for each parameter you want substituted into the SQL. When you assign parameters in the code you

Springboot整合JDBCTemplate

浪子不回头ぞ 提交于 2019-12-16 11:51:16
概述 前面有关于Springboot整合Mybatis文章, 传送门 ,对于JDBCTemlate实际也是用来操作数据库的持久层框架,这里使用Springboot整合JDBCTemlate,如何使用JDBCTemlate操作数据库,和Springboot整合Mybatis一样,数据库连接池还是使用默认的连接池tomcat.jdbc.pool,我们不再配置Druid或者其他连接池,关于Springboot如何整合Druid或者其他连接池。 整合 导入依赖,在idea中通过Sping提供的插件直接引入,如下 ​ 或者手动导入 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> 创建一个接口 public interface StuDao { public int add(Student student); } 实现类 @Repository public class StuDaoImpl implements StuDao { @Autowired private NamedParameterJdbcTemplate jdbcTemplate; @Override public int

SpringBoot使用JdbcTemplate

偶尔善良 提交于 2019-12-14 12:40:04
1.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> 2.创建实体类 public class Grade { private Integer gradeId; private String gradeName; public Grade() { } public Grade(String gradeName) { this.gradeName = gradeName; } public Grade(Integer gradeId, String gradeName) { this.gradeId = gradeId; this.gradeName = gradeName; } public Integer getGradeId() { return gradeId; } public void setGradeId(Integer gradeId) { this.gradeId = gradeId; } public String getGradeName() { return gradeName; } public void setGradeName(String