前言:侧重于MyBatis的使用
一、myBatis一对一和对多使用
1、xml文件,巧用映射进行操作
<!--定义映射,这里包含一对多和一对一个查询-->
<resultMap id="MyMovieVo" type="com.xusj.cinema.vo.MyMovieVo">
<result property="movieId" column="movie_id"/>
<result property="movieName" column="movie_name"/>
<result property="movieReleaseTime" column="movie_release_time"/>
<result property="movieArea" column="movie_area"/>
<result property="movieInfo" column="movie_info"/>
<result property="movieVersion" column="movie_version"/>
<result property="movieImg" column="movie_img"/>
<result property="movieEname" column="movie_ename"/>
<result property="movieScore" column="movie_score"/>
<result property="movieStatus" column="movie_status"/>
<result property="movieCount" column="movie_count"/>
<result property="movieBoxOffice" column="movie_box_office"/>
<result property="movieTime" column="movie_time"/>
<result property="movieWantCount" column="movie_want_count"/>
<result property="movieReleaseYear" column="movie_release_year"/>
<result property="commentUserCount" column="comment_user_count"/>
<!--一对一标签:理解:将column="movie_area",当作select=这个语句中的参数,然后查询结果赋值给property-->
<association property="movieAreaStr" column="movie_area"
select="com.xusj.cinema.mapper.MovieMapper.selectById"/>
<!--一对多标签:理解同上-->
<collection property="movieImageList" column="movie_id"
select="com.xusj.cinema.mapper.MovieMapper.selectImgList"/>
<!--select="com.xusj.cinema.mapper.MovieMapper.selectTypeList",这个里面是关联查询,不要思维定式-->
<collection property="movieTypeList" ofType="com.xusj.cinema.domain.MovieType" column="movie_id"
select="com.xusj.cinema.mapper.MovieMapper.selectTypeList"/>
</resultMap>
<!--定义sql片段-->
<sql id="selectMovieVo">
select movie_id,
movie_name,
movie_release_time,
movie_area,
movie_info,
movie_version,
movie_img,
movie_ename,
movie_score,
movie_status,
movie_count,
movie_box_office,
movie_time,
movie_want_count,
movie_release_year,
comment_user_count
from tb_movie
</sql>
<!--返回映射结果,在里面完成一对一和一对多的查询/*使用sql片段*/-->
<select id="selectMovieToEs" resultMap="MyMovieVo">
<include refid="selectMovieVo"/>
</select>
当你的mapper成有多个参数的时候,{}使用大括号给不同的参数赋值
2、mapper层(每个都通过xml中select=”com.xusj.cinema.mapper.MovieMapper.selectTypeList”标签找到对应的mapper进行查询)
/***
* 查询对应的es中的数据
*
* @return {@link MyMovieVo}
* @author xusj
* <br>CreateDate 2022/5/14 15:19
*/
List<MyMovieVo> selectMovieToEs();
/***
* 根据地址查看对应的地区名称
*
* @param id
* @return {@link String}
* @author xusj
* <br>CreateDate 2022/5/14 15:24
*/
String selectById(int id);
List<MovieImage> selectImgList(int id);
List<MovieType> selectTypeList(int id);
===》更新2022.05.15
时间范围
<if test="startTime!=null">
and DATE_FORMAT(a.create_date,'%Y-%m-%d %H:%i:%s') <![CDATA[ >= ]]>DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i:%s')
</if>
<if test="endTime!=null">
and DATE_FORMAT(a.create_date,'%Y-%m-%d %H:%i:%s') <![CDATA[ <= ]]>DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i:%s')
</if>
// 一对多对条件mybatis 标签传参
<resultMap id="selectStoreAndChannelMap" type="com.xiangzheng.gmcserver.vo.StoreAndChannelVO">
<result property="channelCode" column="channelCode"/>
<result property="sysId" column="sysId"/>
// 主要是这里{使用这个}
<collection property="storeInfoList" column="{channelCode=channelCode,sysId=sysId}"
select="com.xiangzheng.gmcserver.mapper.StoresMapper.selectStoreInfo"/>
</resultMap>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/96258.html