亚努人
2016-09-19 23:01
如下图所示,单条信息无法删除,出现下面异常,但是跟着老师的代码走的,请问是什么原因?

老师特别提醒,手动提交sqlSession.commit()
是因为没有手动提交事务,即要加入语句:sqlSession.commit();

配置文件
<?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="Message">
<resultMap type="model.Message" id="MessageResult">
<id column="ID" jdbcType="VARCHAR" property="id"/> <!-- id是查找结果集的主键 -->
<result column="COMMAND" jdbcType="VARCHAR" property="command"/>
<result column="DESCRIPTION" jdbcType="VARCHAR" property="description"/>
<result column="CONTENT" jdbcType="VARCHAR" property="content"/>
</resultMap>
<select id="Message.queryAll" resultMap="MessageResult">
SELECT ID,COMMAND,DESCRIPTION,CONTENT FROM message WHERE 1=1
<if test="command != null and !"".equals(command.trim())">and COMMAND LIKE '%' #{command} '%'</if>
<if test="description != null and !"".equals(description.trim())">and DESCRIPTION LIKE '%' #{description} '%'</if>
</select>
<delete id="deleteOne" parameterType="java.lang.Integer">
DELETE FROM message WHERE ID=#{_parameter}
</delete>
</mapper>DAO层的方法
public void deleteOne(int id){
DBAccess dbAccess = new DBAccess();
SqlSession sqlSession = null;
try{
sqlSession = dbAccess.getSqlSession();//实例化并打开数据库会话
sqlSession.delete("Message.deleteOne",id);
}catch(IOException e){
e.printStackTrace();
}finally{
if(sqlSession!=null){
sqlSession.close();
}
}
}Service层的方法
package service;
import dao.MessageDAO;
public class MaintainService {
public void deleteOne(String id){
if(id != null && !"".equals(id.trim())){
MessageDAO messageDao = new MessageDAO();
messageDao.deleteOne(Integer.valueOf(id));
}
}
}Servlet方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//获取页面的值
String id = request.getParameter("id");
MaintainService maintainService = new MaintainService();
maintainService.deleteOne(id);
//dao方法和跳转页面
try {
request.getRequestDispatcher("/servlet/ShowServlet").forward(request,response);
} catch (Exception e) {
e.printStackTrace();
}
}model层的方法
package model;
public class Message {
private int id;
private String command;
private String description;
private String content;
public Message(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}麻烦帮忙看下,到底是哪里有问题?
把后面的截图发下,还有这个你mybatis里配置有没有问题?
通过自动回复机器人学Mybatis---基础版
107417 学习 · 831 问题
相似问题