Mybatis多参数封装到map中,多条件查询

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 findByConditionByMap(HashMap map);

}

2.UserMapper.xml




   
    

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 map = new HashMap<>();
        map.put("username","a");
        map.put("sex","男");
        map.put("startIndex",0);
        map.put("pageSize",6);
        List userMapperByConditionByMap = userMapper.findByConditionByMap(map);
        for (User user1 : userMapperByConditionByMap) {
            System.out.println(user1);
        }
        sqlSession.close();
    }
}

4.测试结果

Mybatis多参数封装到map中,多条件查询_第1张图片

你可能感兴趣的:(多条件查询)