Mybatis java 解析错误 SQL 映射器配置
My batis java Error parsing SQL Mapper Configuration
So i'm creating CRUD Spring Using Mybatis, when i run the application, it gives me error like this
### Error building SqlSession.
### The error may exist in mybatis/Student.xml
### The error occurred while processing mapper_resultMap[result]
### 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 'java.com.model.Siswa'. Cause: java.lang.ClassNotFoundException: Cannot find class: java.com.model.Siswa
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:54)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:38)
at com.repository.SiswaRepo.saveSiswa(SiswaRepo.java:51)
at com.repository.SiswaRepo$$FastClassBySpringCGLIB$0dcd02.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
at com.repository.SiswaRepo$$EnhancerBySpringCGLIB$743102.saveSiswa(<generated>)
at com.controller.SiswaController.inputSiswa(SiswaController.java:39)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
这里是maven模型
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "Siswa">
<insert id = "insert" parameterType = "java.com.model.Siswa">
INSERT INTO siswas (nama, alamat, status) VALUES (#{fullname}, #{address},
#{status});
INSERT INTO nilai (biologi, physics, calculus) VALUES (#{biologi}, #{fisika},
#{kalkulus});
<selectKey keyProperty = "id" resultType = "int" order = "AFTER">
select last_insert_id() as id
</selectKey>
</insert>
这里是控制器class
@RequestMapping(value = "/input", method = RequestMethod.POST)
public ResponseEntity<?> inputSiswa(@RequestBody JSONObject jobj) throws ParseException, IIOException {
Adapter adapter = new Adapter();
ArrayList<Siswa> array = new ArrayList<>();
array = adapter.buatSiswadariJSON(jobj);
for (int i = 0; i <array.size();i++) {
siswaRepository.saveSiswa(array.get(i));
}
return (new ResponseEntity<>("data has been inputed", HttpStatus.CREATED));
}
这是存储库 class
@Override
public void saveSiswa(Siswa siswa) throws IIOException {
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
//select contact all contacts
String nama = siswa.getFullname();
String address = siswa.getAddress();
String status = siswa.getStatus();
float biologi = siswa.getBiologi();
float fisika = siswa.getFisika();
float kalkulus = siswa.getKalkulus();
Siswa siswa1 = new Siswa(nama,address,status );
session.insert("Student.insert", siswa1);
Siswa siswa2 = new Siswa(biologi,fisika,kalkulus);
session.insert("Student.insert", siswa2);
System.out.println("Records Read Successfully ");
session.commit();
session.close();
}catch (Exception e){
e.printStackTrace();
}
}
谁能告诉我 xml 哪里出错了?最后这里是我在 intellij 中的项目的详细 Maven 结构...
你在XML中插入方法的parameterType
属性设置错误,应该是com.model.Siswa
而不是java.com.model.Siswa
。
另外,parameterType
属性是可选的,MyBatis在调用insert方法时可以从传入的对象中推断出参数类型。
So i'm creating CRUD Spring Using Mybatis, when i run the application, it gives me error like this
### Error building SqlSession.
### The error may exist in mybatis/Student.xml
### The error occurred while processing mapper_resultMap[result]
### 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 'java.com.model.Siswa'. Cause: java.lang.ClassNotFoundException: Cannot find class: java.com.model.Siswa
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:54)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:38)
at com.repository.SiswaRepo.saveSiswa(SiswaRepo.java:51)
at com.repository.SiswaRepo$$FastClassBySpringCGLIB$0dcd02.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
at com.repository.SiswaRepo$$EnhancerBySpringCGLIB$743102.saveSiswa(<generated>)
at com.controller.SiswaController.inputSiswa(SiswaController.java:39)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
这里是maven模型
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "Siswa">
<insert id = "insert" parameterType = "java.com.model.Siswa">
INSERT INTO siswas (nama, alamat, status) VALUES (#{fullname}, #{address},
#{status});
INSERT INTO nilai (biologi, physics, calculus) VALUES (#{biologi}, #{fisika},
#{kalkulus});
<selectKey keyProperty = "id" resultType = "int" order = "AFTER">
select last_insert_id() as id
</selectKey>
</insert>
这里是控制器class
@RequestMapping(value = "/input", method = RequestMethod.POST)
public ResponseEntity<?> inputSiswa(@RequestBody JSONObject jobj) throws ParseException, IIOException {
Adapter adapter = new Adapter();
ArrayList<Siswa> array = new ArrayList<>();
array = adapter.buatSiswadariJSON(jobj);
for (int i = 0; i <array.size();i++) {
siswaRepository.saveSiswa(array.get(i));
}
return (new ResponseEntity<>("data has been inputed", HttpStatus.CREATED));
}
这是存储库 class
@Override
public void saveSiswa(Siswa siswa) throws IIOException {
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
//select contact all contacts
String nama = siswa.getFullname();
String address = siswa.getAddress();
String status = siswa.getStatus();
float biologi = siswa.getBiologi();
float fisika = siswa.getFisika();
float kalkulus = siswa.getKalkulus();
Siswa siswa1 = new Siswa(nama,address,status );
session.insert("Student.insert", siswa1);
Siswa siswa2 = new Siswa(biologi,fisika,kalkulus);
session.insert("Student.insert", siswa2);
System.out.println("Records Read Successfully ");
session.commit();
session.close();
}catch (Exception e){
e.printStackTrace();
}
}
谁能告诉我 xml 哪里出错了?最后这里是我在 intellij 中的项目的详细 Maven 结构...
你在XML中插入方法的parameterType
属性设置错误,应该是com.model.Siswa
而不是java.com.model.Siswa
。
另外,parameterType
属性是可选的,MyBatis在调用insert方法时可以从传入的对象中推断出参数类型。