标签:开发 用户 png 构造 XML out session sel ssi
<!-- 传递pojo综合查询用户信息 --> <select id="findUserList" parameterType="user" resultType="user"> select * from user where 1=1 <if test="id!=null and id!=‘‘"> and id=#{id} </if> <if test="username!=null and username!=‘‘"> and username like ‘%${username}%‘ </if> </select>
<select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <if test="id!=null and id!=‘‘"> and id=#{id} </if> <if test="username!=null and username!=‘‘"> and username like ‘%${username}%‘ </if> </where> </select>
mapper.xml
<if test="ids!=null and ids.size>0"> <foreach collection="ids" open=" and id in(" close=")" item="id" separator="," > #{id} </foreach> </if>
测试代码:
List<Integer> ids = new ArrayList<Integer>(); ids.add(1);//查询id为1的用户 ids.add(10); //查询id为10的用户 queryVo.setIds(ids); List<User> list = userMapper.findUserList(queryVo);
<select id="selectUserByList" parameterType="java.util.List" resultType="com.lhx.mybatis.po.User"> select * from user <where> <!-- 传递List,List中是pojo --> <if test="list!=null"> <foreach collection="list" item="item" open="and id in(" separator="," close=")"> #{item.id} </foreach> </if> </where> </select>
mapper接口
public List<User> selectUserByList(List<User> userlist) throws Exception;
测试类
public void testselectUserByList() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); // 构造查询条件List List<User> userlist = new ArrayList<User>(); User user = new User(); user.setId(1); userlist.add(user); user = new User(); user.setId(2); userlist.add(user); // 传递userlist列表查询用户列表 List<User> list = userMapper.selectUserByList(userlist); System.out.println(list.size()+""); // 关闭session session.close(); }
<!-- 传递数组综合查询用户信息 --> <select id="selectUserByArrayPojo" parameterType="Object[]" resultType="com.lhx.mybatis.po.User"> select * from user <where> <!-- 传递数组 --> <if test="array!=null"> <foreach collection="array" index="index" item="item" open="and id in(" separator="," close=")"> #{item.id} </foreach> </if> </where> </select>
mapper接口
public List<User> selectUserByArrayPojo(Object[] userlist) throws Exception;
测试代码
public void testselectUserByArray() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); // 构造查询条件List Object[] userlist = new Object[2]; User user = new User(); user.setId(1); userlist[0] = user; user = new User(); user.setId(2); userlist[1] = user; // 传递user对象查询用户列表 List<User> list = userMapper.selectUserByArrayPojo(userlist); // 关闭session session.close(); }
<!-- 传递数组综合查询用户信息 --> <select id="selectUserByArray" parameterType="Object[]" resultType="com.lhx.mybatis.po.User"> select * from user <where> <!-- 传递数组 --> <if test="array!=null"> <foreach collection="array" index="index" item="item" open="and id in(" separator="," close=")"> #{item} </foreach> </if> </where> </select>
mapper接口
public List<User> selectUserByArray(Object[] userlist) throws Exception;
测试代码
public void testselectUserByArray() throws Exception { // 获取session SqlSession session = sqlSessionFactory.openSession(); // 获限mapper接口实例 UserMapper userMapper = session.getMapper(UserMapper.class); // 构造查询条件List Object[] userlist = new Object[2]; userlist[0] = "1"; userlist[1] = "2"; // 传递user对象查询用户列表 List<User> list = userMapper.selectUserByArray(userlist); // 关闭session session.close(); }
<!-- 传递pojo综合查询用户信息 --> <select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <if test="id!=null and id!=‘‘"> and id=#{id} </if> <if test="username!=null and username!=‘‘"> and username like ‘%${username}%‘ </if> </where> </select>
<sql id="query_user_where"> <if test="id!=null and id!=‘‘"> and id=#{id} </if> <if test="username!=null and username!=‘‘"> and username like ‘%${username}%‘ </if> </sql>
<select id="findUserList" parameterType="user" resultType="user"> select * from user <where> <include refid="query_user_where"/> </where> </select>
java-mybaits-00402-Mapper-动态sql
标签:开发 用户 png 构造 XML out session sel ssi
原文地址:http://www.cnblogs.com/bjlhx/p/6838294.html