我是Spring3.x的初学者,我正在学习Spring DAO的支持.我想知道NamedParameterJdbcTemplate和JdbcTemplate之间的区别.通过表现哪一个是最好的.什么时候去NamedParameterJdbcTemplate,什么时候去JdbcTemplate.你的答案对我这样的初学者有很大的帮助.
嗨,我正在尝试使用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.queryForMap()并返回Map接口.如何在map内部维护查询数据.我试图加载但是我得到了以下异常,即org.springframework.dao.IncorrectResultSizeDataAccessException:结果不正确
码:-
public List getUserInfoByAlll() {
List profilelist=new ArrayList();
Map m=new HashMap();
m=this.jdbctemplate.queryForMap("SELECT userid,username FROM USER");
Set s=m.keySet();
Iterator it=s.iterator();
while(it.hasNext()){
String its=(String)it.next();
Object ob=(Object)m.get(its);
log.info("UserDAOImpl::getUserListSize()"+ob);
}
return profilelist;
}
Run Code Online (Sandbox Code Playgroud)
Plz帮助我
我想JdbcTemplate在Spring Boot项目中注入一个特定的.我尝试按照此示例进行多种DataSource配置:http://spring.io/blog/2014/05/27/spring-boot-1-1-0-m2-available-now
我的代码编译并运行,但只考虑带有@Primary注释的DataSource ,无论我@Qualifier在SqlService类中放置什么.我的相关代码如下:
DatabaseConfig.java:
@Configuration
public class DatabaseConfig {
@Bean(name = "dsSlave")
@ConfigurationProperties(prefix="spring.mysql_slave")
public DataSource slaveDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.mysql_master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcSlave")
@Autowired
@Qualifier("dsSlave")
public JdbcTemplate slaveJdbcTemplate(DataSource dsSlave) {
return new JdbcTemplate(dsSlave);
}
@Bean(name = "jdbcMaster")
@Autowired
@Qualifier("dsMaster")
public JdbcTemplate masterJdbcTemplate(DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
}
Run Code Online (Sandbox Code Playgroud)
我做了一个快速的服务尝试:
SqlService.java:
@Component …Run Code Online (Sandbox Code Playgroud) 我正在使用JDBC模板,并希望使用预准备语句从数据库中读取.我在.csv文件中迭代多行,并在每一行上执行一些带有相应值的SQL select查询.
我想加快我对数据库的阅读速度,但我不知道如何让JDBC模板与预处理语句一起工作.
有PreparedStatementCreator和PreparedStatementSetter.在本示例中,它们都是使用匿名内部类创建的.但是在PreparedStatementSetter类中,我无法访问我想在预准备语句中设置的值.
因为我正在迭代.csv文件,所以我不能将它们硬编码为String,因为我不知道它们.我也无法将它们传递给PreparedStatementSetter,因为构造函数没有参数.将我的价值观设定为最终值也是愚蠢的.
我习惯于创建准备好的语句非常简单.就像是
PreparedStatement updateSales = con.prepareStatement(
"UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate():
Run Code Online (Sandbox Code Playgroud)
就像在这个Java教程中一样.
Spring的JdbcTemplate抽象提供了许多功能,但它是否可以以提供SQL注入攻击保护的方式使用?
例如,就像使用PreparedStatement并使用正确定义的参数化获得的保护类型一样.
我正在使用spring jdbctemplate并运行如下查询:
SELECT COLNAME FROM TABLEA GROUP BY COLNAME
没有传递任何命名参数,但是,列名称COLNAME将由用户传递.
问题
有没有办法让占位符,比如?列名?例如SELECT ? FROM TABLEA GROUP BY ?
如果我想简单地运行上面的查询并得到一个List<String>最好的方法?
目前我在做:
List <Map<String, Object>> data = getJdbcTemplate().queryForList(query);
for (Map m : data)
System.out.println(m.get("COLNAME"));
Run Code Online (Sandbox Code Playgroud) 我想知道从表中选择记录的最佳做法是什么.我在下面提到了两种方法,我想知道哪种方法是使用Spring JdbcTemplate从表中选择数据的最佳方法.
try {
String sql = "SELECT id FROM tableName WHERE column_name = '" + coulmn value + "'";
long id = jdbcTemplate.queryForObject(sql, Long.class);
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(e);
}
}
Run Code Online (Sandbox Code Playgroud)
这会引发以下异常:
预计1实际0喜欢
当表不包含任何数据时.我的朋友告诉这不是选择数据的最佳做法.他建议下面提到的代码是选择数据的唯一最佳实践.
try {
String countQuery = "SELECT COUNT(id) FROM tableName";
int count = jdbcTemplate.queryForInt(countQuery);
if (count > 0) {
String sql = "SELECT id FROM tableName WHERE column_name = '" + coulmn value + "'";
long id = jdbcTemplate.queryForObject(sql, …Run Code Online (Sandbox Code Playgroud) 我正在尝试找到更快的批量插入方法.
我试图用jdbcTemplate.update(String sql)插入几个批处理,其中sql是由StringBuilder 构建的,如下所示:
INSERT INTO TABLE(x, y, i) VALUES(1,2,3), (1,2,3), ... , (1,2,3)
Run Code Online (Sandbox Code Playgroud)
批量大小正好是1000.我插入了近100批.我使用StopWatch检查了时间并找出了插入时间:
min[38ms], avg[50ms], max[190ms] per batch
Run Code Online (Sandbox Code Playgroud)
我很高兴,但我想让我的代码变得更好.
之后,我尝试使用jdbcTemplate.batchUpdate,如:
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
// ...
}
@Override
public int getBatchSize() {
return 1000;
}
});
Run Code Online (Sandbox Code Playgroud)
sql的样子
INSERT INTO TABLE(x, y, i) VALUES(1,2,3);
Run Code Online (Sandbox Code Playgroud)
我很失望!jdbcTemplate以分开的方式执行1000行批处理的每个插入.我在mysql_log上找到了,发现有一千个插入.我使用StopWatch检查了时间并找出了插入时间:
min [900ms],avg [1100ms],每批最大[2000ms]
那么,任何人都可以向我解释一下,为什么jdbcTemplate在这个方法中做了单独的插入?为什么方法的名称是batchUpdate?或者可能是我以错误的方式使用这种方法?
这个问题几乎总结在标题中.当查询没有返回结果时,JdbcTemplate.queryForList()将返回什么.它会返回一个空的List或null值吗?我无法从文档中找到明确的答案.提前致谢!
jdbctemplate ×10
spring ×9
java ×8
jdbc ×4
spring-jdbc ×3
hikaricp ×1
mysql ×1
spring-batch ×1
spring-boot ×1