首页 > 其他分享 >mybatis全局变量 (mybatis.configuration.variables) 的应用

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

时间:2023-04-12 20:36:54浏览次数:60  
标签:parent variables ids length mybatis configuration

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的#和$的区别就明白了。

标签:parent,variables,ids,length,mybatis,configuration
From: https://www.cnblogs.com/FengZeng666/p/17311130.html

相关文章

  • mybatis.type-aliases-package 的作用
    在mapper.xml文件中的resultMap的type或者parameterType会用到自定义的POJO。如果在application.yml中没有配置mybatis.type-aliases-package的话,就需要写全限定类名:<selectid="selectByStudentById"resultType="com.example.domain.Student">SELECT*FROMstudent......
  • MyBatis
    1.简介MyBatis是一款优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。MyBatis免除了几乎所有的JDBC代码以及设置参数和获取结果集的工作。MyBatis可以通过简单的XML或注解来配置和映射原始类型、接口和JavaPOJO(PlainOldJavaObjects,普通老式Java对象)为数......
  • MyBatis中XXMapper示例记录
    XXMapper.xml的结构示例如下,包括<resultMap>、<id>、<result>、<select>、<update>、<foreach>、<if>标签的使用:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper......
  • 【随手记】解决mybatis返回List<map>类型的数据时 无序 并且 不能返回空值
    返回结果无序希望表格的列能根据数据库查出来的数据保持一致,但是返回页面的结果集是无序在mybatis中使用List<Map>结构接收数据,发现输入的sql语句结果并不是按照输入的字段名顺序返回的。例如输入selectcol1,col2,col3fromtable却返回col2col3col1***......
  • mybatis if-else(写法)
    mybatisif-else(写法)mybaits中没有else要用chosewhenotherwise代替范例<selectid="selectSelective"resultMap="xxx"parameterType="xxx">select<includerefid="Base_Column_List"/>fromxxxwhe......
  • SpringBoot 集成 MybatisPlus 六——ActiveRecord 增、删、改
    1向表中插入记录1.1插入所有列在创建实体对象时,指定所有字段的内容,包括ID列。@TestpublicvoidtestAddUser(){Useruser=newUser(20,"成吉思汗","男","一代天骄");booleanres=user.insert();System.out.println(res);}调用MyBatisPlus时,后台执行的......
  • Mybatis_05 注解CRUD
    Mybatis_05注解CRUD1、面向接口编程大家之前都学过面向对象编程,也学习过接口,但在真正的开发中,很多时候我们会选择面向接口编程根本原因:解耦,可拓展,提高复用,分层开发中,上层不用管具体的实现,大家都遵守共同的标准,使得开发变得容易,规范性更好在一......
  • SpringBoot 集成 MybatisPlus 五——ActiveRecord
    1什么是ActiveRecordActiveRecord(活动记录),是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录。在ActiveRecord模式中,对象中既有持久存储的数据,也有针对数据的操作,ActiveRecord模式把数据增删改查的逻辑作为对象的一......
  • 整合Mybatis
    步骤:在pom.xml中导入相关jar包,注意版本号junit<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency>mybatis<dependency><groupId>org.myb......
  • mybatispuls的代码生成
    pom文件配置 <!--https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5......