单条信息无法删除

来源:4-4 实现单条信息删除

亚努人

2016-09-19 23:01

如下图所示,单条信息无法删除,出现下面异常,但是跟着老师的代码走的,请问是什么原因?

http://img.mukewang.com/57dffd410001e0b412430430.jpg

写回答 关注

4回答

  • Uauslman
    2016-09-23 17:08:19
    已采纳

    老师特别提醒,手动提交sqlSession.commit()

    亚努人

    是的!

    2016-09-30 23:18:43

    共 1 条回复 >

  • 亚努人
    2016-09-21 21:33:08

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

  • 亚努人
    2016-09-20 21:15:59

    http://img.mukewang.com/57e1353a000124b510820518.jpg

    配置文件
    <?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 !&quot;&quot;.equals(command.trim())">and COMMAND LIKE '%' #{command} '%'</if>
        <if test="description != null and !&quot;&quot;.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;
    	}
    	
    }

    麻烦帮忙看下,到底是哪里有问题?

  • 慕斯卡7735277
    2016-09-20 10:52:53

    把后面的截图发下,还有这个你mybatis里配置有没有问题?

    亚努人

    相关的截图和代码如下所示:

    2016-09-20 21:08:48

    共 1 条回复 >

通过自动回复机器人学Mybatis---基础版

微信公众号自动回复功能学习Mybatis,基础教程加案例实战方式学习

107410 学习 · 786 问题

查看课程

相似问题