mapper接口参数为map的情况
首先mapper语句的话存在以下情况
1、单个参数,那么直接使用mybatis语法即可。
2、多个参数:
2.1 多个参数的情况下,如果类型相同的话,比如都是String类型,可以直接使用mybaits的parameterType=“String”
2.1 多个参数,但是类型不相同,比如void add(Integer factoryStatus,List<Long> ids)这种情况的话参数可以用Map进行封装。
如上面的需求,更新某个id的生产商状态可以直接传入上面2个参数。
但是如果遇到多个条件查询的话,比如有多个框框,包括人员编号、卡号、卡片类型、考勤类型等等,条件可以为空或者不为空,这种情况可以创建一个类将查询条件封装到这个类中,或者参数为查询条件都可以。
如下实体类代码:
@Data public class ProductDto { @ApiModelProperty(value = "商品名称") private String productName; @ApiModelProperty(value = "商品分类id") private Long productCategoryId; @ApiModelProperty(value = "品牌id") private Long brandId; @ApiModelProperty(value = "货号") private String productSn; @ApiModelProperty(value = "上架状态:0->下架;1->上架") private Integer publishStatus; @ApiModelProperty(value = "审核状态:0->未审核;1->审核通过") private Integer verifyStatus; }
接口的代码
public List<Product> listByKeyWord(Integer pageNum, Integer pageSize, ProductDto productDto) { PageHelper.startPage(pageNum, pageSize); Map<String, Object> map = new HashMap<>(16); map.put("ProductName", productDto.getProductName()); map.put("ProductSn", productDto.getProductSn()); map.put("ProductCategoryId", productDto.getProductCategoryId()); map.put("BrandId", productDto.getBrandId()); map.put("PublishStatus", productDto.getPublishStatus()); map.put("VerifyStatus", productDto.getVerifyStatus()); return productMapper.listByKeyWord(map); }
为了和实体类的属性区分,map的key这里设置成大写。
Mapper类代码:
List<Product> listByKeyWord(Map<String, Object> map);
Mapper.xml代码
<select id="listByKeyWord" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from pms_product <where> delete_status=0 <if test="ProductName!=null and ProductName!=''"> and name like concat('%',#{ProductName},'%') </if> <if test="ProductSn!=null and ProductSn!=''"> and product_sn like concat('%',#{ProductSn},'%') </if> <if test="ProductCategoryId!=null and ProductCategoryId!=''"> and product_category_id=#{ProductCategoryId} </if> <if test="BrandId!=null and BrandId!=''"> and brand_id=#{BrandId} </if> <if test="PublishStatus!=null and PublishStatus!=''"> and publish_status=#{PublishStatus} </if> <if test="VerifyStatus!=null and VerifyStatus!=''"> and verify_status=#{VerifyStatus} </if> </where> </select>
*** 这里的话 <if test> 里面是map的key值
上面的代码还可以改造成不用Map格式接收,这种方式会比较简单点。
Mapper文件代码:
List<Order> listByCondition(@Param("orderDto") OrderDto orderDto);
Mapper.xml代码:
<select id="listByCondition" parameterType="com.cn.dto.OrderDto" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List"/> FROM oms_order <where> delete_status=0 <if test="orderDto.orderSn!=null and orderDto.orderSn!=''"> AND order_sn LIKE CONCAT('%',#{orderDto.orderSn},'%') </if> <if test="orderDto.receiverNameOrPhone!=null and orderDto.receiverNameOrPhone!=''"> AND ( receiver_name LIKE CONCAT('%',#{orderDto.receiverNameOrPhone},'%') OR receiver_phone LIKE CONCAT('%',#{orderDto.receiverNameOrPhone},'%') ) </if> <if test="orderDto.createTime!=null and orderDto.createTime!=''"> AND create_time LIKE CONCAT('%',#{orderDto.createTime},'%') </if> <if test="orderDto.status!=null and orderDto.status!=''"> <!--这里的status可能是mysql特有的字段,因此为了区分,需要标明出来--> AND 'status'=#{orderDto.status} </if> <if test="orderDto.orderType!=null and orderDto.orderType!=''"> AND order_type=#{orderDto.orderType} </if> <if test="orderDto.sourceType!=null and orderDto.sourceType!=''"> AND source_type=#{orderDto.sourceType} </if> </where> </select>
3、传入的参数为List的情况
代码不做任何变动
int deleteBatch(List<Long> ids);
<update id="deleteBatch" parameterType="long"> UPDATE oms_order SET delete_status=1 WHERE id IN <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> </update>
这里的collection就是list,记得和上面的例子区分开来
mybatis.xml中标签
其中<insert>标签的话和其他语句一样都有类似id的属性,但是他有特殊的useGeneratedKey=“true” keyProperty="id" ** 这个的意思是表示如果数据库中id字段设定成自增的话,那么这里的话调用这个sql语句的话,插入的信息id也会进行自增。
如果不加的话获取不到插入的这条记录ID值