我是初学者(抱歉,我的错误解释,请随时纠正我)在MyBatis Spring-Boot中,我很难理解并使其适用于@Many
我正在使用3层逻辑编程(表示层,服务层,数据层)
感谢您的帮助:)
我有3个表(如屏幕截图所示是TB_Products,而不是TB_Product):
我想获取表格TB_Users和TB_Products的数据以将其“放入” DTO
我创建4个Java对象类SearchEntity,ProductEntity(用于数据层)
我创建一个接口SearchRepositoryMapper。
我还创建了SearchService接口和SearchServiceImpl。
Java对象类:
SearchEntity
public class SearchEntity implements Serializable{
private static final long serialVersionUID = -9143930742617602050L;
private String id;
private String firstName;
private String lastName;
private List<ProductEntity> products;
// Getters and Setters code .....
}
ProductEntity
public class ProductEntity implements Serializable{
private static final long serialVersionUID = -6525703679290992635L;
private String id;
private String productId;
private String product;
private String number;
private String date;
private String description;
// Getters and Setters code .....
}
SearchRepositoryMapper
public interface SearchRepositoryMapper {
// Get some fields from TB_Users and all fields from TB_Products
@Select("SELECT * FROM TB_Users WHERE id = #{id}")
@Results({
@Result(property = "id", column ="id"),
@Result(property = "firstName", column = "firstName"),
@Result(property = "lastName", column= "lastName"),
@Result(property = "products", javaType = List.class, column="id",
many = @Many(select = "getProductIdByUserId"))})
public SearchEntity findAllInfoByUserId(@Param("id") int id);
@Select("SELECT *, productId FROM TB_Products WHERE productId = #{id}")
public ArrayList<ProductEntity> getProductIdByUserId(@Param("id") int id);
// Find id by uderId and return null if it doesn't exist
@Select("SELECT id FROM TB_Users WHERE userId = #{userId}")
int findIdByUserId(@Param("userId") String userId);
}
SearchServiceImpl
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
SearchRepositoryMapper searchRepository;
@Override
public SearchDto getAllInfoByUserId(String id) {
SearchDto returnValue = new SearchDto(); // Init returnValue as SearchDto
int searchId = searchRepository.findIdByUserId(id); // Init searchId with the TB_Users id
SearchEntity searchEntity = searchRepository.findAllInfoByUserId(searchId);
BeanUtils.copyProperties(searchEntity, returnValue);
return returnValue;
}
}
因此,当我执行代码并执行GET请求时,会收到以下错误消息:
{
"message": "nested exception is org.apache.ibatis.executor.ExecutorException: Statement returned more than one row, where no more than one was expected."
}
我发现它们来自映射器和SearchEntity searchEntity = searchRepository.findAllInfoByUserId(searchId);
但是我不知道如何解决它。我编写代码的方式是错误的
感谢纠正我
答案 0 :(得分:1)
该异常清楚地表明查询返回了多个结果。请验证表中的数据是否正确。