求大神指点:为啥插入出错

tocky515 2016-06-21 11:47:14
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')

求大神们帮忙看看错在那里哇?实在太奇怪了。

...全文
478 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Die_antwort 2019-05-17
  • 打赏
  • 举报
回复
引用 4 楼 ouzhijian 的回复:
[quote=引用 1 楼 soton_dolphin 的回复:] 多了个右尖括号? <insert id="insertUser" parameterType="UserEntity"> >
[/quote]
引用 1 楼 soton_dolphin 的回复:
多了个右尖括号? <insert id="insertUser" parameterType="UserEntity"> >
老哥是真滴强
soton_dolphin 2016-06-22
  • 打赏
  • 举报
回复
多了个右尖括号? <insert id="insertUser" parameterType="UserEntity"> >
Intboy 2016-06-22
  • 打赏
  • 举报
回复
引用 1 楼 soton_dolphin 的回复:
多了个右尖括号? <insert id="insertUser" parameterType="UserEntity"> >
ouzhijian 2016-06-22
  • 打赏
  • 举报
回复
引用 1 楼 soton_dolphin 的回复:
多了个右尖括号? <insert id="insertUser" parameterType="UserEntity"> >
没事眯一会 2016-06-22
  • 打赏
  • 举报
回复
引用 1 楼 soton_dolphin 的回复:
多了个右尖括号? <insert id="insertUser" parameterType="UserEntity"> >
  • 打赏
  • 举报
回复
楼上看的真仔细,
caogang_90 2016-06-22
  • 打赏
  • 举报
回复
引用 楼主 tocky515 的回复:
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') 求大神们帮忙看看错在那里哇?实在太奇怪了。
You have an error in your SQL 这里都提示sql这块有问题了

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧