如下图所示,mybatis删除数据异常:
log4j调试的信息表示SQL语句应该是对的,然而数据库中并没有删除信息。
相关的配置文件及代码如下:
配置文件:
<?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"> <!-- 命名空间不能省略,类似于package -->
<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);//message.xml配置文件中对应ID号的SQL语句
}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();
}
}
求各路大神解答,谢谢!