我使用JdbcTemplate从我的Spring DAO类调用存储过程.我的问题是,存储过程返回多个表.有没有办法使用Spring JdbcTemplate访问多个表.
如果我使用
jdbcTemplate.queryForList(myStoredProc, new Object[]{parameters}
iam只从结果中获取第一个表.
我的数据库是SQL Server 2005.
除了jdbcTemplate之外,还有其他方法可以满足我的要求.如果是,请告诉我.
提前致谢....
Spring JDBC模板中似乎没有命名查询支持.通过命名查询,我的意思是在java代码中按名称引用sql语句的工具,并将实际语句保存在某个配置文件中.
在没有开箱即用支持的情况下,我正在研究将sql语句保留在java代码之外的最佳方法.
以下是替代方案:
评论?
目前我们的代码使用JdbcTemplate的batchUpdate方法来进行批量插入.
我的问题是,如果其中一个更新中的任何异常如何处理它(假设只是添加日志)并继续下一个更新sql语句?
另外,JdbcTemplate的batchUpdate()方法如何处理异常?
这里的代码片段.
/**
* Saves the list of <code>Item</code> objects to the database in a batch mode
*
* @param objects
* list of objects to save in a batch mode
*/
public void save(final List<Item> listOfItems) {
for (List<Debit> list : listOfItems) {
getJdbcTemplate().batchUpdate(insertItem, new ItemBatchPreparedStatementSetter(list));
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含字符串,int和boolean字段的类.我为他们宣布了吸气剂和制定者.
public class SomeClass {
private int id;
private String description;
private boolean active;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
Run Code Online (Sandbox Code Playgroud)
我是BeanPropertyRowMapper来从Oracle DB获取所有对象.
@Override
public List<Destination> getAll() {
List<SomeClass> objs = jdbcTemplate.query(
myQuery, new BeanPropertyRowMapper<SomeClass>(SomeClass.class));
return objs;
} …Run Code Online (Sandbox Code Playgroud) 我需要UPSERTPostgres的功能.由于Postgres本身不支持这个,我编写了一个函数来执行该操作(尝试更新,如果没有更新行则插入)
这是该功能的模板:https://stackoverflow.com/a/1109198/681671
CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
LOOP
-- first try to update the key
UPDATE db SET b = data WHERE a = key;
IF found THEN
RETURN;
END IF;
-- not there, so try to insert the key
-- if someone else inserts the same key concurrently,
-- we could get a unique-key failure
BEGIN
INSERT INTO db(a,b) VALUES (key, data);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- …Run Code Online (Sandbox Code Playgroud) 我是Java Spring的新手并学习JDBC模板来访问数据库.现在我有3个关系表,我需要使用JDBC模板加入它们并需要打印结果.我该如何实现它.任何工作的例子肯定会帮助我很多.
谢谢
我正在攻读Spring Core认证,我对使用JdbcTemplate有一些疑问.
我知道JdbcTemplate为我提供了不同的方法来执行查询(用SQL编写).
所以我有以下方法:
1)queryForObject():
public long getPersonCount() {
String sql = “select count(*) from PERSON”;
return jdbcTemplate.queryForObject(sql, Long.class);
}
Run Code Online (Sandbox Code Playgroud)
所以在这个方法中我指定了2个参数,分别表示返回对象的SQL语句和类类型.在前一种情况下,指定将查询结果返回到Long对象.
如果从查询返回的结果与指定的类型不匹配,会发生什么?(例如,如果查询返回一个String并且我已经指定了一个Long作为queryForObject()方法的参数?这样的情况如何处理?
2)queryForMap():
public Map getPersonInfo(int id) {
String sql = “select * from PERSON where id=?”;
return jdbcTemplate.queryForMap(sql, id);
}
Run Code Online (Sandbox Code Playgroud)
阅读文档在我看来,只有当我的查询返回单行时才使用它.这是对的吗?
所以现在我怀疑与queryForMap()方法的使用有关.
我知道Map接口使用存储对象 系统.
因此,Map被认为存储多个夫妇.所以我认为这可以存储多行:Map的键是我表的一行的键,值包含其他列的值(到一个对象).
但似乎queryForMap()方法的逻辑是完全不同的.它是如何工作的?
也许它会返回一个以这种方式工作的Map:
key:包含表的主键的键,并且与此键相关联的多个值与我表的其他字段的内容相关.或者是什么?
3)queryForList():
public List getAllPersonInfo() {
String sql …Run Code Online (Sandbox Code Playgroud) 我有一个表,其中包含一个自动递增的主键和一个唯一键:
CREATE TABLE `product` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`canonical_url` varchar(750) CHARACTER SET latin1 NOT NULL,
...
PRIMARY KEY (`id`),
UNIQUE KEY `canonical_url_idx` (`canonical_url`)
Run Code Online (Sandbox Code Playgroud)
如果canonical_url已存在,我使用on duplicate key功能更新记录:
"INSERT INTO product(id, canonical_url, name VALUES(?, ? ?) ON DUPLICATE KEY UPDATE name=VALUES(name), id=LAST_INSERT_ID(id)"
KeyHolder productKeyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement(productSql, new String[] {"id"});
ps.setInt(1, id);
ps.setString(2, canonicalUrl);
ps.setString(3, name);
}, productKeyHolder);
final int productId = productKeyHolder.getKey().intValue();
Run Code Online (Sandbox Code Playgroud)
问题是我收到了这个错误:
只有在返回单个键时才应使用getKey方法.当前键条目包含多个键:[{GENERATED_KEY = 594},{GENERATED_KEY = 595}]
有谁知道是什么原因造成的?
我已经在application.properties中设置了spring.datasource。*:
spring.datasource.url=jdbc:h2:./data/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
Run Code Online (Sandbox Code Playgroud)
然后我配置了JdbcTemplate Bean
@Bean
@Autowired
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
Run Code Online (Sandbox Code Playgroud)
但是当我启动应用程序时,我在控制台中看到了
Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
Run Code Online (Sandbox Code Playgroud)
而不是我的设置。为什么?
spring database-connection spring-jdbc properties-file spring-boot
我将DataSource定义为bean:
@Bean(name="dataSource")
public DriverManagerDataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:~/myDB");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource;
}
Run Code Online (Sandbox Code Playgroud)
它工作完美,但是我需要为数据库创建指定模式并在其上加载数据。是否有机会像Spring Data一样执行两个脚本(模式脚本和数据脚本)?我发现的唯一的东西是datasource.setSchema(),以及我所担心的,我必须指定它的完整路径。如果我的模式脚本位于src/main/resources/路径中,那么如何指定它呢?(我确实做了,文档怎么说,但失败并显示一条消息)
发生意外错误(类型=内部服务器错误,状态= 500)。org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接。嵌套的异常是org.h2.jdbc.JdbcSQLException:找不到架构“〜/ schema-h2.sql” [90079-193]
谢谢你的建议
spring-jdbc ×10
spring ×6
java ×5
jdbc ×3
jdbctemplate ×2
boolean ×1
database ×1
mysql ×1
oracle ×1
postgresql ×1
spring-boot ×1
spring-mvc ×1
sql ×1