mybatis是一个非常好用且灵活的持久层框架,但也正是因为太过灵活,导致有时候参数很难整理。我把我在项目中遇到的一个特殊情况列出来,希望下次再碰到时,也有个印象。
实体类如下:
这里的实体类中,包含了2个List对象,所以在写xml文件的时候多少对我这种菜鸟有点难度。
- <select id="findSellerBatchInfo" parameterType="com.ouyeel.chem.bdt.search.BatchManagerSerachModel" resultMap="batchInfoMap">
- select
- batch.ID,
- batch.BATCH_NAME,
- batch.BATCH_STATE batch_state,
- batch.START_TIME batch_start_time,
- batch.END_TIME batch_end_time,
- batch.IS_DIRECTIONAL_BID batch_is_direc,
- batch.BUYER_DEPOSIT batch_buyer_deposit,
- batch.BIDDING_TYPE batch_bidding_type,
- batch.OFFER_TYPE batch_offer_type,
- batch.BATCH_DATE batch_date,
- batch.ANNOUNCEMENT_DAY batch_announ,
- batch.BID_DAY batch_bid_day,
- price.id price_id,
- price.STARTING_PIRCE price_starting_price,
- price.BIDDING_GRADIENT price_bidding_gradient,
- price.TAX_RATE price_tax_rate,
- price.TAX_FREE_PRICE price_tax_free_price,
- price.RESERVE_PRICE price_reserve_price,
- item.PRODUCT_NAME item_product_name,
- item.PRODUCT_NO item_product_no,
- item.PACKAGES item_packages,
- item.ORIGIN_PLACE item_origin_place,
- item.WAREHOUSE_NAME item_warehouse,
- item.PIECE_UNIT item_piece_unit,
- item.TRADING_WEIGHT item_trading_weight
- from bdt_batch batch
- left join bdt_resources_item item on item.batch_id = batch.id
- left join bdt_price_info price on price.batch_id = batch.id and item.id = price.item_id
- where batch.member_code = #{memberCode,jdbcType=VARCHAR}<!-- and batch.id = 'P170508005' -->
- <if test="id != null and id != '' ">
- and batch.id like '%${id}%'
- </if>
- <if test="startTime != null and startTime != '' ">
- and batch.batch_date >= #{startTime,jdbcType=TIMESTAMP}
- </if>
- <if test="endTime != null and endTime != '' ">
- and batch.batch_date <= #{endTime,jdbcType=TIMESTAMP}
- </if>
- <if test="batchState != null and batchState != '' ">
- and batch.batch_state = #{batchState,jdbcType=VARCHAR}
- </if>
- <if test="sidx != null and sidx != '' ">
- order by batch.${sidx} ${sord}
- </if>
- <if test="sidx == null or sidx ==''">
- order by batch.id desc
- </if>
- </select>
- <resultMap type="com.ouyeel.chem.bdt.entity.vo.BatchManagerViewVo" id="batchInfoMap">
- <!-- association字面意思关联,这里只专门做一对一关联; property表示是com.mybatis.bean.StudentTemp中的属性名称;
- javaType表示该属性是什么类型对象 -->
- <!-- property 表示com.mybatis.bean.Class中的属性; column 表示表中的列名 -->
- <id property="id" column="id"/>
- <result property="batchName" column="batch_name"/>
- <result property="startTime" column="batch_start_time"/>
- <result property="endTime" column="batch_end_time"/>
- <result property="isDirectionalBid" column="batch_is_direc"/>
- <result property="batchState" column="batch_state"/>
- <result property="buyerDeposit" column="batch_buyer_deposit"/>
- <result property="biddingType" column="batch_bidding_type"/>
- <result property="offerType" column="batch_offer_type"/>
- <result property="batchDate" column="batch_date"/>
- <result property="announcementDay" column="batch_announ"/>
- <result property="bidDay" column="batch_bid_day"/>
- <!-- property表示集合类型属性名称,ofType表示集合中的对象是什么类型 -->
- <collection property="priceInfo" ofType="com.ouyeel.chem.bdt.entity.PriceInfoDto">
- <id property="id" column="price_id"/>
- <result property="startingPirce" column="price_starting_price"/>
- <result property="biddingGradient" column="price_bidding_gradient"/>
- <result property="taxRate" column="price_tax_rate"/>
- <result property="taxFreePrice" column="price_tax_free_price"/>
- <result property="reservePrice" column="price_reserve_price"/>
- </collection>
- <!-- property表示集合类型属性名称,ofType表示集合中的对象是什么类型 -->
- <collection property="resourceItem" ofType="com.ouyeel.chem.bdt.entity.ResourcesItemDto">
- <result property="productName" column="item_product_name"/>
- <result property="productNo" column="item_product_no"/>
- <result property="packages" column="item_packages"/>
- <result property="originPlace" column="item_origin_place"/>
- <result property="warehouseName" column="item_warehouse"/>
- <result property="pieceUnit" column="item_piece_unit"/>
- <result property="tradingWeight" column="item_trading_weight"/>
- </collection>
- </resultMap>