C_Menethil_L
C_Menethil_L

Reputation: 1

java.sql.SQLSyntaxErrorException in mybatis

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

Upvotes: 0

Views: 191

Answers (1)

Evgeni Enchev
Evgeni Enchev

Reputation: 552

Missing an AND in your select, try this way:

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

Upvotes: 1

Related Questions