Loading

mybatis全局变量 (mybatis.configuration.variables) 的应用

mybatis.configuration.variables是一个可自定义的全局变量:

在 application.yml 中定义:

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.entity
  configuration:
    variables:
      dbtype: mysql

mapper.xml中的使用:

<!-- 更新子树 -->  
<update id="updateSunTreeParentIds">  
	update sys_menu set parent_ids= CONCAT(#{newParentIds},
      
    <if test="'${dbType}' == 'mysql'">
         substring(parent_ids, length(#{oldParentIds})+1,length(parent_ids)+1))
    </if>
	
    <if test="'${dbType}' == 'oracle'">
         substr(parent_ids, length(#{oldParentIds})+1,length(parent_ids)+1))
    </if>
	
	where parent_ids like concat(#{oldParentIds}, '%')     
  
</update>

注意这里dbtype是字符串,需要加引号,因为${}替换后不会自动加引号,结果就将变为
<if test="oracle == 'oracle'">
很明显是不对的。

posted @ 2023-04-12 20:38  拾月凄辰  阅读(407)  评论(0编辑  收藏  举报