public interface VideoMapper {
Video selectById(@Param("video_id") int videoId);//结合XML配置文件
List selectListByXML();
@Select("select * from video")//注解配置
List selectList();
}
resources下的mapper
2.2.进阶-增删改查
2.2.1.多参数模糊查询
查询指定评分,标题包含指定字符的视频
dao层
List selectByPointAndTitleLike(@Param("point") double point, @Param("title") String title);
resources下的mapper
select * from video where point=#{point} and title like concat('%', #{title},'%')
Video video = new Video();
video.setTitle("小滴课堂面试专题900道");
video.setCoverImg("xdclass.net/aaa.png");
video.setPoint(9.4);
video.setCreateTime(new Date());
video.setPrice(9900);
video.setSummary("这个是面试专题概要");
int rows = videoMapper.add(video);
List list = new ArrayList<>();
list.add(video1);
list.add(video2);
int rows = videoMapper.addBatch(list);
2.2.3.更新
update video
set
title = #{title,jdbcType=VARCHAR},
summary = #{summary,jdbcType=VARCHAR},
cover_img = #{coverImg,jdbcType=VARCHAR},
price = #{price,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
point = #{point,jdbcType=DOUBLE}
where
id = #{id}
Video video = new Video();
video.setId(59);
video.setTitle("小滴课堂面试专题900道 2021年新版");
video.setCoverImg("xdclass.net/6666.png");
videoMapper.updateVideo(video);
选择性更新
利用Mybatis里面的 if test 语法
update video
title = #{title,jdbcType=VARCHAR}, summary = #{summary,jdbcType=VARCHAR}, cover_img = #{coverImg,jdbcType=VARCHAR}, price = #{price,jdbcType=INTEGER}, create_time = #{createTime,jdbcType=TIMESTAMP}, point = #{point,jdbcType=DOUBLE},
where
id = #{id}
2.2.4.删除
delete from video where create_time ]]> #{createTime} and price = ]]> #{price}
Map map = new HashMap<>();
map.put("createTime","2021-01-11 09:33:20");
map.put("price",9000);
int rows = videoMapper.deleteByCreateTimeAndPrice(map);
总结: 1.为了避免与标签的< >混淆,大于小于这样表示
大于等于 = ]]>
小于等于
2.parameterType指的是接收的数据类型(能够增删改得)
2.3.配置mybatis-config.xml
2.3.1.起个别名呗
配置修改后,resources > mapper下则可以简化
select * from video where id = #{video_id,jdbcType=INTEGER}
2.3.2.sql片段
查询指定值,并不是用select * 把所有字段全部查出
id,title,summary,cover_img
select from video where id = #{video_id,jdbcType=INTEGER}