mybatis相关技巧

1. mybatis模糊查询新方式

<bind name="reportNumber" value="'%'+reportNumber+'%'"/>

详细栗子:

<if test="reportNumber != null and reportNumber !=''">
    <bind name="reportNumber" value="'%'+reportNumber+'%'"/>
    AND giri.report_number LIKE #{reportNumber}
</if>

或者

concat('%',#{dto.reportNumber},'%')

2. mapper.xml中大于等于新方式

<![CDATA[>=]]>
<=    &lt;=
>=    &gt;=

3.选择查询条件

<where>
<choose>
                <when test="userRoleCode == 'bxcw'">
                    <!--财务人员:单据状态为提交-->
                    AND gici.case_status = 'SUBMITTED'
                </when>
                <when test="userRoleCode == 'bxgs'">
                    <!--保险公司:单据状态为财务通过-->
                    AND gici.case_status = 'FINANCIAL_PASS'
                </when>
                <otherwise>
                    <!--业务人员:索赔状态为新建或者财务拒绝或者保险公司拒绝-->
                    AND (
                    gici.case_status = 'NEW'
                    OR gici.case_status = 'FINANCIAL_REJECT'
                    OR gici.case_status = 'INSURANCE_COM_REJECT'
                    )
                    AND (giri.contact_employee_id = #{userId} OR giri.created_by = #{userId})
                </otherwise>
            </choose>

4. mybatis方法解析

insert():有值插入,没有的值插入null
insertSelective(): 只插入有值得属性

5. mybatis一对一

association 关键字
例子:

<resultMap id="IcBusinessGroundKpi" type="com.hand.hic.gic.dto.IcBusinessGroundKpiCountDTO">
    <result property="reportTime" column="report_time"/>
    <result property="closeTime" column="close_time"/>
    <result property="paymentTime" column="payment_time"/>
    <result property="payTime" column="pay_time"/>
    <result property="writeOffTime" column="write_off_time"/>
    <association property="total" javaType="com.hand.hic.gic.dto.IcBusinessGroundKpiDTO">
        <result property="report" column="report"/>
        <result property="unSettled" column="un_settled"/>
        <result property="settled" column="settled"/>
        <result property="unRequest" column="un_request"/>
        <result property="requested" column="requested"/>
        <result property="unCompensate" column="un_compensate"/>
        <result property="compensated" column="compensated"/>
        <result property="unWriteOff" column="un_write_off"/>
        <result property="writeOn" column="write_on"/>
    </association>
</resultMap>

注意点:association标签下的属性需要写,不然无法识别

6. mybatis一对多

collection 关键字
例子:
mybatis相关技巧_第1张图片
注意点:collection 标签下的属性貌似可以不用写,待验证…

你可能感兴趣的:(mybatis)