java.sql.SQLSyntaxErrorException
问题遇到的现象和发生背景
在进行mybatis配置和编写测试时遇到的问题,一点击运行就报错
问题相关代码
下面是测试类的代码
@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", "张三");
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", "计算机");
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类的部分有关代码
public class Student { private int id; //主键id
private String name; // 姓名
private String major; // 专业
private String sno; // 学号
public String toString()
{
return "Student{"+"id="+id+",sno='"+sno+'\''+",name='"+name+'\''+",major='"+major+'\''+'}';
}
运行结果及报错内容
回答:
major like
前面少了个and
回答:
select * from students where name like concat('%',#{name},'%') and major like concat('%',#{major},'%')
以上是 java.sql.SQLSyntaxErrorException 的全部内容, 来源链接: utcz.com/p/944370.html