Mapper文件的sql语句
springboot集合mybatis https://www.jianshu.com/p/34bfce24b115
https://www.cnblogs.com/sh086/p/8375791.html 动态标签
所有获取属性的都以#开头 如#{name}
简单版多表关联查询
实体类定义如下:
public class Question {
private String id; //ID
private String content; //问题
private String type; //问题类型 1:单选,2:多选,3:问答
private Integer sort; //排序
private List<QuestionOption> optionsList; //问题选项
}
public interface QuestionMapper extends BaseMapper<Question> {
List<Question> list();
常见:
<resultMap id="BaseResultMapCity" type="com.vinsuan.parking.platform.Question">
<id column="id" property="id" jdbcType="INTEGER"/>
<collection property="optionList" ofType="com.vinsuan.parking.platform.QuestionOption"
select="com.vinsuan.parking.platform.mapper.QuestionMapper.list">
<id column="id" property="id"/>
<result column="name" property="name"/>
</collection>
</resultMap>
<collection> 中的property比如private List<CityVo> cityList 那么property就是cityList,
ofType就是返回类的路径名,
column是getByProvinceId中的参数对应的列名,
select就是对应mapper中方法的路径,如果这个方法在同个表中,只需要写方法,不需要路径。
<collection>:多用于集合和数组中。
resultType:返回值类型查询的数据如果是数据库中有的了字段,比如City
resultMap:查询的数据在数据库中没有,需要自己定义,比如CityVo
public int updateDelStatus(List<Long> ids, Integer delStatus) throws BusinessException {
Map<String, Object> map = new HashMap<>(16);
map.put("Ids", ids);
map.put("DelStatus", delStatus);
int count = productMapper.updateDelStatus(map);
if (count < 1) {
throw new BusinessException(ResultCode.PRODUCT_UPDATE_FAILED.getCode(), ResultCode.PRODUCT_UPDATE_FAILED.getMessage());
}
return count;
}
int updateDelStatus(Map<String, Object> map);
<update id="updateDelStatus" parameterType="map">
update pms_product
set delete_status=#{DelStatus}
where id in
<foreach collection="Ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</update>
** 这里mapper的参数为map,foreach里面的collection为代码中的key,这里为Ids
究极复杂版多表关联查询
实体类如下:
@Data
public class Procut2 {
@TableId(type = IdType.AUTO)
private Long id;
@ApiModelProperty(value = "商品名称")
private String name;
@ApiModelProperty(value = "副标题")
private String subTitle;
@ApiModelProperty("商品阶梯价格设置")
private List<ProductLadder> productLadderList;
@ApiModelProperty("优选专区和商品的关系")
private List<PrefrenceAreaProductRelation> prefrenceAreaProductRelationList;
@ApiModelProperty("商品所选分类的父id")
private Long productCategoryParentId;
}
上面代码中一个商品只有1个阶梯价格,但是可以有多个优选专区和商品的关系,要求当商品id等于X的时候查询出该商品的信息。
Product2Mapper类
public interface ProductMapper {
Product2 getUpdateInfoById(Long id);
}
Product2Mapper.xml文件:
<resultMap id="updateInfoMap" type="com.cn.vo.pms.ProductVo" extends="BaseResultMap">
<result column="productCategoryParentId" property="productCategoryParentId" jdbcType="BIGINT"/>
<collection property="productLadderList" ofType="com.cn.pojo.pms.ProductLadder"
resultMap="com.cn.mapper.pms.ProductLadderMapper.BaseResultMap"/>
<collection property="prefrenceAreaProductRelationList" column="{productId=id}"
select="selectPrefrenceAreaProductRelationByProductId"/>
</resultMap>
<select id="selectPrefrenceAreaProductRelationByProductId"
resultMap="com.cn.mapper.cms.PrefrenceAreaProductRelationMapper.BaseResultMap">
select *
from cms_prefrence_area_product_relation
where product_id = #{productId}
</select>
<select id="getUpdateInfoById" resultMap="updateInfoMap">
SELECT *,
pc.parent_id productCategoryParentId,
l.id ladder_id,
l.product_id ladder_product_id,
l.discount ladder_discount,
l.count ladder_count,
l.price ladder_price,
FROM pms_product p
LEFT JOIN pms_product_category pc on pc.id = p.product_category_id
LEFT JOIN pms_product_ladder l ON p.id = l.product_id
WHERE p.id = #{id}
</select>
分下下xml文件的sql
<resultMap>这里的话extends Product2Mapper中的BaseResultMap就能查出id、name、subtitle这些信息,
其次 productCategoryParentId这个属性的话是不存在Product2这张表中的,这个名字是用户可以进行自定义的,因此colums额外定义出来
第一个collection的话就是简单的关联查询,即查询出来的话只有1条信息
第二个collection的话 column可以传入参数值,column={productId=id},其中productId属于这个collection中的select方法即selectPrefrenceAreaProductRelationByProductId里面的参数,
而id的话则属于接收这个resultMap方法即getUpdateInfoById里面的参数 查询出来后可以有多条信息
查询出来的结果如下:
{
"code": "200",
"message": "操作成功",
"data": {
"id": 23,
"name": "毛衫测试",
"subTitle": "毛衫测试11",
"productLadderList": [
{
"id": 23,
"productId": 23,
"count": 0,
"discount": 0.00,
"price": 99.00
}
],
"prefrenceAreaProductRelationList": [
{
"id": 24,
"prefrenceAreaId": 1,
"productId": 23
},
{
"id": 25,
"prefrenceAreaId": 2,
"productId": 23
}
],
"productCategoryParentId": 2
}
}
动态sql标签:
if,choose (when, otherwise),trim (where, set),foreach
if语句和where、set常结合使用,因此不演示
if标签如果有多个语句需要加上逗号
where:查询使用
<select id="selectUserByUsernameAndSex" resultType="user" parameterType="com.ys.po.User">
select * from user
<where>
<if test="userName != null and userName!=''"> test后面是类的字段属性
username=#{userName} 列名=#{字段属性}
</if>
<if test="passWord != null and passWord !=''">
and pass_word=#{passWord} 第一个if不用and,后面的都需要加and
</if>
</where>
</select>
set和where一样,就是把<where>换成<set>,也需要<if>条件。
choose:进行查询,只要有一个条件满足即可,使用 choose 标签可以解决此类问题,
和<when>、<otherwise>一起使用,也就是说<when>当XX,如果满足就进行,<otherwise>否则的话XXX
<select id="selectUserByChoose" resultType="com.ys.po.User" parameterType="com.ys.po.User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="userName !='' and userName != null">
and username=#{userName}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
foreach:用来查询列表中比如1至3的信息
<select id="queryShopCategoryByIds" resultType="com.cn.pojo.ShopCategory">
SELECT
shop_category_id,
shop_category_name,
shop_category_desc,
shop_category_img,
priority,
create_time,
last_edit_time,
parent_id
FROM
tb_shop_category
WHERE shop_category_id IN
<foreach collection="CategoryList" item="shopCategoryId" open="("
这里的collection为CategoryList的原因是因为在mapper文件中:List<Area> findUserListByIdList(@param("CategoryList") List<Long> idList),
因为加了@param属性,所以才能生效,因此强制以后每个mapper文件都加上@param
item的话就是对应要查询的条件, open、separator、close固定写法。
separator="," close=")"> #{shopCategoryId} </foreach>

浙公网安备 33010602011771号