用select * from user where id in (2,4,11)的sql语句构建Mapper文件实现个性化查询
时间: 2024-05-07 11:15:08 浏览: 81
Mapper文件代码示例:
```xml
<!--定义namespace-->
<mapper namespace="com.example.mapper.UserMapper">
<!--定义个性化查询语句-->
<select id="getUserByIds" parameterType="java.util.List" resultMap="userMap">
SELECT * FROM user WHERE id IN
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<!--定义ResultMap-->
<resultMap id="userMap" type="com.example.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="email" property="email"/>
<result column="phone" property="phone"/>
</resultMap>
</mapper>
```
Java代码示例:
```java
//定义Mapper接口
public interface UserMapper {
List<User> getUserByIds(List<Integer> ids);
}
//调用Mapper接口方法
List<Integer> ids = Arrays.asList(2, 4, 11);
List<User> users = userMapper.getUserByIds(ids);
```
阅读全文