Mybatis-动态SQL-内置参数_databaseId和_parameter
2018-01-30 00:00
567 查看
因为数据库版本的不同我们需要写多个SQL,为了简化和提高维护性Mybatis提供了“_databaseId”内置参数来整合SQL以适应不同版本数据库,Mybatis还提供了“_parameter”内置参数
下面看一个SQL配置,同时使用了这两个内置对象
这个SQL并没有写得很完整,只是为了说明两个内置参数的使用方式,不过还是可以执行的。
_databaseId:
数据库版本,其值是在Mybatis全局配置文件中的<databaseIdProvider>标签中配置的<!-- databaseIdProvider:支持多数据库厂商的; type="DB_VENDOR":VendorDatabaseIdProvider是DB_VENDOR映射的实现类 作用:就是得到数据库厂商的标识,调用getDatabaseProductName()得到数据库名称,如Oracle(MySQL,SQL Server等其他名字)然后和配置中property比对,拿 到oracle,在与sql映射文件中<select>标签中的databaseid中的值匹配,Mybatis就能根据数据库厂商标识来执行不同的sql; --> <databaseIdProvider type="DB_VENDOR"> <property name="MySQL" value="mysql"/> <property name="Oracle" value="oracle"/> </databaseIdProvider>
_parameter:
由方法传递过来的参数,可以是任何参数类型,Java会把基本参数类型自动封装成包装类型下面看一个SQL配置,同时使用了这两个内置对象
<select id="getCustsByCust" resultType="com.jv.dynamic.bean.Cust"> <if test="_parameter != null"> select cust_id, <choose> <when test="custId!=null and custId%2==0" > concat(cust_name,'_aa') cust_name </when> <otherwise> concat(cust_name,'_bb') cust_name </otherwise> </choose> from cust <where> <!-- ognl表达式还支持&&等操作符,但是在xml中需要使用其转义字符 比如:<if test="custId!=null!=null && custId!=null!="""> 其中&是代表&符号;"代表双引号 为了阅读方便,推荐使用肺转移符 --> <if test="custId!=null and custId!=''"> cust_id=#{custId} </if> <if test="custName!=null and custName!=''"> and cust_name like #{custName} </if> </where> </if> <if test="_parameter == null"> <!-- 因为是全表查询,需要限制返回的记录数,否则容易造成内存溢出 --> select cust_id,cust_name from cust <if test="_databaseId == 'mysql' "> limit 1000 </if> <if test="_databaseId == 'oracle'"> <![CDATA[ where rownum<1001 ]]> </if> </if> </select>
这个SQL并没有写得很完整,只是为了说明两个内置参数的使用方式,不过还是可以执行的。
相关文章推荐
- 21、动态SQL之内置参数_parameter的使用
- MyBatis动态SQL使用,传入参数Map中的Key判断
- Mybatis 框架 6 - 动态SQL_单参数List、单参数Array、多参数Map
- MyBatis源码(五)之动态Sql解析运行阶段参数处理
- MyBatis参数传入集合之foreach动态sql
- mybatis if 动态生成SQL的一个注意点 parameterType="map"
- MyBatis 动态SQL的单参数不执行问题
- MyBatis参数传入集合之foreach动态sql
- 向SqlParameter内动态添加参数
- MyBatis参数传入集合之foreach动态sql
- MyBatis动态SQL使用,传入参数Map中的Key判断
- mybatis 动态sql和参数
- MyBatis中的动态SQL,实现不确定条件的CRUD-----使用Map来传递查询的参数
- mybatis多参数传入的动态sql
- MyBatis参数传入集合之foreach动态sql
- 向SqlParameter内动态添加参数
- Mybatis中动态SQL语句中的parameterType不同数据类型的用法
- MyBatis动态SQL使用,传入参数Map中的Key判断
- Mybatis动态SQL单一基础类型参数用if标签注意事项
- MyBatis参数传入集合之foreach动态sql