jdbctemplate

using spring jdbc template for populating results

孤街浪徒 提交于 2019-12-06 03:55:56
问题 I have two classes class Deptartment{ int deptid, String deptname; List<Employee> employees; } class Employee{ int empid; String empname; int deptid; } Table: Department: deptid,deptname Employee empid,empname,deptid Query: select * from deptartment d,employee e where d.deptid= e.deptid Now how can i populate Department object using spring jdbc template? 回答1: To help Sean Patrick Floyd, here's his solution with a single query : final Map<Integer, Department> departments = new HashMap<Integer,

getting DateTime from ResultSet in JdbcTemplate

时光怂恿深爱的人放手 提交于 2019-12-06 01:26:59
问题 in database my column is of type TIMESTAMP, so my class has properties of type Datetime like this: public void setDiscoveryDate(final DateTime discoveryDtTm) { this.discoveryDtTm = discoveryDtTm; } now in JdbcTemplate I want to get it, so some code like this: variant.setDiscoveryDate(rs.getTimestamp("discovery_dt_tm")); which does Not work because column the get for resultset I could not find something that returns DateTime, I only saw either getDate or getTime. 回答1: That's because DateTime

Spring @Cacheable methods with lists

我的梦境 提交于 2019-12-05 20:29:50
问题 I'm using latest Ehcache in my Spring 4.1.4 application. What I have is: class Contact{ int id; int revision; } @Cacheable("contacts") public List<Contact> getContactList(List<Integer> contactIdList) { return namedJdbc.queryForList("select * from contact where id in (:idlist)", Collections.singletonMap("idlist", contactIdList)); } @CachePut(value="contact", key = "id") public void updateContact(Contact toUpdate) { jdbctemplate.update("update contact set revision = ? where id = ?", contact

Spring JdbcTemplate与Spring NamedParameterJdbcTemplate

眉间皱痕 提交于 2019-12-05 16:34:45
JdbcTemplate简介   Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。   JdbcTemplate位于 中。其全限定命名为org.springframework.jdbc.core.JdbcTemplate。   要使用JdbcTemlate还需一个 这个包包含了一下事务和异常控制    JdbcTemplate主要提供以下五类方法: execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句; update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句; query方法及queryForXXX方法:用于执行查询相关语句; call方法:用于执行存储过程、函数相关语句。 案例分析 1.新建一个属性配置文件 1 jdbc.user=root 2 jdbc.password=123456 3 jdbc.driverClass=com.mysql.jdbc.Driver 4 jdbc.jdbcUrl=jdbc\:mysql\:///test 2.配置Spring配置文件applicationContext.xml 1 <context:property-placeholder

JDBC、JDBCTemplate、MyBatis、Hiberante 比较与分析

夙愿已清 提交于 2019-12-05 16:33:12
JDBC (Java Data Base Connection,java数据库连接) JDBC(Java Data Base Connection,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成.JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序 优点:运行期:快捷、高效 缺点:编辑器:代码量大、繁琐异常处理、不支持数据库跨平台 JDBCTemplate JdbcTemplate针对数据查询提供了多个重载的模板方法,你可以根据需要选用不同的模板方法.如果你的查询很简单,仅仅是传入相应SQL或者相关参数,然后取得一个单一的结果,那么你可以选择如下一组便利的模板方法 优点:运行期:高效、内嵌Spring框架中、支持基于AOP的声明式事务 缺点:必须于Spring框架结合在一起使用、不支持数据库跨平台、默认没有缓存 MyBatis MyBatis的前身就是iBatis,iBatis本是apache的一个开源项目,2010年这个项目由apahce sofeware foundation 迁移到了google code,并且改名 总体来说 MyBatis 主要完成两件事情 根据JDBC 规范建立与数据库的连接 通过Annotaion/XML+JAVA反射技术

Spring-JDBC

╄→尐↘猪︶ㄣ 提交于 2019-12-05 15:05:12
Spring-JDBC 是指 Spring 框架对 JDBC 的简单封装, 提供了一个 JDBCTemplate 对象简化 JDBC 的开发. 使用步骤: 导入 jar 包 创建 JdbcTemplate 对象, 依赖于数据源 DataSource JdbcTemplate template = new JdbcTemplate(ds) 调用 JdbcTemplate 的方法来完成 CURD 的操作. update(): 执行 DML 语句, 增, 删, 改 语句. queryForMap(): 查询结果将结果集封装为 map 集合. 将列名作为 key, 值作为 value, 这个方法查询的结果长度只能是 1 queryForList(): 查询结果将结果集封装为 List 集合. 将每一条记录封装为一个 map 集合, 再将 map 集合装载到 List 集合中. query(): 查询结果, 将结果封装为 JavaBean 对象. 对象的属性值类型必须是引用类型 (查询的值可能是 null ) (使用包装类) query 的参数: RowMapper: 一般我们使用 BeanPropertyRowMapper 实现类, 可以完成数据到 JavaBean 的自动封装 ( 反序列化的概念 ) query(querySql, new BeanPropertyRowMapper

JDBCTemplate optional parameters

梦想的初衷 提交于 2019-12-05 11:13:13
I am using spring JDBCTemplate. I have a scenario, where the parameters that need to be passed into my query function, are conditional/optional. For example, I have the following code: List<RealTimeDTO> result = jdbcTemplate.query(sql, new Object[] {custId, number, requestType, startDate, endDate}, new CCCRowMapper()); In the code, I passed in custId, number, requestType, etc. However, requestType is an optional parameter that may come back as null or empty so I don't want it to be passed into the Object[] if it is either null or empty . What can I do to handle this type of situation? I could

How to use JdbcTemplate to perform Join queries

前提是你 提交于 2019-12-05 07:26:30
I have the following database model create table Diary (id bigint NOT NULL AUTO_INCREMENT, creationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, name varchar(255) not null, description text, viewtype varchar(255) not null, member bigint, primary key (id), foreign key (member) references Member(id)); create table Page (id bigint NOT NULL AUTO_INCREMENT, creationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP, viewtype varchar(255) not null, diary bigint, member bigint, primary key (id), foreign key (diary) references Diary(id), foreign key (member) references Member(id)); create table Comment (id bigint

simplest way to collect all SQL sent out

▼魔方 西西 提交于 2019-12-05 06:12:00
问题 For many reasons I prefer not to disclose (long and boring story), I need to capture the interactions of a complex application with the Database. The application builds on top of Spring/JdbcTemplate and I need to find all the SQL sent out by this application. How can I do that in the simplest possible way? Creating a pseudo mock implementation of JdbcTemplate does not seem reasonable. First off JdbcTemplate is a class and not an interface. Second it has a large interface that makes it tedious

Spring NamedParameterJDBCTemplate reuse of Prepared Statements

泄露秘密 提交于 2019-12-05 05:38:36
I am using the Spring NamedParameterJdbcTemplate to fetch some values from a table. For some reason, the query runs very slow in my Java app as opposed to running the same query on SQL Management Studio. I also noticed in the profiler, the prepared statements don't get reused. If I run the same query in my JAVA app multiple times, I see different prepared statements being executed. So, not sure why the statements are not getting reused. Is the performance slow because I am using a IN clause in my query? Here is my sample java code StringBuilder vQuery = new StringBuilder(); vQuery.append("