不知道哪里错了
来源:3-1 OneToMany对象关联查询
weixin_慕瓜9083905
2020-02-05 22:17:39
@Test
public void testOneToMany() throws Exception{
SqlSession session = null;
try {
session = MyBatisUtils.openSession();
List<Goods> list = session.selectList("goods.selectOneToMany");
for(Goods goods:list){
System.out.println(goods.getTitle()+":"+goods.getGoodsDetail().size());
}
}catch (Exception e){
throw e;
}finally {
MyBatisUtils.closeSession(session);
}
}
<resultMap id="rmGoods1" type="com.imooc.mybatis.entity.Goods">
<id column="goods_id" property="goodsId"></id>
<!--
collection的含义是在查找结果后对所有对象得到的id字段之带到goodsDetails命名
空间的selectByGoodsId的SQL语句进行查询将得到的赋值给goodsDetails List对象
-->
<collection property="goodsDetails" select="goodsDetail.selectByGoodsId"
column="goods_id"/>
</resultMap>
<select id="selectOneToMany" resultMap="rmGoods1">
select * from t_goods limit 0,1
</select>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="goodsDetail">
<select id="selectByGoodsId" parameterType="Integer" resultType="com.imooc.mybatis.entity.GoodsDetail">
select * from t_goods_detail where goods_id = #{value }
</select>
</mapper>
5回答
同学你好,报错提示Goods类中goodsDetails属性缺少setter方法,如:
建议同学检查一下Goods类中goodsDetails属性是否有get和set方法,如果没有,添加get和set方法后再试试。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
好帮手慕阿满
2020-02-06
同学你好,在同学的代码中,Goods类中的属性是goodsDetail,如:
但是在配置文件中,同学书写属性goodsDetails,多了s,如:
属性名不同,所以报错提示缺少goodsDetails的set方法。
这里建议同学统一一下属性名再试试。
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
weixin_慕瓜9083905
提问者
2020-02-06
已经创建了啊
package com.imooc.mybatis.entity;
import java.util.List;
public class Goods {
private Integer goodsId;//商品编号
private String title;//标题
private String subTitle;//子标题
private Float originalCost;//原始价格
private Float currentPrice;//当前价格
private Float discount;//折扣率
private Integer isFreeDelivery;//是否包邮1包邮,0不包邮
private Integer categoryId;//分类编号
private List<GoodsDetail> goodsDetail;
public List<GoodsDetail> getGoodsDetail() {
return goodsDetail;
}
public void setGoodsDetail(List<GoodsDetail> goodsDetail) {
this.goodsDetail = goodsDetail;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public Float getOriginalCost() {
return originalCost;
}
public void setOriginalCost(Float originalCost) {
this.originalCost = originalCost;
}
public Float getCurrentPrice() {
return currentPrice;
}
public void setCurrentPrice(Float currentPrice) {
this.currentPrice = currentPrice;
}
public Float getDiscount() {
return discount;
}
public void setDiscount(Float discount) {
this.discount = discount;
}
public Integer getIsFreeDelivery() {
return isFreeDelivery;
}
public void setIsFreeDelivery(Integer isFreeDelivery) {
this.isFreeDelivery = isFreeDelivery;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
}
weixin_慕瓜9083905
提问者
2020-02-06
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'goodsDetails' of 'class com.imooc.mybatis.entity.Goods' with value '[com.imooc.mybatis.entity.GoodsDetail@6892b3b6, com.imooc.mybatis.entity.GoodsDetail@6e6f2380, com.imooc.mybatis.entity.GoodsDetail@76a4ebf2, com.imooc.mybatis.entity.GoodsDetail@2e8c1c9b, com.imooc.mybatis.entity.GoodsDetail@53fe15ff, com.imooc.mybatis.entity.GoodsDetail@449a4f23, com.imooc.mybatis.entity.GoodsDetail@1530c739, com.imooc.mybatis.entity.GoodsDetail@5b1669c0, com.imooc.mybatis.entity.GoodsDetail@78e4deb0, com.imooc.mybatis.entity.GoodsDetail@6e9175d8, com.imooc.mybatis.entity.GoodsDetail@7d0b7e3c]' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'goodsDetails' in 'class com.imooc.mybatis.entity.Goods'
### The error may exist in mappers/goods_detail.xml
### The error may involve goodsDetail.selectByGoodsId
### The error occurred while handling results
### SQL: select * from t_goods_detail where goods_id = ?
### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'goodsDetails' of 'class com.imooc.mybatis.entity.Goods' with value '[com.imooc.mybatis.entity.GoodsDetail@6892b3b6, com.imooc.mybatis.entity.GoodsDetail@6e6f2380, com.imooc.mybatis.entity.GoodsDetail@76a4ebf2, com.imooc.mybatis.entity.GoodsDetail@2e8c1c9b, com.imooc.mybatis.entity.GoodsDetail@53fe15ff, com.imooc.mybatis.entity.GoodsDetail@449a4f23, com.imooc.mybatis.entity.GoodsDetail@1530c739, com.imooc.mybatis.entity.GoodsDetail@5b1669c0, com.imooc.mybatis.entity.GoodsDetail@78e4deb0, com.imooc.mybatis.entity.GoodsDetail@6e9175d8, com.imooc.mybatis.entity.GoodsDetail@7d0b7e3c]' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'goodsDetails' in 'class com.imooc.mybatis.entity.Goods'
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:135)
at com.imooc.mybatis.MyBatisTestor.testOneToMany(MyBatisTestor.java:178)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.apache.ibatis.reflection.ReflectionException: Could not set property 'goodsDetails' of 'class com.imooc.mybatis.entity.Goods' with value '[com.imooc.mybatis.entity.GoodsDetail@6892b3b6, com.imooc.mybatis.entity.GoodsDetail@6e6f2380, com.imooc.mybatis.entity.GoodsDetail@76a4ebf2, com.imooc.mybatis.entity.GoodsDetail@2e8c1c9b, com.imooc.mybatis.entity.GoodsDetail@53fe15ff, com.imooc.mybatis.entity.GoodsDetail@449a4f23, com.imooc.mybatis.entity.GoodsDetail@1530c739, com.imooc.mybatis.entity.GoodsDetail@5b1669c0, com.imooc.mybatis.entity.GoodsDetail@78e4deb0, com.imooc.mybatis.entity.GoodsDetail@6e9175d8, com.imooc.mybatis.entity.GoodsDetail@7d0b7e3c]' Cause: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'goodsDetails' in 'class com.imooc.mybatis.entity.Goods'
at org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:185)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.set(BeanWrapper.java:59)
at org.apache.ibatis.reflection.MetaObject.setValue(MetaObject.java:140)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.applyPropertyMappings(DefaultResultSetHandler.java:455)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:404)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:354)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:103)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
... 25 more
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no setter for property named 'goodsDetails' in 'class com.imooc.mybatis.entity.Goods'
at org.apache.ibatis.reflection.Reflector.getSetInvoker(Reflector.java:365)
at org.apache.ibatis.reflection.MetaClass.getSetInvoker(MetaClass.java:167)
at org.apache.ibatis.reflection.wrapper.BeanWrapper.setBeanProperty(BeanWrapper.java:177)
... 41 more
Process finished with exit code -1
好帮手慕阿满
2020-02-06
同学你好,问一下同学报错信息是什么?建议同学将报错信息贴一下,方便我们根据报错具体解决问题。
祝:学习愉快~
相似问题