0

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$$550dcd02.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$$98743102.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)

here is the maven model

    <?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>

here is the controller 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));
}

and here is the respository 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();
    }
}

can someone tell me where did i go wrong at the xml? and the last here is the detail maven structure of my project in intellij...

enter image description here

2
  • Please don't post code (or text in general) as images but post actual nicely formatted text. Commented Jun 29, 2021 at 13:13
  • i have fix my question Commented Jun 29, 2021 at 13:17

1 Answer 1

1

The parameterType attribute of your insert method in the XML is set wrongly, it should be com.model.Siswa instead of java.com.model.Siswa.

Also, the parameterType attribute is optional, MyBatis can infer the parameter type from the object passed in when calling the insert method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.