包装类:QueryVO.java
package pojo; import java.util.ArrayList; import java.util.List; public class QueryVO { private User user; private List<Integer> ids; public List<Integer> getIds() { return ids; } public void setIds(List<Integer> ids) { this.ids = ids; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace命名空间,隔离sql文件 --> <!-- #{}占位符相当于jdbc的? --> <!-- ${value} 字符串拼接 --> <!-- 动态代理开发原则 1、namespace必须是接口的全路径 2、接口的方法必须与sql的id一致 3、接口的入参与parameterType类型一致 4、接口的返回值必须与resultType类型一致 --> <mapper namespace="mapper.UserMapper"> <!-- sql片段 --> <sql id="user_sql"> id, username,birthday,sex,address </sql> <select id="getUserBYId" parameterType="int" resultType="pojo.User"> select <include refid="user_sql"></include> from user where id = #{id} </select> <select id="getUSerByUserName" parameterType="String" resultType="pojo.User"> select id, username,birthday,sex,address from user where username like '%${value}%' </select> <select id="getUSerByqueryVo" parameterType="queryVo" resultType="user"> select id, username,birthday,sex,address from user where username like '%${user.username}%' </select> <select id="findUserCount" resultType="Integer"> select count(1) from user </select> <select id="getUSerByPojo" parameterType="user" resultType="pojo.User"> select id, username,birthday,sex,address from user <!--自动补上where关键字 处理多余的and 有where标签就不使用where --> <where> <if test="username !=null and username != ''"> username like '%${value}%' </if> <if test="sex !=null and sex != ''"> and sex like #{sex} </if> </where> </select> <select id="getUSerByIds" parameterType="queryVo" resultType="pojo.User"> select <include refid="user_sql"></include> from user <!--自动补上where关键字 处理多余的and 有where标签就不使用where --> <where> <!-- foreach 循环标签 --> <!--collection 要遍历的集合 --> <foreach collection="ids" open="id IN(" item="uid" separator="," close=")"> #{uid} </foreach> </where> </select> </mapper>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace命名空间,隔离sql文件 --> <!-- #{}占位符相当于jdbc的? --> <!-- ${value} 字符串拼接 --> <!-- 动态代理开发原则 1、namespace必须是接口的全路径 2、接口的方法必须与sql的id一致 3、接口的入参与parameterType类型一致 4、接口的返回值必须与resultType类型一致 --> <mapper namespace="mapper.UserMapper"> <!-- sql片段 --> <sql id="user_sql"> id, username,birthday,sex,address </sql> <select id="getUserBYId" parameterType="int" resultType="pojo.User"> select <include refid="user_sql"></include> from user where id = #{id} </select> <select id="getUSerByUserName" parameterType="String" resultType="pojo.User"> select id, username,birthday,sex,address from user where username like '%${value}%' </select> <select id="getUSerByqueryVo" parameterType="queryVo" resultType="user"> select id, username,birthday,sex,address from user where username like '%${user.username}%' </select> <select id="findUserCount" resultType="Integer"> select count(1) from user </select> <select id="getUSerByPojo" parameterType="user" resultType="pojo.User"> select id, username,birthday,sex,address from user <!--自动补上where关键字 处理多余的and 有where标签就不使用where --> <where> <if test="username !=null and username != ''"> username like '%${value}%' </if> <if test="sex !=null and sex != ''"> and sex like #{sex} </if> </where> </select> <select id="getUSerByIds" parameterType="queryVo" resultType="pojo.User"> select <include refid="user_sql"></include> from user <!--自动补上where关键字 处理多余的and 有where标签就不使用where --> <where> <!-- foreach 循环标签 --> <!--collection 要遍历的集合 --> <foreach collection="ids" open="id IN(" item="uid" separator="," close=")"> #{uid} </foreach> </where> </select> </mapper>
测试
@Test public void getUSerByIds() { SqlSession openSession = SqlSessionFactoryUtil.getSqlSessionFactory().openSession(); //获得接口实现类 UserMapper mapper = openSession.getMapper(UserMapper.class); QueryVO vo = new QueryVO(); vo.setIds(Arrays.asList(1,2,3,4,5,6)); List<User> uSerByIds = mapper.getUSerByIds(vo); for (User user : uSerByIds) { System.out.println(user); } openSession.close(); }