MyBatis 通用Sql語句

Mybatis 數據類型對應

Java 類型 jdbcType
String VARCHAR
Integer INTEGER
Date TIMESTAMP
Long BIGINT

 

通用字段,不推薦使用 SELECT * FROM

<sql id="Base_Column_List" >
    pai_id, create_date, modify_date, area_name, up_area_id, floor, last_floor, remark, 
    area_code, area_index, name_en, name_jm, post_code, district_number, sort_name, sel_tag, 
    ass_key, ass_date, area_name_sync_cus
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from tb_im_verbal_trick_info
    where id = #{id,jdbcType=VARCHAR}
  </select>

 

通用條件查詢 commonSql

<sql id="commonSql">
    <where>
    <if test='userStatus != null'> <!-- INTEGER 類型,不要添加 !='' 這樣的判斷 -->
        and user_status = #{userStatus, jdbcType=INTEGER}
    </if>
    <if test="modifyTime != null" >
        modify_time = #{modifyTime,jdbcType=TIMESTAMP}
    </if>
    <if test='userPhone != null and userPhone != ""'>  <!-- 通用模糊查詢 -->
        and user_phone like '%${userPhone}%'
    </if>
    <if test='userPhone != null and userPhone != ""'>  <!-- 更安全的通用模糊查詢 -->
        and user_phone like CONCAT('%',#{userPhone,jdbcType=VARCHAR},'%')
    </if>
    <if test='searchVal != null and searchVal != ""'>  <!-- 通用多字段模糊查詢 -->
        and ( real_name like '%${searchVal}%' or name like '%${searchVal}%' )
    </if>
    <if test='createTime!= null'> <!-- TIMESTAMP類型,不要添加 !='' 這樣的判斷 -->
        and create_time= #{createTime, jdbcType=TIMESTAMP}
    </if>
    <if test='createTimeStart != null and createTimeStart != ""'>  <!-- 通用時間範圍查詢 -->
        and DATE_FORMAT(create_time,'%Y-%m-%d') <![CDATA[ >= ]]> #{createTimeStart, jdbcType=VARCHAR}
    </if>
    <if test='createTimeEnd != null and createTimeEnd != ""'>
        and DATE_FORMAT(create_time,'%Y-%m-%d') <![CDATA[ <= ]]> #{createTimeEnd, jdbcType=VARCHAR}
    </if>
    </where>
</sql>
<select id="queryUserInfoBo" parameterType="com.juchiwang.sys.bo.ImVerbalTrickInfoBo" resultMap="BaseResultMap">
    SELECT 
        * 
    FROM 
        tb_im_info
    <include refid="commonSql"></include>
  </select>

 

 通用多表查詢 commonFromWhere

<sql id="commonFromWhere">
    FROM 
        msc_base_feedback mbf
    left JOIN msc_server_info msi ON mbf.server_id = msi.id
    left JOIN msc_business_info mbi ON mbf.business_id = mbi.business_id
</sql>

 

主鍵查詢

接口:

public UscUserInfoBo selectUserInfo(@Param("id") String id);

配置文件:

<select id="selectUserInfo" parameterType="java.lang.String" resultMap="BaseResultMap">
  	select * from usc_user_info 
  	where id = #{id}
</select>

service:

UserInfoBo info = userInfoDao.selectUserInfo(userInfoBo.getId());

 

 根據狀態統計查詢

<select id="selectTabNum" parameterType="com.juchiwang.msc.bo.MscBaseFeedbackBo" resultMap="BaseResultMap">
    SELECT
        IFNULL(SUM(1),0) all_num
        IFNULL(SUM(case when clear_state = 0 then 1 else 0 end),0) clearStateNo, <!-- 有欠款 -->
        IFNULL(SUM(case when back_state = 0 then 1 else 0 end),0) backStateNo, <!-- 未回款 -->
        IFNULL(SUM(case when back_state = 1 and clear_state = 0 then 1 else 0 end),0) backStateHalf, <!-- 部分回款 -->
    FROM 
        msc_base_feedback
  </select>

 

查詢數據是否以存在 

<select id="queryApPerformRole2Count" parameterType="com.juchiwang.bo.ApPerformRoleBo" resultType="java.lang.Integer">
    SELECT
        count(*)
    from
        ap_perform_role apr
    where
        apr.del_state = 0
</select>

 

批量操作

<update id="updatStateApPerformRoleByIds" parameterType="com.juchiwang.bo.ApPerformRoleBo">
    update ap_perform_role
    <set>
        <if test="state != null">
            state = #{state,jdbcType=INTEGER},
        </if>
    </set>
    where
        1=1
        <if test="ids != null and ids.size() > 0">
            and id in
            <foreach collection="ids" index="index" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
</update>

 

查詢表中的二級項

 查詢表中否有子機構的所有機構 ( parent_id = id )

<select id="querySubDept" parameterType="SysDeptSelectBo" resultMap="BaseResultMap">
    SELECT 
        one.id,
        one.dept_name
    FROM sys_dept one
    WHERE
        one.id in (SELECT parent_id FROM sys_dept WHERE parent_id in 
    <foreach collection="delBoIds" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
    and del_state = 0
    )
</select>

 

關聯查詢

<resultMap id="BaseResultMap" type="com.bo.ExpensesReceiptsBo" extends="com.model.dao.ExpensesReceiptsMapper.BaseResultMap">
    <!-- 根據臺賬ID查詢附件 -->
    <collection property="expensesReceiptsFileBos" column="id" select="com.dao.ExpensesReceiptsFileDao.selectExpensesReceiptsFiles"></collection>
</resultMap>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章