添加评论500报错
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>
12
收起
正在回答 回答被采纳积分+1
5回答
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; } }
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); }
相似问题
登录后可查看更多问答,登录/注册
4. SSM到Spring Boot入门与综合实战
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星