1.UserMapper接口
package com.william.dao;
import com.william.domain.QueryValueObject;
import com.william.domain.User;
import java.util.List;
public interface UserMapper {
/**
* 多参数封装到map中,多条件查询
* @param map
* @return
*/
List<User> findByConditionByMap(HashMap<String, Object> map);
}
2.UserMapper.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">
<mapper namespace="com.william.dao.UserMapper">
<!--多参数封装到map中,多条件查询-->
<select id="findByConditionByMap" parameterType="map" resultType="user">
select * from user where username like "%"#{username}"%" and sex=#{sex}
limit #{startIndex},#{pageSize}
</select>
</mapper>
3.TestCrud测试类
package com.william;
import com.william.dao.UserMapper;
import com.william.domain.QueryValueObject;
import com.william.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2019/7/12 10:16
* @description :
* @version: 1.0
*/
public class TestCrud {
/**
* 多参数封装到map中,多条件查询
* @throws IOException
*/
@Test
public void findByConditionByMap() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("Mybatis-configuration.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取某接口的动态代理对象(获取某接口的一个实现类)
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
QueryValueObject queryValueObject = new QueryValueObject();
HashMap<String, Object> map = new HashMap<>();
map.put("username","a");
map.put("sex","男");
map.put("startIndex",0);
map.put("pageSize",6);
List<User> userMapperByConditionByMap = userMapper.findByConditionByMap(map);
for (User user1 : userMapperByConditionByMap) {
System.out.println(user1);
}
sqlSession.close();
}
}
4.测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a3a3af97c6288f474229de338af177d5.png)