我想知道是否有一种更优雅的方式来使用Spring的JDBCTemplate进行IN()查询.目前我做的是这样的:
StringBuilder jobTypeInClauseBuilder = new StringBuilder();
for(int i = 0; i < jobTypes.length; i++) {
Type jobType = jobTypes[i];
if(i != 0) {
jobTypeInClauseBuilder.append(',');
}
jobTypeInClauseBuilder.append(jobType.convert());
}
Run Code Online (Sandbox Code Playgroud)
这是非常痛苦的,因为如果我有九行只是为IN()查询构建子句.我想要像准备语句的参数替换
JdbcTemplate中的queryforInt/queryforLong方法在Spring 3.2中已弃用.我无法找出使用这些方法替换现有代码的最佳做法的原因或内容.
一种典型的方法:
int rowCount = jscoreJdbcTemplate.queryForInt(
"SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",
playerNameKey.toUpperCase(),
teamNameKey.toUpperCase()
);
Run Code Online (Sandbox Code Playgroud)
好的,上面的方法需要重写如下:
Object[] params = new Object[] {
playerNameKey.toUpperCase(),
teamNameKey.toUpperCase()
};
int rowCount = jscoreJdbcTemplate.queryForObject(
"SELECT count(*) FROM _player WHERE nameKey = ? AND teamClub = ?",
params, Integer.class);
Run Code Online (Sandbox Code Playgroud)
显然,这种弃用使JdbcTemplate类更简单(或者它?).QueryForInt总是一种方便的方法(我猜)并且已经存在了很长时间.为什么删除它.结果代码变得更加复杂.
我正在使用Jdbctemplate从db中检索单个String值.这是我的方法.
public String test() {
String cert=null;
String sql = "select ID_NMB_SRZ from codb_owner.TR_LTM_SLS_RTN
where id_str_rt = '999' and ID_NMB_SRZ = '60230009999999'";
cert = (String) jdbc.queryForObject(sql, String.class);
return cert;
}
Run Code Online (Sandbox Code Playgroud)
在我的方案中,完全可能不会得到我的查询,所以我的问题是如何解决以下错误消息.
EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
Run Code Online (Sandbox Code Playgroud)
在我看来,我应该回到null而不是抛出异常.我怎样才能解决这个问题?提前致谢.
对于一个新项目,JPA始终是处理关系数据的推荐工具,还是有些情况下Spring JdbcTemplate是更好的选择?您的回复中需要考虑的一些因素:
使用现代(大约2012年)Spring JDBC模板调用存储过程的正确方法是什么?
说,我有一个存储过程声明了两个IN
和OUT
参数,如下所示:
mypkg.doSomething(
id OUT int,
name IN String,
date IN Date
)
Run Code Online (Sandbox Code Playgroud)
我遇到过CallableStatementCreator
必须明确注册IN
和OUT
参数的基础方法.在JdbcTemplate
课堂上考虑以下方法:
public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters)
Run Code Online (Sandbox Code Playgroud)
当然,我知道我可以像这样使用它:
List<SqlParameter> declaredParameters = new ArrayList<SqlParameter>();
declaredParameters.add(new SqlOutParameter("id", Types.INTEGER));
declaredParameters.add(new SqlParameter("name", Types.VARCHAR));
declaredParameters.add(new SqlParameter("date", Types.DATE));
this.jdbcTemplate.call(new CallableStatementCreator() {
@Override
CallableStatement createCallableStatement(Connection con) throws SQLException {
CallableStatement stmnt = con.createCall("{mypkg.doSomething(?, ?, ?)}");
stmnt.registerOutParameter("id", Types.INTEGER);
stmnt.setString("name", "<name>");
stmnt.setDate("date", <date>);
return stmnt;
}
}, declaredParameters);
Run Code Online (Sandbox Code Playgroud)
declaredParameters
当我在csc
实施中注册时,目的是什么?换句话说,为什么我需要传递一个 …
是否可以在Spring jdbc模板调用中从SQL插入中获取@@ identity?如果是这样,怎么样?
在Spring中,如何使用JdbcTemplate在表中插入数据.任何人都可以为我提供代码示例.
在我们的项目中,我们必须在Spring JDBCTemplate和Hibernate之间做出决定.
我想知道哪个在性能,实现和设计方面更好.如何?
我在行映射器和结果集提取器回调接口上工作.我发现了差异,即,
1.Row mapper可以按行处理.但是Resultset提取器我们可以naviagte所有行,返回类型是object.
除了上面有什么区别吗?.Rowmapper内部和返回类型的工作如何列表?
嗨,我正在尝试使用HikariCP与Spring连接池.我正在使用jdbcTempLate和JdbcdaoSupport.
这是我的数据源的spring配置文件:
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
<property name="dataSourceClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="dataSource.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="dataSource.user" value="username"/>
<property name="dataSource.password" value="password"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
但不幸的是,生成以下错误消息:
Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zaxxer.hikari.HikariDataSource]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.zaxxer.hikari.HikariDataSource.<init>()
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何解决这个问题?
jdbctemplate ×10
spring ×10
java ×7
jdbc ×4
spring-jdbc ×3
hibernate ×1
hikaricp ×1
jpa ×1
sql ×1
sql-server ×1