Incorrect result size: expected 1, actual 1000

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/87975870

错误背景

使用template查询数据库


 错误信息如下

Exception in thread "main" org.springframework.dao.IncorrectResultSizeDataAccessException: Incorrect result size: expected 1, actual 1000
    at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:74)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:487)
    at cn.com.compent.TemplateJDBC.select(TemplateJDBC.java:27)
    at cn.com.compent.TemplateJDBC.main(TemplateJDBC.java:20)


错误代码如下

package cn.com.compent;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import cn.com.bean.Port;

public class TemplateJDBC {
/*author:命运的信徒
 * date:2019/02/27
 * arm:通过templatjdbc来操控数据库
 */
	
	public static void main(String[] args) {
		select();
	}
	public static void select(){
		ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
		JdbcTemplate jt=(JdbcTemplate) ioc.getBean("jdbcTemplate");
		String sql="select * from Port";
		RowMapper<Port> rowMapper=new BeanPropertyRowMapper<Port>(Port.class);
		List<Port> li=(List<Port>) jt.queryForObject(sql, rowMapper);
		for (Port port : li) {
			System.out.println(port.toString());
		}
		
	}
}

错误原因

这个是查询一个的信息(×)

这个是查询整个的信息 (√)

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/87975870