MyBatis 的insert方法执行错误

Certone_qrb 2015-08-24 04:11:43
错误代码

org.apache.ibatis.exceptions.PersistenceException:

### Error updating database. 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 '·

insert into user(username,birthday,sex,address) va' at line 1

### The error may involve test.insertUser-Inline

### The error occurred while setting parameters

### SQL: · insert into user(username,birthday,sex,address) values(?,?,?,?)

### 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 '·

insert into user(username,birthday,sex,address) va' at line 1

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26)

at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:154)

at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:141)

at first.MyBatisFirst.insertUser(MyBatisFirst.java:78)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

at org.junit.runners.ParentRunner.run(ParentRunner.java:292)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Caused by: 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 '·

insert into user(username,birthday,sex,address) va' at line 1

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)

at com.mysql.jdbc.Util.getInstance(Util.java:381)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)

at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1021)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:62)

at com.sun.proxy.$Proxy5.execute(Unknown Source)

at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:44)

at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:69)

at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48)

at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105)

at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71)

at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152)

... 25 more



测试方法

//插入用户数据
//测试有问题!
@Test
public void insertUser() {
String resource = "sqlMapConfig.xml";
InputStream is = null;
try {
is = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
User user = new User();
user.setUsername("qiuruibin");
user.setBirthday(new Date());
user.setSex("male");
user.setAddress("ShangHai");
session.insert("test.insertUser", user);
//提交事务
session.commit();
session.close();
//获取用户信息自增的主键(mysql自增主键返回)
//在insert执行之前自动生成一个自增主键
System.out.println(user.getId());
}


配置文件:

<!-- 添加用户 -->

<!-- MyBatis通过OGNL来获取入参对象的属性值 -->

<!-- OGNL:Object-Graph Navigation Language -->

<insert id="insertUser" parameterType="pojo.User" statementType="PREPARED" keyProperty="id" useGeneratedKeys="true">

insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})

</insert>
...全文
944 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Certone_qrb 2015-08-24
  • 打赏
  • 举报
回复
引用 2 楼 ZMLXS 的回复:
### SQL: · insert into user(username,birthday,sex,address) values(?,?,?,?) ### 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 '· insert into user(username,birthday,sex,address) va' at line 1 报错误, at line 1,根据报的错误发现前面多了一个 .(点), 但是看你的配置文件没有这个 .(点),不知道是不是你去掉了, 建立你清除一下项目, 然后重启在试试。整体看上去没有问题。
我前面做了蛮多注释的,应该都打上注视了,我把这些注释全删了就跑通了,做注释的地方可能有问题!我正在查。
Certone_qrb 2015-08-24
  • 打赏
  • 举报
回复
引用 3 楼 qq_30650591 的回复:
楼主问题解决了么?
解决了,如上楼、
Certone_qrb 2015-08-24
  • 打赏
  • 举报
回复
引用 1 楼 lzy_lizhiyang 的回复:
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) values后边加上空格
你这个方法不行!
  • 打赏
  • 举报
回复
楼主问题解决了么?
ZMLXS 2015-08-24
  • 打赏
  • 举报
回复
### SQL: · insert into user(username,birthday,sex,address) values(?,?,?,?) ### 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 '· insert into user(username,birthday,sex,address) va' at line 1 报错误, at line 1,根据报的错误发现前面多了一个 .(点), 但是看你的配置文件没有这个 .(点),不知道是不是你去掉了, 建立你清除一下项目, 然后重启在试试。整体看上去没有问题。
在远行的路上 2015-08-24
  • 打赏
  • 举报
回复
insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address}) values后边加上空格

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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