I am using mybatis framework in a servlet and the mybatis-config.xml file is unable to find the Student_mapper class.I applied all the paths including the package name and excluding it and also used the element in my mybatis-config.xml but it doesn’t work.I am still getting the same error.
Error building SqlSession.The error may exist in SQL Mapper Configuration Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.ClassNotFoundException: Cannot find class: mybatis/Student_mapper org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:52) org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:36) mybatis.Serv.doGet(Serv.java:49) javax.servlet.http.HttpServlet.service(HttpServlet.java:626) javax.servlet.http.HttpServlet.service(HttpServlet.java:733) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Here’s my mybatis-config.xml file
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="mybatis.Student_mapper" alias="Student_mapper" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/chitra" /> <property name="username" value="postgres" /> <property name="password" value="admin" /> </dataSource> </environment> </environments> <mappers> <mapper class="mybatis/Student_mapper" /> </mappers> </configuration>
Here’s my Student_mapper interface
package mybatis; import java.util.List; import org.apache.ibatis.annotations.*; public interface Student_mapper { final String getAll = "SELECT * FROM STUDENT"; final String getById = "SELECT * FROM STUDENT WHERE ID = #{id}"; final String deleteById = "DELETE from STUDENT WHERE ID = #{id}"; final String insert = "INSERT INTO STUDENT (ID,NAME, COURSE, ROLL) VALUES (#{id},#{name}, #{course}, #{roll})"; final String update = "UPDATE STUDENT SET NAME = #{name}, COURSE = #{course}, ROLL = #{roll} WHERE ID = #{id}"; @Select(getAll) @Results(value = { @Result(property = "id", column = "ID"), @Result(property = "name", column = "NAME"), @Result(property = "course", column = "COURSE"), @Result(property = "roll", column = "ROLL") }) List<Student> getAll(); @Select(getById) @Results(value = { @Result(property = "id", column = "ID"), @Result(property = "name", column = "NAME"), @Result(property = "course", column = "COURSE"), @Result(property = "roll", column = "ROLL") }) Student getById(int id); @Update(update) void update(Student student); @Delete(deleteById) void delete(int id); @Insert(insert) //@Options(useGeneratedKeys = true, keyProperty = "id") void insert(Student student); }
Advertisement
Answer
The mapper class needs to be a valid Java class name. Change your mybatis-config.xml to the following:
<mappers> <mapper class="mybatis.Student_mapper" /> </mappers>