Mybatis日常使用

liuqiang 2021年01月26日 12次浏览

Mybatis xml sql 调用静态方法:

使用${}取值,里面类名前加@,方法名前也加@

生成 9 位的随机数

SELECT CEILING(RAND()*900000000+100000000);

xml 复杂映射

<!-- 非常复杂的结果映射 -->
<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>

判断

    <select id="getTrustBaseInfoPageList" 
 resultType="com.ct.wms.business.projectinfo.entity.TrustBaseInfo"
    parameterType="com.ct.wms.business.projectinfo.param.TrustBaseInfoQueryParam">
        select
        <include refid="Base_Column_List"/>
        from t_trust_base_info
        <where>
            <if test="param.trustPrjCode!=null and param.trustPrjCode!=''">
                and trust_prj_code=#{param.trustPrjCode}
            </if>
            <if test="param.trustPrjName!=null and param.trustPrjName!=''">
                and trust_prj_name like CONCAT('%',#{param.trustPrjName},'%')
            </if>
            <if test="param.tmCode!=null and param.tmCode!=''">
                and tm_code like CONCAT('%',#{param.tmCode},'%')
            </if>
            <if test="param.cmCode!=null and param.cmCode!=''">
                and cm_code like CONCAT('%',#{param.cmCode},'%')
            </if>
            <if test="param.statusList!=null">
                and status in
                <foreach collection="param.statusList" index="index" item="status" open="(" separator="," close=")">
                    #{status}
                </foreach>

            </if>
            <if test="param.trustStdDt!=null and param.trustStdDt!=''">
                and trust_std_dt >=#{param.trustStdDt}
            </if>
            <if test="param.trustEndDt!=null and param.trustEndDt!=''">
                <![CDATA[  and trust_std_dt<=#{param.trustEndDt} ]]>
            </if>
        </where>
    </select>