10,606
社区成员
发帖
与我相关
我的任务
分享
/**
* 指令表对应实体类
*
*/
public class Command {
private String id;
private String cname;
private String description;
//一条指令对应的自动回复内容列表
private List<CommandContent> contentList;
public Command(String id, String cname, String description) {
super();
this.id = id;
this.cname = cname;
this.description = description;
}
/**
* 内容表对应实体类
*
*/
public class CommandContent {
private String id;
private String content;
private String command_id;
public CommandContent(String id, String content, String command_id) {
super();
this.id = id;
this.content = content;
this.command_id = command_id;
}
package com.company.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.company.service.MaintainService;
public class DeleteOneServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
//接收页面的传值
String id=req.getParameter("id");
MaintainService service=new MaintainService();
int flag=service.deleteOneCommand(id);
req.setAttribute("flag","删除成功!");
req.getRequestDispatcher("/List.action").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(req, resp);
}
}
MaintainService类里边定义了一个方法,用来调用dao方法删除删除
/**
* 删除一条指令
* @param id
*/
public int deleteOneCommand(String id){
CommandDao dao=new CommandDao();
//先查询根据这个id查询子表中的数据,这个id就是外键
List<CommandContent> commandContents=new CommandDao().queryContentByCommandId(id);
if(commandContents.size()>0){
//如果子表中有这个id对应的数据,那么先删除子表中的数据
dao.deleteContentByCommandId(id);
}
//然后删除主表信息
int flag=dao.deleteOneCommand(id);
return flag;
}
dao方法如下
/**
* 删除一条指定
*
* @param id
*/
public int deleteOneCommand(String id) {
SqlSession session = null;
int flag = -1;
try {
session = new ConnectionDB().getSqlSession();
flag = session.delete("Command.deleteOneCommand", id);
session.commit();
return flag;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
/**
* 根据外键删除内容
*
* @param id
*/
public void deleteContentByCommandId(String id) {
SqlSession session = null;
try {
session = new ConnectionDB().getSqlSession();
session.delete("CommandContent.deleteByCommandId", id);
session.commit();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 根据指令id查找到指令多有内容
* @param id
* @return
*/
public List<CommandContent> queryContentByCommandId(String id) {
List<CommandContent> commandContents = new ArrayList<CommandContent>();
SqlSession session = null;
try {
session = new ConnectionDB().getSqlSession();
commandContents=session.selectList("CommandContent.selectByCommandId", id);
} catch (IOException e) {
e.printStackTrace();
}
return commandContents;
}
我这个方法删除成功了,就是不知道对不对,我没有做过,我不知道mybatis中删除表中的信息是不是这样实现的,求指教