mybatis一对多删除问题

sakura_樱 2016-12-01 04:58:16
本人是初学者,遇到点问题,请各位大神帮忙看看,谢谢

不全的地方,是我省略了。
/**
* 指令表对应实体类
*
*/
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;
}


我想实现删除指令的时候同时把指令对应的内容全部删除掉,不过不知道怎么实现!


...全文
506 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
街头小贩 2016-12-04
  • 打赏
  • 举报
回复
小可,mybatis也只是能用,懂的不多哈。我建议 1.把所有相关需要删除的表写在一个方法中。作事务控制一下 2.一个表一个删除方法,用事务控制器保证事务完整性 最后: 1.要先删除从表,再删除主表 2.小可觉得mybatis是一个偏查询的方案,不属于纯o/r方案。jpa中只需要执行一次(delete 主表类),jpa会在背后帮你删除相关的关记录(当然是需要配一下级联操作)
街头小贩 2016-12-02
  • 打赏
  • 举报
回复
表结构中作了外键关联吗?
sakura_樱 2016-12-02
  • 打赏
  • 举报
回复
做了外键关联,我今天试了一下,我是这样写的

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中删除表中的信息是不是这样实现的,求指教

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧