删除问题哪里出错了
//这是servlet的代码
public class CatalogServlet extends HttpServlet {
private CatalogDAO catalogDAO = new CatalogDAO();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getServletPath().indexOf("/catalog/add.do")!=-1){
CatalogEntity catalog = CatalogDAOUtils.catalogEntity(req,resp);
catalogDAO.addCatalog(catalog);
resp.sendRedirect("/catalog/list.do");
} else if (req.getServletPath().indexOf("/catalog/delete.do") != -1 ){
String sid = req.getParameter("id");
Integer id = Integer.valueOf(sid);
catalogDAO.deletCatalog(id);
resp.sendRedirect("/catalog/list.do");
} else if (req.getServletPath().indexOf("/catalog/list.do") != -1){
CatalogEntity catalog = new CatalogEntity();
catalog.setId(10000);
CatalogEntity catalogEntity = catalogDAO.getCatalogEntity(catalog);
catalog = catalogDAO.getCatalogEntity(catalog);
req.setAttribute("catalog",catalog);
req.getRequestDispatcher("/WEB-INF/pages/catalog/catalog_list.jsp").forward(req,resp);
}
}
}
//DAO类的代码
public class CatalogDAO {
private SqlSession sqlSession ;
private CatalogEntity catalogEntity ;
private SqlSession getSqlSession (){
sqlSession = SqlSessionFactoryutils.getSqlSessionFactory().openSession();
return sqlSession;
}
public CatalogEntity getCatalogEntity(CatalogEntity catalog ){
try{
catalogEntity = getSqlSession().selectOne("selectCatalog",catalog) ;
} catch (Exception e){
e.printStackTrace();
} finally {
sqlSession.close();
}
return catalogEntity;
}
public void deletCatalog(Integer id){
try{
sqlSession = getSqlSession();
sqlSession.delete("deleteCatalog",id);
} catch (Exception e ){
e.printStackTrace();
} finally {
sqlSession.close();
}
}
public void addCatalog(CatalogEntity catalog){
try{
getSqlSession().insert("addCatalog",catalog);
} catch (Exception e){
e.printStackTrace();
} finally {
sqlSession.close();
}
}
}
//数据库mapper
<mapper namespace="com.imooc.entity.CatalogEntity">
<delete id="deleteCatalog" >
DELETE FROM catalog WHERE id = #{id}
</delete>
<insert id="addCatalog" useGeneratedKeys="true" keyProperty="id">
INSERT INTO catalog (title,pid,info)
VALUES (#{title},#{pid},#{info})
</insert>
<select id="selectCatalog" resultMap="forCatalog">
SELECT * FROM catalog WHERE id = #{id}
</select>
<select id="selectByPid" resultMap="forCatalog">
SELECT * FROM catalog WHERE pid = #{id}
</select>
<resultMap id="forCatalog" type="com.imooc.entity.CatalogEntity">
<result column="id" property="id" ></result>
<result column="title" property="title"></result>
<result column="pid" property="pid"></result>
<result column="info" property="info"></result>
<collection property="children" column="id" select="selectByPid"></collection>
</resultMap>
</mapper>
点击删除链接的时候要删除的catalog的id能跟着request传递,但是就是不能在数据库中删除3
收起
正在回答 回答被采纳积分+1
3回答
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程



恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星