java Spring 3.1.2 RowMapper 参数化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13819995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.1.2 RowMapper parameterization
提问by Mighty
I'm developing a web application using Spring 3.1.2, and need to create a custom row mapper. I have created a private static final class which implements RowMapper, but i'm getting the error message "The type RowMapper is not generic; it cannot be parameterized with arguments ".
我正在使用 Spring 3.1.2 开发 Web 应用程序,并且需要创建自定义行映射器。我创建了一个实现 RowMapper 的私有静态 final 类,但我收到错误消息“类型 RowMapper 不是通用的;它不能用参数参数化”。
All Spring related jars in my lib folder are of 3.1.2.RELEASE version. I have been unable to find anything of the sort elsewhere. Any ideas why this might be happening?
我的 lib 文件夹中所有与 Spring 相关的 jar 都是 3.1.2.RELEASE 版本。我一直无法在其他地方找到类似的东西。任何想法为什么会发生这种情况?
Thanks.
谢谢。
Here is the sample code:
这是示例代码:
public class OutPatient extends Patient{
@Pattern(regexp="[0-9]+", message="OPD No. should only contain digits.")
String opdNo;
public String getOpdNo() {
return opdNo;
}
public void setOpdNo(String opdNo) {
this.opdNo = opdNo;
}
}
DAO Class:
DAO 类:
@Repository("dbHelper")
public class DBHelperImpl{
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public List<OutPatient> fetchOutPatients() {
String sql = "SELECT OPDNO as opdNo FROM `test`.`out_patient`";
@SuppressWarnings("unchecked") //Have to add this annotation to prevent warning
List<OutPatient> outPatients = jdbcTemplate.query(sql, new OutPatientRowMapper());
return outPatients;
}
private static final class OutPatientRowMapper implements RowMapper{ //Unable to add <OutPatient> generics here!
public OutPatient mapRow(ResultSet rs, int rowNum) throws SQLException {
OutPatient outPatient = new OutPatient();
outPatient.setOpdNo(rs.getString("opdNo"));
return outPatient;
}
}
采纳答案by Mighty
I found the solution to my problem after tinkering around a bit more. There were some old spring jars (2.0, I guess) in Tomcat's lib folder. That led to spring using an older version of RowMapper which didnot support parameterization.
经过一番折腾,我找到了解决问题的方法。Tomcat 的 lib 文件夹中有一些旧的 spring jars(我猜是 2.0)。这导致 spring 使用不支持参数化的旧版本 RowMapper。
Solution to this problem is either remove the older jars from the build path, or to change the order of the jars from the Order and Export tab and bring the latest jar to the top.
此问题的解决方案是从构建路径中删除旧的 jar,或者从 Order and Export 选项卡更改 jar 的顺序并将最新的 jar 带到顶部。
Thanks to all those who responded!
感谢所有回复的人!
回答by ross studtman
I had this same kind of issue, Eclipse warning me about RowMapper not being generic.
我遇到了同样的问题,Eclipse 警告我 RowMapper 不是通用的。
So I wrote the import by hand:
所以我手工编写了导入:
import org.springframework.jdbc.core.RowMapper;
导入 org.springframework.jdbc.core.RowMapper;
Which produced this error: The import org.springframework.jdbc.core.RowMapper collides with another import statement
产生了这个错误: The import org.springframework.jdbc.core.RowMapper collides with another import statement
So I looked at my other import statements and what do I find lurking in my Spring project:
所以我查看了我的其他导入语句,我发现我的 Spring 项目中潜伏着什么:
import javax.swing.tree.RowMapper;
导入 javax.swing.tree.RowMapper;
...I deleted that import and then everything worked as it should.
...我删除了该导入,然后一切正常。
回答by Evgeniy Dorofeev
It should be something like this
它应该是这样的
List<Bank> list = t.query(sql, args, new RowMapper<Bank>() {
@Override
public Bank mapRow(ResultSet rs, int rowNum) throws SQLException {
Bank t = new Bank();
t.setName(rs.getString("name"));
return t;
}
});
This code also compiles without warnings
这段代码也编译没有警告
private static final class OutPatientRowMapper implements
RowMapper<OutPatient> {
public OutPatient mapRow(ResultSet rs, int rowNum) throws SQLException {
OutPatient outPatient = new OutPatient();
outPatient.setOpdNo(rs.getString("opdNo"));
return outPatient;
}
}
回答by David Choweller
None of the above answers worked for me. I had all the right Maven dependencies; they were just in the wrong order in the pom.xml file. After I changed the order to the order below, RowMapper was recognized as a parametrized class.
以上答案都不适合我。我拥有所有正确的 Maven 依赖项;它们只是在 pom.xml 文件中的顺序错误。我把顺序改成下面这个顺序后,RowMapper就被识别为参数化类了。
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>20030825.184428</version>
</dependency>
</dependencies>
回答by Ibo
you have to add these dependencies
你必须添加这些依赖项
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
回答by Zanyer
The problem is that it cannot find the library to get RowMapper. The solution is to add the dependency.
问题是它找不到库来获取RowMapper。解决方案是添加依赖项。
These are the ones that you must add to your pom.xml:
这些是您必须添加到 pom.xml 的内容:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>