天天看点

pgsql -- 批量查询

工具:mybatis+pgsql

详见pgsql官方文档

案例

dao层

List<User> list(List<String> idList); 通过idList查询所有用户信息
           

sql

<select id = "list" parameterType = "java.util.List" resultType = "com.test.dto.User">
	select  username,address,sex
	  from  user
   <if test = 'list != null'>
     where  id in
    <foreach collection="list" separator="," item="val" open="(" close=")">
         #{val}
    </foreach>
   </if>
</select>
           

或者可以指定list的名称,如下

dao层

List<User> list(@Param("userList") List<User> userList); 通过idList查询所有用户信息
           

sql

<select id = "list" parameterType = "java.util.List" resultType = "com.test.dto.User">
	select  username,address,sex
	  from  user
   <if test = 'userList != null'>
     where  id in
    <foreach collection="userList" separator="," item="user" open="(" close=")">
         #{user.id}
    </foreach>
   </if>
</select>
           

相关参考文章

Mybatis最入门—动态查询(foreach

mybatis动态sql之foreach标签

mybatis入门到精通

MyBatis foreach标签的用法

mybatis之foreach用法

继续阅读