mybatis一对多增、删、改

1.Service代码:

<span style="font-family:Microsoft YaHei;">public interface CommentService {
	
	/**
	 * 添加点评
	 * @param comment
	 * @return
	 * @throws Exception
	 */
	public Comment insertComment(Comment comment,List<String> atts) throws Exception;
	
	/**
	 * 删除点评(根据id来删除已经发表的点评)
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean deleteCommentById(String id) throws Exception;
	
	/**
	 * 修改点评
	 * @param comment
	 * @return
	 * @throws Exception
	 */
	public Comment updateComment(Comment comment,List<String> atts) throws Exception;	
}</span>

2.Service里面的Impl:

<span style="font-family:Microsoft YaHei;">@Service
public class CommentServiceImpl implements CommentService{

	private static final Logger logger = LoggerFactory.getLogger(ClickZanServiceImpl.class); 
	
	@Autowired
	private CommentMapper commentMapper;//点评Mapper
	
	@Autowired
	private CommentAttachmentMapper commentAttachmentMapper;//附件Mapper
	
	/**
	 * 添加点评
	 */
	public Comment insertComment(Comment comment,List<String>  attachments) throws Exception {
		String id = UUIDUtil.generateKey();
		comment.setCreatedate(BaseUtil.timeToString(new Date()));
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String imageupdatename = sdf.format(date);
		try {
			if(attachments==null){
				comment.setId(id) ;
				commentMapper.insert(comment);
			}else{
				comment.setId(id) ;
				CommentAttachment com = new CommentAttachment();
				commentMapper.insert(comment);
				for (String string : attachments) {
					com.setCommentid(id);
					com.setCreatedate(BaseUtil.ti
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis是一款优秀的ORM框架,可以方便地实现数据库的操作。下面是一对多增删查的示例: 一对多关系指的是一个实体对象关联多个子实体对象,比如一个部门对应多个员工。在这种情况下,通常会使用两个表来表示这种关系,一个主表和一个从表,主表包含一条记录,从表包含多条记录。 一对多查询: ```xml <!--部门表--> <resultMap id="departmentMap" type="Department"> <id property="id" column="id" /> <result property="name" column="name" /> <collection property="employees" ofType="Employee"> <id property="id" column="employee_id" /> <result property="name" column="name" /> <result property="age" column="age" /> </collection> </resultMap> <select id="getDepartment" resultMap="departmentMap"> SELECT d.id, d.name, e.id as employee_id, e.name, e.age FROM department d LEFT JOIN employee e ON d.id = e.department_id WHERE d.id = #{id} </select> ``` 一对多: ```xml <!--部门表--> <insert id="insertDepartment" parameterType="Department"> INSERT INTO department (name) VALUES (#{name}) </insert> <!--员工表--> <insert id="insertEmployee" parameterType="Employee"> INSERT INTO employee (name, age, department_id) VALUES (#{name}, #{age}, #{department.id}) </insert> ``` 一对多修改: ```xml <!--部门表--> <update id="updateDepartment" parameterType="Department"> UPDATE department SET name = #{name} WHERE id = #{id} </update> <!--员工表--> <update id="updateEmployee" parameterType="Employee"> UPDATE employee SET name = #{name}, age = #{age}, department_id = #{department.id} WHERE id = #{id} </update> ``` 一对多除: ```xml <!--部门表--> <delete id="deleteDepartment" parameterType="int"> DELETE FROM department WHERE id = #{id} </delete> <!--员工表--> <delete id="deleteEmployee" parameterType="int"> DELETE FROM employee WHERE id = #{id} </delete> ``` 以上是一对多关系的增删查示例,具体实现还要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值