添加评论500报错

添加评论500报错

http://img1.sycdn.imooc.com//climg/5dc5913d091d880314510568.jpg

username的字段名不知道哪里错了。。



Comment.xml

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.bbs.dao.CommentDao">
    <resultMap id="comment" type="Comment">
        <id property="id" column="id" javaType="Integer"/>
        <result property="userId" column="user_id" javaType="Integer"/>
        <result property="username" column="username" javaType="String"/>
        <result property="content" column="content" javaType="String"/>
        <result property="createTime" column="create_time" javaType="java.util.Date"/>
        <result property="postId" column="post_id" javaType="Integer"/>
    </resultMap>
    <insert id="insert" parameterType="Comment" useGeneratedKeys="true">
        insert into comment(user_id,username,content,create_time,post_id) values (#{userId},${username},#{content},#{createTime},#{postId})
    </insert>
    <select id="selectByPid" parameterType="Integer" resultMap="comment">
        select * from comment where post_id=#{pid}
    </select>
</mapper>

controller

@Controller("commentController")
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentBiz commentBiz;

    @RequestMapping(value = "/add",params = "pid")
    public String add(Integer pid,Comment comment){
        commentBiz.add(comment);
        return "redirect:/post/get?id="+pid;
    }

}

biz

@Service("commentBiz")
public class CommentBizImpl implements CommentBiz {

    @Autowired
    @Qualifier("commentDao")
    private CommentDao commentDao;

    public void add(Comment comment) {
        commentDao.insert(comment);
    }

页面

<form:form method="POST" action="/comment/add?pid=${post.id}" accept-charset="UTF-8" id="thread_create_form" class="create_form" modelAttribute="comment">
    <form:input type="hidden" path="postId" value="${post.id}"/>
    <form:input type="hidden" path="userId" value="${user.id}"/>
    <form:input type="hidden" path="username" value="${user.username}"/>
    <div class="form-group">
        <form:textarea cssClass="post-editor form-control" rows="5" style="overflow:hidden" path="content" placeholder="请使用Markdown撰写内容" cols="50"></form:textarea>
    </div>
    <div class="form-group status-post-submit">
        <input class="btn btn-primary col-xs-2" id="thread-create-submit" type="submit" value="回复">
    </div>
</form:form>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

5回答
好帮手慕柯南 2019-11-09 17:12:05

同学你好!

1.老师看了报错信息也没有发现是哪一行报的错,建议同学在以下两行代码去打断点,看一下是在执行哪一条语句时报错了

http://img1.sycdn.imooc.com//climg/5dc6823909c8a62a06760148.jpg

2.找到是哪一行报错,然后在根据报错的信息具体解决,

祝学习愉快~


提问者 hblack 2019-11-09 16:35:07

报错信息

09-Nov-2019 16:04:57.640 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
09-Nov-2019 16:04:58.125 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Initializing Spring FrameworkServlet 'springMVC'
09-Nov-2019 16:32:47.541 严重 [http-nio-8080-exec-10] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [springMVC] in context with path [] 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: 2] with root cause
	org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 2
		at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:81)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
		at com.sun.proxy.$Proxy19.selectOne(Unknown Source)
		at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
		at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
		at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
		at com.sun.proxy.$Proxy23.selectById(Unknown Source)
		at com.imooc.bbs.biz.impl.PostBizImpl.getById(PostBizImpl.java:36)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
		at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
		at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
		at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
		at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
		at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
		at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
		at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
		at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
		at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
		at com.sun.proxy.$Proxy24.getById(Unknown Source)
		at com.imooc.bbs.controller.PostController.get(PostController.java:51)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
		at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
		at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
		at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
		at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
		at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
		at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
		at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
		at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
		at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
		at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
		at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
		at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
		at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
		at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
		at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
		at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
		at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
		at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
		at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
		at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528)
		at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
		at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
		at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
		at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
		at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
		at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798)
		at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
		at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)
		at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
		at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
		at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
		at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
		at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
		at java.lang.Thread.run(Thread.java:748)

Post实体类

public class Post {
    private Integer id;
    private String username;
    private String title;
    private String category;
    private String content;
    private String tag;
    private Date createTime;
    private User user;
    private List<Comment> comments;

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
}


好帮手慕柯南 2019-11-09 16:28:02

同学你好!

同学这里应该是在查询时报的错,这里是查询出了多条数据

  1. 同学的Post实体类中的comment是否是List接收的呢?

  2. 如果是List接收的,老师希望同学提供一下以下信息:报错信息中通常会有是代码的第几行报错了,建议同学贴一下完整的报错信息,以及实现类。老师看一下呢。

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 hblack #1
    实体类和报错信息贴在楼上
    2019-11-09 16:35:29
提问者 hblack 2019-11-09 15:30:10

postDao

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.bbs.dao.PostDao">
    <resultMap id="post" type="Post">
        <id property="id" column="id" javaType="Integer"/>
        <result property="username" column="username" javaType="String"/>
        <result property="title" column="title" javaType="String"/>
        <result property="category" column="category" javaType="String"/>
        <result property="content" column="content" javaType="String"/>
        <result property="tag" column="tag" javaType="String"/>
        <result property="createTime" column="create_time" javaType="java.util.Date"/>
        <!--
        <association property="user" column="username" javaType="User">
            <id property="id" column="uid" javaType="Integer"/>
            <result property="username" column="uname" javaType="String"/>
        </association>
        -->
        <collection property="comments" column="id" ofType="Comment" select="com.imooc.bbs.dao.CommentDao.selectByPid"/>
    </resultMap>
    <insert id="insert" parameterType="Post" useGeneratedKeys="true">
        insert into post(username,title,category,content,tag,create_time) values (#{username},#{title},#{category},#{content},#{tag},#{createTime})
    </insert>
    <delete id="delete" parameterType="Integer">
        delete from post where id=#{id}
    </delete>
    <select id="selectAll" resultMap="post">
        select * from post
    </select>
    <select id="selectByUsername" parameterType="String" resultMap="post">
        select * from post where username=#{username}
    </select>
    <select id="selectById" parameterType="Integer" resultMap="post">
        select p.*,c.* from post p left join comment c on p.id=c.post_id where p.id=#{id}
    </select>
</mapper>

postController

@RequestMapping(value = "/get",params = "id")
public String get(Integer id, Map<String,Post> map, Map<String, Object> commentMap, Map<String, Object> newComment){
   Post post = postBiz.getById(id);
   map.put("post",post);
   commentMap.put("comments", commentBiz.getByPid(id));
   newComment.put("comment",new Comment());
   return "postDetail";
}

postBizImpl

public Post getById(Integer id) {
    return postDao.selectById(id);
}


好帮手慕柯南 2019-11-09 11:58:26

同学你好!

同学在写sql获取username的值时将#,不小心写成了$,建议同学修改为#测试一下

http://img1.sycdn.imooc.com//climg/5dc6394d09b8e42306770107.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 hblack #1
    嗯嗯,确实是这个问题,但是现在又有了新问题。。添加评论时Expected one result (or null) to be returned by selectOne(), but found: 2,可是评论的方法定义的是list类型啊,不太清楚在哪里改,相关代码贴在楼上
    2019-11-09 15:28:28
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师