MyBatis 参数传递

2018-07-24  本文已影响28人  沧海一粟谦
Slumdog Millionaire

实际应用

1.SQL语句中使用IN的情况,可以使用数组封装IN中的值
2.批量操作数据的情况,可以把操作的数据封装在数组中

参数处理

1.传递单个参数的形式(mybatis会自动进行参数的赋值)
2.传递多个参数(mybatis会自动封装在Map集合中)
3.Collection、List、Array作为参数,封装为Map,但有一定的规则)

1.单参数处理

public interface PersonMapper {
     public  void  deletePerson(Integer id);
}
<delete id="deletePerson" parameterType="int">
        delete from employee where id=#{id}
    </delete>

测试

public class ParameterTest {

    public static SqlSessionFactory sqlSessionFactory = null;

    public static SqlSessionFactory getSqlSessionFactory() {
        if (sqlSessionFactory == null) {
            String resource = "mybatis-config.xml";
            try {
                Reader reader = Resources.getResourceAsReader(resource);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sqlSessionFactory;
    }

    /**
     * 根据ID删除所对应的Person数据
     */
    public void deletePerson() {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();

        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);

        personMapper.deletePerson(2);

        sqlSession.commit();
    }

 public static void main(String[] args) {
       new ParameterTest().deletePerson();
    }
}

2.多参数处理

1.JavaBean传递参数

public interface PersonMapper {
//     public Person getPersonByNameAndGender(String username,String gender);
     public Person getPersonByNameAndGender(Person person)
}
<select id="getPersonByNameAndGender" resultType="person">
   <!-- select * from persone where username=#{param1} and gender=#{param2} -->
        select * from persone where username=#{username} and gender=#{gender}
    </select>

如果使用Mybatis默认的多参数传值必须使用mybatis根据参数位置自定义的名字param1 和 param2,而不能使用#{username} 和#{gender}

2.Map接口

public interface PersonMapper {
     public Person getPersonByNameAndGender(Map<String,Object> param);
}
<select id="getPersonByNameAndGender" resultType="person">
        <!-- 传的参数是Map的键,否则取出的值为null -->
        select * from persone where username=#{name} and gender=#{gender}
    </select>

测试

public void testPersonByNameAndGender(){

        SqlSession sqlSession = this.getSqlSessionFactory().openSession();

        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);

        Map<String,Object> param=new HashMap<String, Object>();

        param.put("name","wangwu");
        param.put("gender","F");

        Person person=personMapper.getPersonByNameAndGender(param);
       System.out.println(person);

    }

3.注解@param

public interface PersonMapper {
     public Person getPersonByNameAndGender(@Param("username") String username, @Param("gender") String gender);
}
<select id="getPersonByNameAndGender" resultType="person">
        select * from persone where username=#{username} and gender=#{gender}
    </select>

3.集合类型参数传递

1.当参数为Collection接口,转换为Map,Map的key为collection
2.当参数类型为List接口,除了collection的值外,list作为key
3.当参数为数组,也会转换为Map,Map的key为array

public interface PersonMapper {
     /** 参数类型为List接口 **/
     // public Person getPersonByCollection(Collection list); 
    /** 参数为数组 **/
     public Person getPersonByCollection(int[] ids);
}

同样可以使用@param注解给参数起个别名

<select id="getPersonByCollection" resultType="person">
         <!-- 参数类型为List接口 -->
        <!-- select * from persone where id=#{collection[0]} -->
        <!-- select * from persone where id=#{list[0]} -->
        <!-- 参数为数组 -->
        select * from persone where id=#{array[0]}
    </select>

测试

public void testCollection()
    {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();

        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        /** 参数类型为List接口 **/
        Person person=personMapper.getPersonByCollection(Array.asList(1,2));
        /** 参数为数组 **/
        Person person=personMapper.getPersonByCollection(new int[]{1,2});

        System.out.println(person);
    }

4.数组参数传递

foreach元素:
特点:循环遍历集合,支持数组和List、Set接口
应用:数据库中数据字典的内容,经常使用foreach元素确定查找
常用属性:collection(遍历的集合的名字),item(当前迭代对象),index(当前迭代对象的索引),open和close(拼接开头和结尾的字符串),separator(每次循环的分隔符)

public interface PersonMapper {
     public List<Person> getPersonsByIds(int[] ids);
}

使用foreach动态遍历

<select id="getPersonsByIds" resultType="person">
        select * from person where id in
        <foreach collection="array" item="id" index="i" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

测试

public void testCollection()
    {
        SqlSession sqlSession = this.getSqlSessionFactory().openSession();

        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);

        Person person=personMapper.getPersonByCollection(new int[]{1,2,3,4,5});

        System.out.println(person);
    }

参数处理总结

1.使用map传递参数,业务可读性差,适用于参数较少的情况
2.@param,受到参数个数(n)的影响,建议n<5时,为最佳的传递方式
3.以上参数的处理各有利弊,参数>5时,建议用JavaBean方法

上一篇 下一篇

猜你喜欢

热点阅读