mybatis 内置参数

本文介绍了MyBatis中内置参数_parameter的用法,包括单个参数和多个参数的情况,展示了错误和正确的写法。同时提到了_databaseId,当配置了databaseIdProvider时,它代表当前数据库的别名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

_parameter:代表整个参数

  • 单个参数:_parameter就是这个参数
    <!--当使用@Param("employee")修饰变量后,就不能在使用"_parameter",可以使用employee或param1来获取变量中的值-->
    <!--public List<Employee> getEmployeeTestInnerParameter(@Param("employee") Employee employee);-->
    <select id="getEmployeeTestInnerParameter3" resultType="com.ll.mybatis.entity.Employee">
        select id, last_name, email, gender
        from tbl_employee
        <if test="employee != null">
            <!--<if test="_parameter!=null">-->
            <where>
                <if test="employee.lastName!=null">
                    last_name=#{employee.lastName}
                </if>
                <if test="employee.email!=null">
                    and email=#{employee.email}
                </if>
            </where>
        </if>
    </select>

下面是错误写法

    <!--public List<Employee> getEmployeeTestInnerParameter(Employee employee);-->
    <select id="getEmployeeTestInnerParameter" resultType="com.ll.mybatis.entity.Employee">
        select id, last_name, email, gender
        from tbl_employee
        <!--下面写法错误,list类型形参employee没有使用@param修饰,不能直接使用employee,mybatis会误认为是对象中的属性-->
        <if test="employee != null">
            <!--<if test="_parameter!=null">-->
            <where>
                <if test="employee.lastName!=null">
                    last_name=#{employee.lastName}
                </if>
                <if test="employee.email!=null">
                    and email=#{employee.email}
                </if>
            </where>
        </if>
    </select>

下面是正确写法

 <!--public List<Employee> getEmployeeTestInnerParameter(Employee employee);-->
 <select id="getEmployeeTestInnerParameter" resultType="com.ll.mybatis.entity.Employee">
        select id, last_name, email, gender
        from tbl_employee
        <if test="_parameter!=null">
            <where>
                <!--获取属性_parameter可以省略-->
                <if test="_parameter.lastName!=null">
                    last_name=#{lastName}
                </if>
                <if test="email!=null">
                    and email=#{_parameter.email}
                </if>
            </where>
        </if>
    </select>
  • 多个参数:多个参数会被封装为一个map:_parameter就是代表这个map

_databaseId

    <!--
       databaseIdProvider:数据库厂商标识,支持多数据库厂商
   -->
    <databaseIdProvider type="DB_VENDOR">
        <property name="SQL Server" value="sqlserver"/>
        <property name="DB2" value="db2"/>
        <property name="Oracle" value="oracle"/>
        <property name="MySQL" value="mysql"/>
    </databaseIdProvider>

如果配置了databaseIdProvider标签,_databaseId就是代表当前数据库的别名

    <!--根据不同的数据库执行不同的内容-->
    <!--public List<Employee> getEmployeeTestInnerParameter(Employee employee);-->
    <select id="getEmployeeTestInnerParameter" resultType="com.ll.mybatis.entity.Employee">
        <!--mysql数据库-->
        <if test="_databaseId=='mysql'">
            select id, last_name, email, gender
            from tbl_employee
            <if test="_parameter!=null">
                <where>
                    <if test="_parameter.lastName!=null">
                        last_name=#{lastName}
                    </if>
                    <if test="_parameter.email!=null">
                        and email=#{email}
                    </if>
                </where>
            </if>
        </if>

        <!--orcale数据库-->
        <if test="_databaseId=='orcale'">
            select id, last_name, email, gender
            from employees
        </if>
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值