mybatis初始一

mybatis对单参数的处理:

delete from user where id=#{id}      在mapper.xml中,此条语句的{id}与所调用方法传值时的名称未规定必须一致,但最好保持一致;

mybatis对多参数的处理:

mybatis处理两个参数比如:select *from user where username=#{username} and password=#{password},即使在调用方法时

传递参数的名称为:(username,password),但在此处依然报错,报绑定异常,有时需要改为param1,param2;

mybatis对javaBean的传递参数处理:

select *from user where username=#{username} and password=#{password};//假如有一个Person对象包含username和password,

当我们对其传递参数时,可以直接传递一个Person对象,那么在mybatis中可以直接根据其pojo的名称进行赋值;

mybatis对于map集合的处理:

Map<String,Object> map=new HashMap<String,Object>();map.put("name","李四");map.put(“pass”,“sg”);

select *from user where username=#{name} and password=#{pass};//假如传递的参数是由map封装而成,那么此时就需要

在mapper.xml中传递对应的key,其他则会出错;

mybatis处理含有注解param的值:public Person delete(@Param("username") String username,String password);

假如使用了注解@Param时,则相当于在底层进行了map封装,所以sql语句中也要用对应的参数,当然对于第二个没有用注解的方式,默认为param2,

以此类推;

mybatis集合数组的传递:

假如传递的参数为Collection,那么在xml中,就得匹配以collection[0];

假如传递的是int数组,那么匹配的就是array[0];

处理方式,假如不想用默认的方式,那么可以使用@Param定义自己想要的value;

使用map的方式,业务可读性差;

 

posted on 2019-05-30 19:48  要学的东西很多  阅读(178)  评论(0)    收藏  举报

导航