首页 > 解决方案 > Error parsing SQL Mapper Configuration... Cause: java.lang.ClassNotFoundException: Cannot find class:

问题描述

Please help me to resolve this error.

There was an unexpected error (type=Internal Server Error, status=500).

Error building SqlSession. ### The error may exist in mybatis/xml/Student.xml ### Cause:

org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Student'. Cause: java.lang.ClassNotFoundException: Cannot find class: Student

and here is my project folder structure enter image description here

my Student class:

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;
       }
}

my controller class:

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

标签: spring-bootmybatis

解决方案


我通过这些步骤修复了我的错误

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

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

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

    int result = session.insert(" mybatis.model.Student .insert", student);


推荐阅读