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

发布时间 2023-04-12 20:32:21作者: 拾月凄辰

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>

需要加引号,数字类型的可以不用管, 但如果遇到属性值为中文,不加引号报错提示列找不到,这个问题不难理解, 弄明白了mybatis的#和$的区别就明白了。