求大神指点:为啥插入出错
1、用数据库版本为:mysql-5.7.13-winx64,通过mybatis插入数据。
2、数据库中创建的表为:
DROP TABLE IF EXISTS `hrm_user`;
CREATE TABLE `hrm_user` (
`ID` int(11) NOT NULL,
`UserID` varchar(11) DEFAULT NULL,
`UserName` varchar(255) DEFAULT NULL,
`UserPassword` varchar(255) DEFAULT NULL,
`UserPhone` varchar(32) DEFAULT NULL,
`UserEmail` varchar(255) DEFAULT NULL,
`UserAddress` varchar(255) DEFAULT NULL,
`UserNativePlace` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3、映射文件中写的插入语句为,
<insert id="insertUser" parameterType="UserEntity"> >
insert into hrm_user (ID, UserID, UserName, UserPassword)
values (#{id, jdbcType=INTEGER}, #{userID, jdbcType=VARCHAR}, #{userName, jdbcType=VARCHAR}, #{userPassword, jdbcType=VARCHAR})
</insert>
4、插入时的代码为:
//使用"spring.xml"和"spring-mybatis.xml"这两个配置文件创建Spring上下文
appContext = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
//从Spring容器中根据bean的id取出我们要使用的userService对象
userService = (IUserService) appContext.getBean("userService");
//创建实体
user = new UserEntity();
user.setId(14);
user.setUserID("00000012");
user.setUserName("cxx");
user.setUserPassword("cxx");
user.setUserPhone("13700000007");
user.setUserEmail("cxx163.com");
user.setUserAddress("11");
user.setUserNativePlace("11");
//执行插入。
userService.addUser(user);
5、服务对象UserService,注入了DAO层的接口IUserDao
@Service("userService")
public class UserServiceImpl implements IUserService {
/**
* 使用@Autowired注解标注userDao变量。
* 当需要使用userDao时,Spring就会自动注入,是按类型注入的。
*/
@Autowired
IUserDao userDao;
/* (non-Javadoc)
* @see com.tocky.demo.data.service.IUserService#getUserByID(java.lang.String)
*/
public UserEntity getUserByID(int userID) throws Exception {
UserEntity userEntity;
userEntity = userDao.selectUserByID(userID);
return userEntity;
}
/* (non-Javadoc)
* @see com.tocky.demo.data.service.IUserService#updateUser(java.lang.String)
*/
public void addUser(UserEntity userEntity) throws Exception {
userDao.insertUser(userEntity);
}
}
6、DAO层接口IUserDao
public interface IUserDao {
UserEntity selectUserByID(int id) throws Exception;
int insertUser(UserEntity userEntity) throws Exception;
}
7、执行时,报如下错误,
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: 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 '>
insert into hrm_user (ID, UserID, UserName, UserPassword)
values (1' at line 1
### The error may involve com.tocky.demo.data.dao.userdao.IUserDao.insertUser-Inline
### The error occurred while setting parameters
### SQL: > insert into hrm_user (ID, UserID, UserName, UserPassword) values (?, ?, ?, ?)
### Cause: java.sql.SQLSyntaxErrorException: 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 '>
insert into hrm_user (ID, UserID, UserName, UserPassword)
values (1' at line 1
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: 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 '>
insert into hrm_user (ID, UserID, UserName, UserPassword)
values (1' at line 1
java.sql.SQLSyntaxErrorException: 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 '>
insert into hrm_user (ID, UserID, UserName, UserPassword)
values (1' at line 1
### Error updating database. Cause: java.sql.SQLSyntaxErrorException: 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 '>
insert into hrm_user (ID, UserID, UserName, UserPassword)
values (1' at line 1
### The error may involve com.tocky.demo.data.dao.userdao.IUserDao.insertUser-Inline
### The error occurred while setting parameters
### SQL: > insert into hrm_user (ID, UserID, UserName, UserPassword) values (?, ?, ?, ?)
### Cause: java.sql.SQLSyntaxErrorException: 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 '>
insert into hrm_user (ID, UserID, UserName, UserPassword)
values (1' at line 1
; bad SQL grammar []
[]
8、在报错中的SQL语句放到Navicat for MySQL中执行又不会报错。
insert into hrm_user (ID, UserID, UserName, UserPassword) values (14, '00000012', 'cxx', 'cxx')
求大神们帮忙看看错在那里哇?实在太奇怪了。