0
The phenomenon and background of the problem encountered

Problems encountered when configuring mybatis and writing tests, an error will be reported as soon as you click to run

problem related code,
 @Test
    public  void findmany() throws IOException
    {
        InputStream is= Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession =sqlSessionFactory.openSession();


        Map<String,Object> params = new HashMap<>();
        params.put("name", "sam");
        params.put("major", "");

        List<Student> student=sqlSession.selectList("com.javaee.pojo.Student.findmany",params);
        System.out.println(student);

        Map<String,Object> params2 = new HashMap<>();
        params2.put("name", "");
        params2.put("major", "math");

        student=sqlSession.selectList("com.javaee.pojo.Student.findmany",params2);
        System.out.println(student);

        Map<String,Object> params3 = new HashMap<>();
        params3.put("name", "");
        params3.put("major", "");

        student=sqlSession.selectList("com.javaee.pojo.Student.findmany",params3);
        System.out.println(student);

        sqlSession.close();
    }


mapper


    <select id="findmany"
            parameterType="map"
            resultType="com.javaee.pojo.Student">

        select * from students where name like concat('%',#{name},'%')  major like concat('%',#{major},'%')

    </select>

Student Class

public class Student {
    private int id;        
    private String name;    
    private String major;    
    private String sno;       

    public String toString()
    {
        return "Student{"+"id="+id+",sno='"+sno+'\''+",name='"+name+'\''+",major='"+major+'\''+'}';



    }


Running results and error content

enter image description here

1 Answer 1

1

Missing an AND in your select, try this way:

select * from students where name like concat('%',#{name},'%') AND major like concat('%',#{major},'%')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.