gpt4 book ai didi

java - ResultSetExtractor 类型不是通用的;它不能使用参数 > 进行参数化

转载 作者:行者123 更新时间:2023-12-01 22:32:31 27 4
gpt4 key购买 nike

我正在学习spring。在创建一个示例时,我遇到了错误。

The type ResultSetExtractor is not generic; it cannot be parameterized with arguments <List<Employee>>

我实现的应用程序如下

Employee.java

package com.develop;
public class Employee {
private int id;
private String name;
private float salary;
public Employee(){}
public Employee(int id, String name, float salary){
this.id = id;
this.name = name;
this.salary = salary;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}

}

EmployeeDao.java

package com.develop;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

public class EmployeeDao {
private JdbcTemplate template;

public void setJdbcTemplate(JdbcTemplate template) {
this.template = template;
}

public List<Employee> getAllEmployees(){
return template.query("select * from employee",new ResultSetExtractor<List<Employee>>(){
@Override
public List<Employee> extractData(ResultSet rs) throws SQLException,
DataAccessException {

List<Employee> list=new ArrayList<Employee>();
while(rs.next()){
Employee e=new Employee();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setSalary(rs.getInt(3));
list.add(e);
}
return list;
}
});
}
}

测试.java

 package com.develop;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
List<Employee> list=dao.getAllEmployees();
for(Employee e:list)
System.out.println(e);
}
}

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="oracle" /> -->

<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springdatabase" />
<property name="username" value="root" />
<property name="password" value="admin123" />

</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>

<bean id="edao" class="com.develop.EmployeeDao">
<property name="template" ref="jdbcTemplate"></property>
</bean>

</beans>

已创建表

创建表员工(id号(10),NAME varchar2(100),工资号(10));

最佳答案

错误消息清楚地说明了这一点:

The ResultSetExtractor is not generic.

该接口(interface)在较新版本的 Spring 中变得通用。但无论如何,您仍然可以使用它的原始形式,尽管在处理 extractData() 方法的结果时您将被迫进行强制转换。

public List<Employee> getAllEmployees(){  
return template.query("select * from employee",new ResultSetExtractor(){
@Override
public Object extractData(ResultSet rs) throws SQLException,
DataAccessException {

List<Employee> list=new ArrayList<Employee>();
while(rs.next()) {
Employee e=new Employee();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setSalary(rs.getInt(3));
list.add(e);
}
return list;
}
});
}
<小时/>

附注:我假设您使用的是旧版本的 Spring 框架,因为在较新的版本中,ResultSetExtractor is actually generic 。因此,您可以更新 Spring 版本(但要小心,因为这可能会导致编译问题和其他问题),或者坚持上面代码片段中使用的方法。

关于java - ResultSetExtractor 类型不是通用的;它不能使用参数 <List<Employee>> 进行参数化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27423767/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com