解析SQL Mapper配置时出错...原因:java.lang.ClassNotFoundException:找不到类:

时间:2019-05-15 08:33:12

标签: spring-boot mybatis

请帮助我解决此错误。

  

发生意外错误(类型=内部服务器错误,状态= 500)。

     

建立SqlSession时出错。 ###错误可能存在于mybatis / xml / Student.xml ###原因:

     

org.apache.ibatis.builder.BuilderException:解析SQL Mapper时出错   组态。原因:org.apache.ibatis.builder.BuilderException:   解析Mapper XML时出错。原因:   org.apache.ibatis.builder.BuilderException:解决类时出错。   原因:org.apache.ibatis.type.TypeException:无法解析类型   别名“学生”。原因:java.lang.ClassNotFoundException:找不到   班:学生

这是我的项目文件夹结构 enter image description here

我的学生班:

package mybatis.model;

public class Student {
private int id;
private String name;
private String branch;
private int percentage;
private int phone;
private String email;

    public Student(String name, String branch, int percentage, int phone, String email) {
          super();
          this.name = name;
          this.branch = branch;
          this.percentage = percentage;
          this.phone = phone;
          this.email = email;
       }
}

我的控制器类:

package mybatis.controller;
import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import mybatis.model.Student;

@RestController
public class mybatisController {

    @GetMapping("/")
    public String home() throws IOException {
        Reader reader = Resources.getResourceAsReader("mybatis/xml/SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); 
        SqlSession session = sqlSessionFactory.openSession();

        //Create a new student object
        Student student = new Student("Mohammad","It", 80, 984803322, "Mohammad@gmail.com" ); 

        //Insert student data      
        Integer result = session.insert("Student.insert", student);
        return result>0 ? "inserted":"fail";
    }
}

SqlMapConfig.xml

enter image description here

Student.xml

enter image description here

1 个答案:

答案 0 :(得分:0)

我通过以下步骤纠正了错误

在Student.xml中,将Student parameterType替换为完整的classPath:

<mapper namespace="mybatis.model.Student">

<insert id = "insert" parameterType="mybatis.model.Student">
  1. 然后在mybatisController中执行相同的操作,在其中调用映射器的插入ID:

    int结果= session.insert(“ mybatis.model.Student .insert”,学生);