本来我需要查询一个shop(商家)对象,其中包含着Dish(菜品)的集合,属于一个一对多的查询
实体类代码如下
//商家实体类
public class Shop {
private String id;
private String name;//店名
private String phone;//电话号
private String address;//店家地址
private double fare;//配送费
private double mincost;//起送价
private int status;//商家状态值用于开关店
private String photo;//店家图片
private String shoptype;//店家类型
private int ischeck;//是否被管理员审核
private List<Dish> dishes;//商家包含菜品集合
//菜品实体类
public class Dish {
private String id;
private String sid;//商家id
private String name;//菜名
private double price;//单价
private int status;//状态值
private String photo;//菜品图片
mybatis中的查询代码如下
<resultMap type="shop" id="SelectById" >
<result property="id" column="s.id" />
<result property="name" column="s.name" />
<result property="photo" column="s.address" />
<result property="status" column="s.status" />
<result property="photo" column="s.photo" />
<result property="ischeck" column="s.ischeck" />
<result property="shoptype" column="s.shoptype" />
<collection property="dishes" ofType="dish" >
<result property="id" column="d.id" />
<result property="sid" column="d.sid" />
<result property="name" column="d.name" />
<result property="price" column="d.price" />
<result property="status" column="d.status" />
<result property="photo" column="d.photo" />
</collection>
</resultMap>
<select id="selectById" resultMap="SelectById">
select
s.id,
s.name,
s.phone,
s.address,
s.fare,
s.mincost,
s.status ,
s.photo ,
s.shoptype,
s.ischeck,
d.id,
d.sid,
d.name,
d.price,
d.status,
d.photo
from
tbshop s
left join
tbdish d
on
s.id=d.sid
where
ischeck in (1,3)
and
s.id=#{id}
</select>
然后运行程序就抛出了异常
严重: Servlet.service() for servlet [springmvc] in context with path [/eleya] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4] with root cause
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:81)
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.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:434)
at com.sun.proxy.$Proxy62.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:167)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53)
at com.sun.proxy.$Proxy65.selectById(Unknown Source)
at com.softeem.service.impl.shop.IShopServiceImpl.goShop(IShopServiceImpl.java:18)
at com.softeem.controller.shop.ShopController.goShop(ShopController.java:49)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:220)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)`这里写代码片`
然后把查询语句用Navicat Premium查询了一下结果如下:
查询语句的结果没问题,这个时候发现查询出的字段名好像不是我预期的s.id s.name,难道是匹配的错误?
然后我就用了log4j输出了一下查询结果
然后看到了log4j的输出日志中查询出的字段并没有加上s. 或者 d.的前缀,这个时候我就明白了我们通过 表名.字段名所查询出来的字段并不会改变,而在可视化工具中为了方便我们辨认所以自动加上了数字后缀.
所以这种方式根本匹配不到数据
然后更改了MyBatis中的语句如下
<resultMap type="shop" id="SelectById" autoMapping="true">
<result property="id" column="s_id" />
<result property="name" column="s_name" />
<result property="photo" column="s_photo" />
<result property="status" column="s_status" />
<collection property="dishes" ofType="dish" autoMapping="true">
</collection>
</resultMap>
<select id="selectById" resultMap="SelectById">
select
s.id s_id,
s.name s_name,
s.phone,
s.address,
s.fare,
s.mincost,
s.status s_status,
s.photo s_photo,
s.shoptype,
s.ischeck,
d.id,
d.sid,
d.name,
d.price,
d.status,
d.photo
from
tbshop s
left join
tbdish d
on
s.id=d.sid
where
ischeck in (1,3)
and
s.id=#{id}
</select>
给重复的字段名取了别名后,就可以完美的匹配到想要的字段了.
异常的问题在这里就解决了
然后我就很好奇,按照第一次的代码我的匹配字段名错了,应该是匹配不到任何数据的,可是为什么会报一个TooManyResultsException异常呢,
这个异常的意思不是结果太多但是是拿对象接收的吗?
然后我就把返回类型改成了List,把它输出出来
[Shop [id=3, name=叫了只炸鸡, phone=13278859150, address=武汉洪山, fare=6.0, mincost=25.0, status=1, photo=shop2.png, shoptype=快餐, ischeck=1, dishes=[Dish [id=3, sid=3, name=叫了只炸鸡, price=18.0, status=1, photo=shop2.png]]]
Shop [id=3, name=叫了只炸鸡, phone=13278859150, address=武汉洪山, fare=6.0, mincost=25.0, status=1, photo=shop2.png, shoptype=快餐, ischeck=1, dishes=[Dish [id=3, sid=3, name=叫了只炸鸡, price=12.0, status=1, photo=shop2.png]]]
Shop [id=3, name=叫了只炸鸡, phone=13278859150, address=武汉洪山, fare=6.0, mincost=25.0, status=1, photo=shop2.png, shoptype=快餐, ischeck=1, dishes=[Dish [id=3, sid=3, name=叫了只炸鸡, price=16.0, status=1, photo=shop2.png]]]
Shop [id=3, name=叫了只炸鸡, phone=13278859150, address=武汉洪山, fare=6.0, mincost=25.0, status=1, photo=shop2.png, shoptype=快餐, ischeck=1, dishes=[Dish [id=3, sid=3, name=叫了只炸鸡, price=12.0, status=1, photo=shop2.png]]]
]
竟然会有四个对象,本来应该是一个商家对应四个菜品存储在一个Shop对象里,现在却有四个对象,每个对象对应了一个菜品,希望大家可以解决我的这个疑惑,欢迎斧正!