spring-boot - 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
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
Student.xml
解决方案
我通过这些步骤修复了我的错误
在 Student.xml 中,我将 Student parameterType 替换为完整的 classPath:
<mapper namespace="mybatis.model.Student">
和
<insert id = "insert" parameterType="mybatis.model.Student">
然后在 mybatisController 中执行相同的操作,调用 mapper 的插入 ID:
int result = session.insert(" mybatis.model.Student .insert", student);
推荐阅读
- java - 使用 XML 时 Eclipse 中没有默认建议
- java - 如何修复 String 类型不适用于我的方法中的参数 boolean?
- android - 无法在 Android Studio 中设置 NDK 的路径
- r - 如果选择列中的所有值都返回 NA 作为结果,则删除数据框中的行
- javascript - 如何将时间从 MySQL 转换为 JS,将其复制并输出到控制台
- javascript - D3 - 检索与图表中单击的元素关联的数据
- netsuite - 将约会标记为已完成时,您可以在 NetSuite 中执行脚本/工作流吗?
- java - 运行时将 jpanels 切换为 jframe 内容窗格
- python - 无法从芹菜工人连接到 RabbitMQ - 403 ACCESS_REFUSED - 烧瓶
- c# - 使用 System.Data.SqlClient 将对称密钥传递给调用 exec sp_executesql 的 SQL Server 存储过程的正确方法