I have been trying to insert a Student record in mysql database, using spring 3 & mybatis3, but i am constantly getting following exception.
com.ibatis.common.jdbc.exception.NestedSQLException:
ERROR [stderr] (http-localhost/127.0.0.1:8080-4) --- The error occurred in /config/StudentMapper.xml.
ERROR [stderr] (http-localhost/127.0.0.1:8080-4) --- The error occurred while applying a parameter map.
ERROR [stderr] (http-localhost/127.0.0.1:8080-4) --- Check the insertStudent-InlineParameterMap.
ERROR [stderr] (http-localhost/127.0.0.1:8080-4) --- Check the statement (update failed).
ERROR [stderr] (http-localhost/127.0.0.1:8080-4) --- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'lastName)' at line 1
Please Help me resolve this exception.
sqlMapConfig.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ift.tt/1sCAYo5">
<sqlMapConfig>
<properties resource="/config/SqlMapConfigExample.properties"/>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
useStatementNamespaces="false" />
<transactionManager type="JDBC" commitRequired="false">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="${driver}" />
<property name="JDBC.ConnectionURL" value="${url}" />
<property name="JDBC.Username" value="${username}" />
<property name="JDBC.Password" value="${password}" />
<property name="JDBC.DefaultAutoCommit" value="true"/>
</dataSource>
</transactionManager>
<sqlMap resource="/config/StudentMapper.xml" />
</sqlMapConfig>
My StudentMapper.xml-
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ift.tt/1quhcNG">
<sqlMap namespace="com.fulcrum.mappers.StudentMapper">
<resultMap id="result" class="com.fulcrum.model.Student">
<result property="id" column="id" />
<result property="firstName" column="firstName" />
<result property="lastName" column="lastName" />
</resultMap>
<insert id="insertStudent" parameterClass="com.fulcrum.model.Student">
<![CDATA[
INSERT INTO student (firstName, lastName) VALUES (#firstName,#lastName);
]]>
<selectKey keyProperty="id" >
SELECT LAST_INSERT_ID() AS id
</selectKey>
</insert>
</sqlMap>
My insertStudentCode in daoimpl-
public void insertStudent(Student student) {
System.out.println("UserDaoImpl insertStudent()");
System.out.println("Student Name::" +student.getFirstName()+" "+student.getLastName());
/*New*/
String resource = "/config/SqlMapConfig.xml";
Reader reader;
try {
reader = Resources.getResourceAsReader (resource);
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
System.out.println("SqlMap::::" +sqlMap);
// sqlMap.startTransaction();
sqlMap.insert("insertStudent", student);
// sqlMap.commitTransaction();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
My Student Class Code-
package com.fulcrum.model;
public class Student {
private Long id;
private String firstName;
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Aucun commentaire:
Enregistrer un commentaire