• Mybatis映射文件中的参数传递


    一、接口中只有一个参数

    1.参数是基本类型or基本类型的包装类or字符串类型

    这种情况下映射文件中#{}里的内容可以是任意的,你可以使用#{xxx} 或 #{abc} .....因为此时#{}相当于一个占位符。

    public interface EmployeeMapper {
        public Employee getEmpById(Integer id);
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <select id="getEmpById" resultType="com.xj.domain.Employee">
            select * from employee where id = #{xxx}
        </select>
    </mapper>
    映射文件

    2.参数是对象类型

    这种情况下直接通过 #{对象属性名} 即可取值。

    public interface EmployeeMapper {
        public void saveEmp();
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <insert id="saveEmp">
            INSERT INTO employee(id,lastName,email,gender,d_id) values(#{id},#{lastName},#{email},#{gender},#{dId})
        </insert>
    </mapper>
    映射文件

    3.参数是Map类型

    这种情况下可以直接跟据map的key进行取值。

    public interface EmployeeMapper {
        public Employee getEmp(Map<String,Object> map);
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <select id="getEmp" resultType="com.xj.domain.Employee">
            SELECT * FROM employee WHERE id = #{id} AND lastName = #{lastName}
        </select>
    </mapper>
    映射文件

    4.参数是Conlection、数组类型的

    方式一:使用mybatis默认提供的方式

    这种情况下mybatis也会帮我们对参数进行特殊处理,mybtis同样把Conlection、数组类型的参数封装为Map,如果是Collection类型则Map的key为collection,如果是Array类型则Map的key为array。特殊的如List,他的key为list,但是由于List也属于Collection,所以key也可以用collection。

    public interface EmployeeMapper {
        public Employee getEmpById(List<Integer> ids);
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <select id="getEmpById" resultType="com.xj.domain.Employee">
            select * from employee where id = #{list[0]}
        </select>
    </mapper>
    映射文件

     方式二:使用@Param注解

    我们可以在接口的参数上标注@Param注解,指定参数的名称,然后映射文件中就可以直接通过#{注解指定的参数名}进行取值

    public interface EmployeeMapper {
        public Employee getEmpById(@Param("idList") List<Integer> ids);
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <select id="getEmpById" resultType="com.xj.domain.Employee">
            select * from employee where id = #{idList[0]}
        </select>
    </mapper>
    映射文件

    二、接口中有两个及两个以上参数

    1.多个参数都是基本类型or基本类型的包装类orString类型

    方式一:使用mybatis默认提供的方式

    有多个参数时,mybatis会把这多个参数封装为Map类型,这个Map中的key为param1,param2,....,paramN。所以我们要取出第一个参数就可以用#{param1}取出,要取第N个参数就用#{paramN}。

    public interface EmployeeMapper {
        public Employee getEmp(String lastName,Integer id);
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <select id="getEmp" resultType="com.xj.domain.Employee">
            SELECT * FROM employee WHERE id = #{param2} AND lastName = #{param1}
        </select>
    </mapper>
    映射文件

    方式二:使用@Param注解

    我们可以在接口的参数上标注@Param注解,指定参数的名称,然后映射文件中就可以直接通过#{注解指定的参数名}进行取值

    public interface EmployeeMapper {
        public Employee getEmp(@Param("name") String lastName,@Param("id") Integer id);
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <select id="getEmp" resultType="com.xj.domain.Employee">
            SELECT * FROM employee WHERE id = #{id} AND lastName = #{name}
        </select>
    </mapper>
    映射文件

    2.多个参数中既有对象类型,又有参数是基本类型or基本类型的包装类or字符串类型

    public interface EmployeeMapper {
        public Employee getEmp(@Param("id") Integer id,@Param("emp") Employee employee);
    }
    接口
    <mapper namespace="com.xj.mapper.EmployeeMapper">
        <select id="getEmp" resultType="com.xj.domain.Employee">
            SELECT * FROM employee WHERE id = #{id} AND lastName = #{emp.lastName}
        </select>
    </mapper>
    映射文件
  • 相关阅读:
    MDX Step by Step 读书笔记(六) Building Complex Sets (复杂集合的处理) Filtering Sets
    在 Visual Studio 2012 开发 SSIS,SSAS,SSRS BI 项目
    微软BI 之SSIS 系列 在 SSIS 中读取 SharePoint List
    MDX Step by Step 读书笔记(五) Working with Expressions (MDX 表达式) Infinite Recursion 和 SOLVE_ORDER 原理解析
    MDX Step by Step 读书笔记(五) Working with Expressions (MDX 表达式)
    使用 SQL Server 2012 Analysis Services Tabular Mode 表格建模 图文教程
    MDX Step by Step 读书笔记(四) Working with Sets (使用集合) Limiting Set and AutoExists
    SQL Server 2012 Analysis Services Tabular Model 读书笔记
    Microsoft SQL Server 2008 MDX Step by Step 学习笔记连载目录
    2011新的开始,介绍一下AgileEAS.NET平台在新的一年中的发展方向
  • 原文地址:https://www.cnblogs.com/bear7/p/13696735.html
Copyright © 2020-2023  润新知