課程
/后端開發(fā)
/Java
/通過自動(dòng)回復(fù)機(jī)器人學(xué)Mybatis---基礎(chǔ)版
如下圖所示,單條信息無法刪除,出現(xiàn)下面異常,但是跟著老師的代碼走的,請(qǐng)問是什么原因?
2016-09-19
源自:通過自動(dòng)回復(fù)機(jī)器人學(xué)Mybatis---基礎(chǔ)版 4-4
正在回答
老師特別提醒,手動(dòng)提交sqlSession.commit()
亞努人 提問者
是因?yàn)闆]有手動(dòng)提交事務(wù),即要加入語句: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是查找結(jié)果集的主鍵?--> ????<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();//實(shí)例化并打開數(shù)據(jù)庫會(huì)話 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方法和跳轉(zhuǎn)頁面 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; } }
麻煩幫忙看下,到底是哪里有問題?
把后面的截圖發(fā)下,還有這個(gè)你mybatis里配置有沒有問題?
舉報(bào)
微信公眾號(hào)自動(dòng)回復(fù)功能學(xué)習(xí)Mybatis,基礎(chǔ)教程加案例實(shí)戰(zhàn)方式學(xué)習(xí)
1 回答刪除單條記錄出錯(cuò)?。?!
2 回答刪除信息報(bào)500錯(cuò)誤
3 回答單條刪除,傳值正常,就是刪除不了
2 回答如何刪除關(guān)聯(lián)子表里的信息
2 回答無法批量刪除,求詳細(xì)解決辦法。
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-09-23
老師特別提醒,手動(dòng)提交sqlSession.commit()
2016-09-21
是因?yàn)闆]有手動(dòng)提交事務(wù),即要加入語句:sqlSession.commit();
2016-09-20
麻煩幫忙看下,到底是哪里有問題?
2016-09-20
把后面的截圖發(fā)下,還有這個(gè)你mybatis里配置有沒有問題?