mybatis常用技巧

「爱情、让人受尽委屈。」 2022-10-11 00:51 383阅读 0赞

mybatis常用技巧

  • list的for循环
  • 分页
  • insert
  • update
  • #{} 预处理
  • ${} 字符串替换
  • 批量提交
  • mybatis plus实用工具类
    • TableInfoHelper
  • mapper.xml文件常用标签
    • select标签
    • insert标签
    • update标签
    • delete标签
    • if标签
    • choose标签
    • where标签
    • set标签
    • trim标签
    • foreach

list的for循环

mybatis-plus

  1. IPage<Map<String, Object>> page = this.baseMapper.selectMyPage(new Page<Map<String,Object>>(pageIndex, pageSize), paramMap);
  2. @Select({ "<script>",
  3. "select a.* from t_customer a where a.TABLE_ID in",
  4. "<foreach item='item' collection='tableIdList' separator=',' open='(' close=')' >",
  5. " #{item, jdbcType=VARCHAR}",
  6. "</foreach>",
  7. "</script>"})
  8. List<Customer > findByTableIdList(@Param("tableIdList")List<String> tableIdList);

分页

service

  1. paramMap.put("cust_name", "%公司");
  2. paramMap.put("clue_level_code ", "6");
  3. IPage<Map<String, Object>> page = this.baseMapper.selectMyPage(new Page<Map<String,Object>>(pageIndex, pageSize), paramMap);

mapper

  1. @Select({ "<script>",
  2. " select * from t_customer ",
  3. " where 1=1 ",
  4. " <if test=\"param.cust_name != null and param.cust_name != '' \"> and cust_name like #{param.cust_name} </if> ",
  5. " <if test=\"param.cust_code != null and param.cust_code != '' \"> and cust__code = #{param.cust_code} </if> ",
  6. "</script>"})
  7. IPage<Map<String, Object>> selectMyPage(IPage<Map<String, Object>> page, @Param("param")Map<String, Object> param);

insert

  1. void insertMapObject(@Param("param")Map<String, Object> param);
  2. <insert id="insertMapObject" parameterType="Map">
  3. insert into t_customer (
  4. <if test="param.ID != null and param.ID != '' ">ID </if>
  5. <if test="param.CUST_ID != null and param.CUST_ID != '' ">, CUST_ID </if>
  6. <if test="param.CUST_NAME != null and param.CUST_NAME != '' ">, CUST_NAME </if>
  7. ) values (
  8. <if test="param.ID != null and param.ID != '' ">#{param[${'ID'}]} </if>
  9. <if test="param.CUST_ID != null and param.CUST_ID != '' ">, #{param[${'CUST_ID'}]} </if>
  10. <if test="param.CUST_NAME != null and param.CUST_NAME != '' ">, #{param[${'CUST_NAME'}]} </if>
  11. )
  12. </insert>

写法二
动态变量map参数,
缺点:必须确保key就是字段名

  1. @Select({ "<script>",
  2. " insert into t_customer ",
  3. " <foreach collection='param.keys' item='key' open='(' close=')' separator=',' >",
  4. " ${key}",
  5. " </foreach>",
  6. " values",
  7. " <foreach collection='param.keys' item='key' open='(' close=')' separator=',' >",
  8. " #{param[${key}]}",
  9. " </foreach>",
  10. "</script>"})
  11. void insertMapObject(@Param("param")Map<String, Object> param);

update

  1. void updateMapObject(@Param("param")Map<String, Object> param, @Param("id")String id);
  2. <update id="updateMapObject" parameterType="Map">
  3. update t_customer
  4. set id = #{id}
  5. <if test="param.ID != null and param.ID != '' ">, ID = #{param.ID} </if>
  6. <if test="param.CUST_ID != null and param.CUST_ID != '' ">, CUST_ID = #{param.CUST_ID} </if>
  7. <if test="param.CUST_NAME != null and param.CUST_NAME != '' ">, CUST_NAME = #{param.CUST_NAME} </if>
  8. where id = #{id}
  9. </update>

写法二
动态变量map参数,
缺点:必须确保key就是字段名

  1. @Select({ "<script>",
  2. " update t_customer ",
  3. " set",
  4. " <foreach collection='param.keys' item='key' open='' close='' separator=',' >",
  5. " ${key} = #{param[${key}]}",
  6. " </foreach>",
  7. " where id = #{id}",
  8. "</script>"})
  9. void updateMapObject(@Param("param")Map<String, Object> param, @Param("id")String id);

#{} 预处理

#{}可以防止sql注入

${} 字符串替换

批量提交

mybatis plus实用工具类

TableInfoHelper

  1. // 实体类反射表辅助类
  2. com.baomidou.mybatisplus.core.metadata.TableInfoHelper
  3. // 获取实体映射表信息
  4. public static TableInfo getTableInfo(Class<?> clazz)
  5. // 获取所有实体映射表信息
  6. public static List<TableInfo> getTableInfos()
  7. public boolean saveOrUpdate(T entity) {
  8. if (null != entity) {
  9. Class<?> cls = entity.getClass();
  10. TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
  11. Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!");
  12. String keyProperty = tableInfo.getKeyProperty();
  13. Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!");
  14. Object idVal = ReflectionKit.getMethodValue(cls, entity, tableInfo.getKeyProperty());
  15. return StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal)) ? save(entity) : updateById(entity);
  16. }
  17. return false;
  18. }

mapper.xml文件常用标签

select标签

  1. <select id="selectByPage" parameterType="" resultType="" resultMap="" flushCache="" useCache="">
  2. select * from t_table where id = #{param.id}
  3. </select>
  • parameterType: 参数类型,可选属性。
    MyBatis 可以通过 TypeHandler推断出具体传入语句的参数,默认值为 unset
  • resultType: 期望返回的数据类型的类的全限定名。
  • resultMap: 结果集映射,不可以和 resultType 同时使用。一般如果返回数据类型是常用的类型
    比如 String Map List 的时候,可以使用 resultType
    如果返回的是简单 POJO 类的时候,也可以直接使用 resultType
    如果是复杂的映射或者连级查询的时候就需要使用 resultMap
  • flushCache :将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:false。
  • useCache: 将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true。

insert标签

update标签

delete标签

if标签

choose标签

where标签

where标签会除去多余的 andor

  1. <select id="selectByPage" resultType="Map">
  2. select * from t_table
  3. <where>
  4. <if test="param.isEnable !=null and param.isEnable !=''"> and IS_ENABLE=#{param.isEnable}</if>
  5. <if test="param.updateControlId !=null and param.updateControlId !=''"> and UPDATE_CONTROL_ID=#{param.updateControlId}</if>
  6. </where>
  7. </select>

set标签

set标签会除去多余的逗号 (,

  1. <update id="update">
  2. upate t_table
  3. <set>
  4. <if test="param.isEnable !=null and param.isEnable !=''"> IS_ENABLE=#{param.isEnable},</if>
  5. <if test="param.updateControlId !=null and param.updateControlId !=''">UPDATE_CONTROL_ID=#{param.updateControlId},</if>
  6. </set>
  7. where id = #{param.id}
  8. </update>

trim标签

去掉前缀and

  1. <select id="selectByPage" resultType="Map">
  2. select * from t_table
  3. <trim prefix="where" prefixOverrides="and">
  4. <if test="param.isEnable !=null and param.isEnable !=''"> and IS_ENABLE=#{param.isEnable}</if>
  5. <if test="param.updateControlId !=null and param.updateControlId !=''"> and UPDATE_CONTROL_ID=#{param.updateControlId}</if>
  6. </trim>
  7. </select>

去掉后缀逗号,

  1. <insert id="insert">
  2. insert into t_table
  3. <trim prefix="(" suffix=")" suffixOverrides=",">
  4. <if test="param.isEnable !=null">IS_ENABLE,</if>
  5. <if test="param.updateControlId !=null">UPDATE_CONTROL_ID,</if>
  6. </trim>
  7. <trim prefix="values(" suffix=")" suffixOverrides=",">
  8. <if test="param.isEnable !=null">#{param.isEnable},</if>
  9. <if test="param.updateControlId !=null">#{param.updateControlId},</if>
  10. </trim>
  11. </insert>

foreach

  1. <delete id="insert">
  2. delete t_table
  3. where id in
  4. <foreach collection="param.ids" index="i" item="id" open="(" separator="," close=")">
  5. #{id}
  6. </foreach>
  7. </delete>

发表评论

表情:
评论列表 (有 0 条评论,383人围观)

还没有评论,来说两句吧...

相关阅读

    相关 前端技巧

    让文字在一行上下最好的居中方法:line-height: px return关键字2个作用:1.结束函数 2.返回一个值给调用者 给容器设定尺寸, 不给图片设置尺寸,图片显

    相关 CSS技巧

    11.17 CSS英文命名 在写CSS的时候,经常为一些名字而发愁,比如说菜单后面的背景要用什么词来表示呢?大家都知道,菜单一般用menu来表示,那么菜单后面的背景我会用me