首先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值