MyBatis+MySql Insert操作失败解决办法

[转]MyBatis+MySql Insert操作失败解决办法

2013-5-21阅读238 评论0

初次接触MyBatis,遇到数据库使用MySql时Insert失败的问题(select时正常)。在日志中复制SQL语句是可以执行的,不知道什么原因,怀疑是事务问题。

google后看到一哥们也遇到了此问题,不过他找到了一个解决办法,下面是他原话(他的症状和我遇到的一样,不过我不知道哪里有问题,他知道是数据库引擎的问题~):

The Engine in MySql is innoDB. Which retrieves the data from the table successfully, but i cant insert the data, i cant get any exception or error.

which works fine in MyISAM type.

有他这段信息就好办了,下面是我的2个解决办法:

方法1:把数据库表都改成了MyISAM,果然数据插入成功了!

方法2:数据库引擎不变

搜了一些资料,看到说 innoDB是自动提交事务的,而MyBatis管理事务时默认不提交。

资料地址:

http://tech.b3log.org/articles/2012/05/29/1338271764507.html讲述innoDB和MyISAM的区别及MyBatis管理事务时默认不提交。

http://sishuok.com/forum/blogPost/list/3897.html有openSession方法调用的 

openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)方法的原型。

 看到openSession有autoCommit这个参数,那这样调用(openSession(true))就OK了。测试成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
项目分为两个工程domain和manager,工程结构如下图所示,其中domain是Maven java工程主要完成对数据库的操作,manager是Maven Web工程,完成web访问 SQL脚本 /* Navicat MySQL Data Transfer Source Server : MySQL Source Server Version : 50022 Source Host : 127.0.0.1:3306 Source Database : zhaochao Target Server Type : MYSQL Target Server Version : 50022 File Encoding : 65001 Date: 2014-09-18 20:16:10 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(11) NOT NULL auto_increment, `user_name` varchar(250) default NULL, `user_passwd` varchar(250) default NULL, `user_email` varchar(250) default NULL, `user_type` int(11) default NULL, `create_time` datetime default NULL, `update_time` datetime default NULL, PRIMARY KEY (`id`), KEY `FK_Reference_1` (`user_type`), CONSTRAINT `FK_Reference_1` FOREIGN KEY (`user_type`) REFERENCES `t_user_type` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES ('1', 'admin', 'admin', '[email protected]', '1', '2014-09-18 14:26:26', '2014-09-18 14:26:56'); INSERT INTO `t_user` VALUES ('2', 'a', 'a', '[email protected]', '1', '2014-09-15 17:15:53', '2014-09-18 19:59:09'); INSERT INTO `t_user` VALUES ('5', 'b', 'b', '[email protected]', '1', '2014-09-18 20:01:06', '2014-09-18 20:04:38'); -- ---------------------------- -- Table structure for t_user_type -- ---------------------------- DROP TABLE IF EXISTS `t_user_type`; CREATE TABLE `t_user_type` ( `id` int(11) NOT NULL auto_increment, `type_name` varchar(250) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_user_type -- ---------------------------- INSERT INTO `t_user_type` VALUES ('1', '管理员'); INSERT INTO `t_user_type` VALUES ('2', '经理'); INSERT INTO `t_user_type` VALUES ('3', '主管');

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值