mybatis接口方法参数传参解读

目录

单个简单类型参数

Map参数

实体类(pojo)参数

多参数

@Param注解(命名参数) 


单个简单类型参数

简单类型包括:

  • byte short int long float double char
  • Byte Short Integer Long Float Double Character
  • String
  • java.util.Date
  • java.sql.Date
    /**
     * 根据学生id进行查询
     * @param id
     * @return
     */
    Student selectById(Long id);

 对应的映射xml文件

    <select id="selectById" resultType="com.study.pojo.Student">
        select * from t_student where id=#{id};
    </select>

简单类型对于mybatis来说都是可以自动类型识别的:

  • 也就是说对于mybatis来说,它是可以自动推断出ps.setXxxx()方法的。ps.setString()还是ps.setInt()。它可以自动推断。

如果参数只有一个的话,#{} 里面的内容就随便写了。对于 ${} 来说,注意加单引号。

Map参数

需求:根据name和age查询 

/**
* 根据name和age查询
* @param paramMap
* @return
*/
List<Student> selectByParamMap(Map<String,Object> paramMap);

  对应的映射xml文件

    <select id="selectByParamMap" resultType="com.study.pojo.Student">
        select * from t_student where name=#{name} and age=#{age}
    </select>

 对应的java测试代码

    @org.junit.Test
    public void test02(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Map map=new HashMap();
        map.put("name","张三");
        map.put("age","18");
        List<Student> student = mapper.selectByParamMap(map);
        System.out.println(student);
    }

这种方式是手动封装Map集合,将每个条件以key和value的形式存放到集合中。然后在使用的时候通过#{map集合的key}来取值。

实体类(pojo)参数

需求:插入一条Student数据

    /**
     * 插入学生数据
     * @param student
     * @return
     */
    int insertStudent(Student student);

 对应的映射xml文件

    <insert id="insertStudent">
        insert  into t_student values (null ,#{name},#{age},#{brith})
    </insert>

  对应的java测试代码

    @org.junit.Test
    public void test03(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setAge("28");
        student.setBrith(new Date());
        student.setName("李四");
        int i = mapper.insertStudent(student);
        System.out.println(i);
        System.out.println(student);
    }

这里需要注意的是:#{} 里面写的是属性名字。这个属性名其本质上是:set/get方法名去掉set/get之后的名字,也就是使用了反射进行。 

多参数

需求:通过name和sex查询

    /**
     * 根据name和age查询
     * @param name
     * @param age
     * @return
     */
    List<Student> selectByNameAndAge(String name, String age);

  对应的映射xml文件(错误示范)

    <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
        select * from t_student where name=#{name} and age=#{age}
    </select>

   对应的java测试代码

    @org.junit.Test
    public void test04(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> student = mapper.selectByNameAndAge("张三", "18");
        System.out.println(student);
    }

 运行报错

 异常信息描述了:name参数找不到,可用的参数包括[arg1, arg0, param1, param2]

修改StudentMapper.xml配置文件:尝试使用[arg1, arg0, param1, param2]去参数

修改xml文件

    <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
        select * from t_student where name=#{param1} and age=#{param2}
    </select>

再次运行得到结果 

 同时也可以将xml文件修改为:

    <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
        select * from t_student where name=#{arg0} and age=#{arg1}
    </select>

通过测试可以看到:

  • arg0 是第一个参数
  • param1是第一个参数
  • arg1 是第二个参数
  • param2是第二个参数

实现原理:实际上在mybatis底层会创建一个map集合,以arg0/param1为key,以方法上的参数为value例如以下代码:

Map<String,Object> map = new HashMap<>();
map.put("arg0", name);
map.put("arg1", sex);
map.put("param1", name);
map.put("param2", sex);

// 所以可以这样取值:#{arg0} #{arg1} #{param1} #{param2}
// 其本质就是#{map集合的key}
注意:使用mybatis3.4.2之前的版本时:要用#{0}和#{1}这种形式。

@Param注解(命名参数) 

可以不用arg0 arg1 param1 param2吗?这个map集合的key我们自定义可以吗?当然可以。使用@Param注解即可。这样可以增强可读性。

需求:根据name和age查询

添加注解

    /**
     * 根据name和age查询
     * @param name
     * @param age
     * @return
     */
    List<Student> selectByNameAndAge(@Param("name") String name,@Param("age") String age);

 对应的xml文件

    <select id="selectByNameAndAge" resultType="com.study.pojo.Student">
        select * from t_student where name=#{name} and age=#{age}
    </select>

猜你喜欢

转载自blog.csdn.net/m0_62436868/article/details/131763543