首先感谢:
MyBatis 多对多 中间表插入数据:https://blog.csdn.net/u010857795/article/details/71512044
Mybatis中传入多个参数的问题: https://www.cnblogs.com/mingyue1818/p/3714162.html
后补:
先介绍一下要完成的任务,在我的项目中,要解决电影和标签之间的多对多关系的存储,查询,以及删除。
第一、多对多关系的存储
思路是,先分别存储好电影和标签的基本数据,然后再存储二者之间的关系。
这里遇到的一个问题就是,怎么获取刚才添加电影的主键 id,这就需要用到 useGeneratedKeys,keyProperty,keyColumn 这几个关键字。
mapper.xml
<insert id="save" parameterType="Movie" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
insert into t_movie (title,published_time,length,director,scriptwriter,performer,cover_path,
resource_path,info_intro,is_recommend,is_comment)
values (#{title},#{publishedTime},#{length},#{director},#{scriptwriter},#{performer},#{coverPath},
#{resourcePath},#{infoIntro},#{isRecommend},#{isComment})
</insert>
service.java
@Override
public void save(Movie movie) {
movieDao.save( movie );
//id是自增的,无法在mapper中直接获取id。所以在这里实现tag和movie的关联。
//在xml中设置两个属性,就可以将id封装到javabean对象的id中,所以这里就可以通过getId方法获取。
int movie_id = movie.getId();//获取刚才插入电影的Id,
//获取电影的标签列表
System.out.println("tagList"+movie.getTagList());
List<Tag> tagList = movie.getTagList();
//
for (Tag tag : tagList) {
movieDao.saveRelationOfMovieAndTag( movie_id, tag.getId() );
}
}
第二:多对多关系的查询
java文件
public class Tag {
private Integer id;
private String name;
private List<Movie> movieList;
}
public class Movie {
private Integer id;
private String title;
......
private List<Tag> tagList;
}
在 mapper.xml 中需要用到 resultMap 、collection 等关键字,然后在select语句中,不使用resultType,而是resultMap。
<!--间接映射,-->
<resultMap id="movieMap" type="Movie">
<id property="id" column="id"/> <!--主键,property对应java成员属性,column对应mysql数据表中的字段-->
<result property="title" column="title"/> <!--除了主键,其他字段全部使用result-->
<result property="publishedTime" column="published_time"/>
<result property="length" column="length"/>
<result property="director" column="director"/>
<result property="scriptwriter" column="scriptwriter"/>
<result property="performer" column="performer"/>
<result property="coverPath" column="cover_path"/>
<result property="resourcePath" column="resource_path"/>
<result property="infoIntro" column="info_intro"/>
<result property="isRecommend" column="is_recommend"/>
<result property="isComment" column="is_comment"/>
<result property="plays" column="plays"/>
<!--下面是重点-->
<!--多对一关系,用association映射-->
<association property="district" javaType="District">
<id property="id" column="did"/>
<result property="name" column="dname"/>
</association>
<!--一对多,多对多,用collection映射-->
<collection property="tagList" ofType="Tag"> <!--oftype是集合的泛型-->
<id property="id" column="tid"/>
<result property="name" column="tname"/>
</collection>
</resultMap>
<!--不使用resultType,而是resultMap-->
<select id="findAll" resultMap="movieMap" >
SELECT m.id id,title,published_time,length,director,scriptwriter,performer,cover_path,resource_path,info_intro,is_recommend,is_comment,plays,
t.id tid,t.name tname,d.id did,d.name dname
FROM t_tag t,ct_movie_tag mt,t_movie m,t_district d
where m.id = mt.movie_id
and mt.tag_id = t.id
and m.district_id = d.id
</select>
第三:多对多关系的删除
删除需要注意的是,因为有外键约束,直接删除电影是不行的,必须先删除掉与之关联的数据。
@Override
public void deleteById(int id) {
/*有外键约束,必须先删除与之关联的数据*/
movieDao.deleteRelationOfMovieAndTag( id );
movieDao.deleteById( id );
}