码迷,mamicode.com
首页 > 编程语言 > 详细

推荐的MyBatis传参方式List、数组等

时间:2018-01-12 22:37:25      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:array   list   pen   https   param   推荐   ref   collect   type   

MyBatis 推荐的传参方式

如果要详细的学习 MyBatis,推荐看这个教程:https://www.w3cschool.cn/mybatis/

1. 单个参数

//接口方法
int getAgeById(Integer id);
//xml映射文件
<select id="getAgeById" resultType="Integer">
    select age from user where user_id = #{id}
</select>

2. 多个参数

//接口方法
 User login(@Param("username") String username, @Param("password") String password);
 //xml映射文件
 <select id="login" resultMap="BaseResultMap">
    select
    *
    from user
    where username = #{username} and password = #{password}
</select>

3. 数组参数

//接口方法
ArrayList<User> selectByIds(Integer [] ids);
//xml映射文件
<select id="selectByIds" resultMap="BaseResultMap">
    select
    *
    from user where id in
    <foreach item="item" index="index" collection="array" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>

4.List参数

//接口方法
ArrayList<User> selectByIds(List<Integer> ids);
//xml映射文件
<select id="selectByIds" resultMap="BaseResultMap">
    Select
    <include refid="Base_Column_List" />
    from jria where ID in
    <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
          #{item}
      </foreach>
  </select> 

推荐的MyBatis传参方式List、数组等

标签:array   list   pen   https   param   推荐   ref   collect   type   

原文地址:http://blog.51cto.com/13523664/2060417

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!