我是Spring 3.0的新手.对于DAO访问,我选择了SpringJDBC.SpringJDBC提供了JDBC Template和SimpleJDBCTemplate.哪一个最好.我在一些论坛中读到了SimpleJDBCTemplate,将在Spring 3.1中弃用.这两者有什么区别.指导哪一个是最好的.
我正在使用Spring JDBC,我对如何处理多个一对多关系(或多对多关系)有点不确定.在这种情况下,我将一个存储库注入我的resultsetextractors之一,以便我可以检索其关联.这是这样做的吗?这不好吗?还有其他更好的方法吗?
注意:我遗漏了注册库
public class SomeResultSetExtractor implements ResultSetExtractor {
public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
List result = new LinkedList();
while (rs.next()) {
SomeObject object = new SomeObject(rs.getString(1), rs.getLong(2));
result.add(object);
List<AnotherObject> otherObjects = anotherRepository.findAllById(object.getId);
object.setOtherObjects(otherObjects);
// and so on
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
Okey所以在阅读Dmytro Polivenok之后回答我改为RowMapper界面,而我目前正在使用其他存储库来填充我在我的示例中显示的所有关联.这是一个很好的方式吗?
最近迁移到POSTGRESQL,我试图在创建db表的新条目时获取唯一生成的密钥.该表screenstable如下所示:
CREATE TABLE screenstable
(
id serial NOT NULL,
screenshot bytea,
CONSTRAINT screen_id PRIMARY KEY (id )
)
Run Code Online (Sandbox Code Playgroud)
插入数据的方法screenstable如下:
@Autowired NamedParameterJDBCTemplate template;
public int insertImage(ImageBean imageBean){
String query = "insert into screenstable (screenshot) values (:image)";
SqlParameterSource data = new BeanPropertySqlParameterSource(imageBean);
KeyHolder keyHolder = new GeneratedKeyHolder();
template.update(query, data, keyHolder);
return keyHolder.getKey().intValue();
}
Run Code Online (Sandbox Code Playgroud)
并且ImageBean是
import java.util.Arrays;
public class ImageBean {
private int id;
private byte[] image;
@Override
public String toString() {
return "ImageBean [id=" + id …Run Code Online (Sandbox Code Playgroud) 我正在运行Spring Boot连接到PostgreSQL数据库.我已经验证,如果在数据库之后启动Spring Boot,数据将写入数据库.
spring.datasource.url = jdbc:postgresql://localhost/dbname
spring.datasource.username = user
spring.datasource.password = secret
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
Run Code Online (Sandbox Code Playgroud)
当开发中的数据库发生变化时,我会遇到异常:
PreparedStatementCallback; SQL []; This connection has been closed.; nested exception is org.postgresql.util.PSQLException: This connection has been closed.
请注意,异常发生时可以再次访问数据库; 我认为问题是连接是陈旧的,因为它最初连接的数据库端点由于重新启动而不再可用.重新启动Spring Boot可以解决此问题.
如何配置Spring Boot以重新连接到PostgreSQL,以便我不需要重新启动它?
我试图按照Spring Boot JPA中的答案- 配置自动重新连接和如何在spring jpa中关闭连接时如何重新连接数据库?.我不确定PostgreSQL的行为是否与MySQL不同.我阅读Spring Boot文档没有帮助; 我不知道所描述的组件是否足以理解我应该查看哪些文档.
我使用Spring和JDBC,发现它是自动提交.
如何配置在spring-servlet.xml中将其关闭?
这是我目前的配置:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
Run Code Online (Sandbox Code Playgroud) 我想指定jdbc:embedded-database标记的URL .这不可能吗?
例如,如果我在上下文中有以下内容:
<jdbc:embedded-database type="HSQL" id="dataSource">
<jdbc:script execution="INIT" location="classpath:com/example/init.sql" />
</jdbc:embedded-database>
Run Code Online (Sandbox Code Playgroud)
它将创建一个位于内存中的数据库 jdbc:hsqldb:mem:dataSource
我想要做的是能够拥有不同的bean ID和数据库名称......
例如:
<jdbc:embedded-database type="HSQL" id="dataSource" url="jdbc:hsqldb:mem:testdb">
<jdbc:script execution="INIT" location="classpath:com/example/init.sql" />
</jdbc:embedded-database>
Run Code Online (Sandbox Code Playgroud) 我想使用jdbcTemplate进行提交和回滚.
我的问题是基于这个主题
如何在jdbcTemplate上进行提交或回滚
jdbcTemplate.commit();
jdbcTemplate.rollback();
Run Code Online (Sandbox Code Playgroud)
或者还有一些其他方法可以使用jdbcTemplate实现提交和回滚功能.
我目前正在尝试构建一个使用Spring,Hibernate和Maven(在tomcat上运行)的小型webapp.它工作得很好,除了我无法使我的嵌入式数据库工作.我希望你能帮助我.
当我将webapp部署到Tomcat时,我总是面临这个错误:
匹配的通配符是严格的,但是找不到元素'jdbc:embedded-database'的声明
在我的调查过程中,我了解到这条消息指向缺少的图书馆.因此我添加了我的pom.xml,我添加了工件spring-jdbc.
你能帮我找到错误吗?非常感谢!
这是我的spring-configuration文件,它会在webapp初始化期间导致错误:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<bean id="sessionFactory" class=
"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="embeddedDatasource" />
<property name="packagesToScan" value="org.rest" />
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
</value>
</property>
</bean>
<jdbc:embedded-database id="embeddedDatasource" type="HSQL"/>
<bean id="txManager" class=
"org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
</beans>
Run Code Online (Sandbox Code Playgroud)
这是我的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.marcus</groupId>
<artifactId>maven-webapp-archetype</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>maven-webapp-archetype Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency> …Run Code Online (Sandbox Code Playgroud) 问题是关于RowMapper在主/详细场景中的最佳实践用法,我们希望使用spring jdbc急切地获取详细信息.
假设我们同时拥有Invoice和InvoiceLine类.
public class Invoice{
private BigDecimal invId;
private Date invDate;
private List<InvoiceLine> lines;
}
public class InvoiceLine{
private int order;
private BigDecimal price;
private BigDecimal quantity;
}
Run Code Online (Sandbox Code Playgroud)
当使用带有行映射器的Spring Jdbc时,我们通常会有一个
public class InvoiceMapper implements RowMapper<Invoice>{
public Invoice mapRow(ResultSet rs, int rowNum) throws SQLException {
Invoice invoice = new Invoice();
invoice.setInvId(rs.getBigDecimal("INVID"));
invoice.setInvDate(rs.getDate("INVDATE"));
return invoice;
}
}
Run Code Online (Sandbox Code Playgroud)
现在问题是我想急切地获取与此发票实例相关的InvoiceLine.如果我在rowmapper类中查询数据库会没关系吗?或者任何人更喜欢另一种方式?我使用下面的模式,但不满意.
public class InvoiceMapper implements RowMapper<Invoice>{
private JdbcTemplate jdbcTemplate;
private static final String SQLINVLINE=
"SELECT * FROM INVOICELINES WHERE INVID = ?";
public Invoice …Run Code Online (Sandbox Code Playgroud) Spring JDBC中的数据库方法接受单个参数源.例如 -
int org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(String sql, SqlParameterSource paramSource) throws DataAccessException
Run Code Online (Sandbox Code Playgroud)
是否可以将多个参数源组合在一起?例如,假设我有一个豆Order-
class Order {
int id;
float price;
int customerId;
Date date;
//Lots of other fields
}
Run Code Online (Sandbox Code Playgroud)
我想用一些额外的字段保存这个bean,比如recordModificationTime和accessLevel.
如果我使用MapSqlParameterSource存在于bean之外的这些额外字段,我无法使用,BeanPropertySqlParameterSource因为该方法只接受一个参数源.必须使用MapSqlParameterSource我的所有数据意味着我必须手动提取所有bean属性,这是很多工作.
处理这个问题的最佳方法是什么?
spring ×10
spring-jdbc ×10
java ×7
jdbc ×2
postgresql ×2
jdbctemplate ×1
maven-3 ×1
spring-boot ×1
tomcat7 ×1