weixin_42829475 2021-03-17 17:41 采纳率: 100%
浏览 443
已采纳

mybatis 插入数据后commit无效

[DEBUG] http-bio-8080-exec-10 com.itheima.mm.controller.CourseController.addCourse(CourseController.java:44) 2021-03-17 17:28:46,755 19172ms:调用service完成业务course:Course(id=0, name=55555, icon=null, createDate=2021-03-17 17:28:46, isShow=0, userId=1, orderNo=null, catalogList=null, tagList=null, creator=null, catalogQty=null, tagQty=null, questionQty=null, title=null)
[DEBUG] http-bio-8080-exec-10 com.itheima.mm.service.impl.CourseServiceImpl.AddCourse(CourseServiceImpl.java:25) 2021-03-17 17:28:46,755 19172ms:course:Course(id=0, name=55555, icon=null, createDate=2021-03-17 17:28:46, isShow=0, userId=1, orderNo=null, catalogList=null, tagList=null, creator=null, catalogQty=null, tagQty=null, questionQty=null, title=null)
[DEBUG] http-bio-8080-exec-10 org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:137) 2021-03-17 17:28:46,758 19175ms:Opening JDBC Connection
[DEBUG] http-bio-8080-exec-10 org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:406) 2021-03-17 17:28:46,766 19183ms:Created connection 1282703172.
[DEBUG] http-bio-8080-exec-10 org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(JdbcTransaction.java:101) 2021-03-17 17:28:46,766 19183ms:Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@4c747f44]
[DEBUG] http-bio-8080-exec-10 org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:159) 2021-03-17 17:28:46,766 19183ms:==>  Preparing: insert into t_course(name, create_date, is_show, user_id) values (?,?,?,?) 
[DEBUG] http-bio-8080-exec-10 org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:159) 2021-03-17 17:28:46,768 19185ms:==> Parameters: 55555(String), 2021-03-17 17:28:46(String), 0(Integer), 1(Integer)
[DEBUG] http-bio-8080-exec-10 org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:159) 2021-03-17 17:28:46,769 19186ms:<==    Updates: 1

运行代码:

    public void AddCourse(Course course) {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        log.debug("course:{}",course);
        try {
            CourseDao courseDao=getDao(getSession(),CourseDao.class);
            Integer result=courseDao.insertCourse(course);
            if (result==0){
                throw new MmDaoException("新增失败");
            }
            commitAndCloseSession(sqlSession);
        }catch (MmDaoException e){
            log.error("",e);
            e.printStackTrace();
            rollbackAndCloseSession(sqlSession);
        }
    }

父类里的:

	protected void commitAndCloseSession(SqlSession sqlSession){
		if(sqlSession!=null) {
			sqlSession.commit();
			sqlSession.close();
		}
	}

数据库也正常连通,查询也能查到,就是sqlSession.commit()没起到应有的效果,代码也是跟着课程做的,反正就是莫名其妙,期待有大神能找出问题或者给点思路

 

 

另外我本来想试试自动提交会不会有用结果又出错了

public void AddCourse(Course course) {
        SqlSession sqlSession = SqlSessionUtils.openSessionAuto();
        log.debug("course:{}",course);
        try {
            CourseDao courseDao=getDao(getSession(),CourseDao.class);
            Integer result=courseDao.insertCourse(course);
            if (result==0){
                throw new MmDaoException("新增失败");
            }
            commitAndCloseSession(sqlSession);
        }catch (MmDaoException e){
            log.error("",e);
            e.printStackTrace();
            rollbackAndCloseSession(sqlSession);
        }
    }

public class SqlSessionUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "/mybatis-config.xml";
            InputStream inputStream = SqlSessionUtils.class.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }
    public static SqlSession openSessionAuto(){
        return sqlSessionFactory.openSession(true);
    }
}
Error:(24,48) java: 找不到符号

编译都通不过

  • 写回答

1条回答 默认 最新

  • 农夫丶果园 2021-03-17 17:46
    关注

    getSession() 方法和 SqlSessionUtils.openSession() 得到是同一个SqlSession吗?

    如果你getSession()返回了一个新的SqlSession , 你又用这个新的SqlSession调用getMapper后执行sql语句,最后用 SqlSessionUtils.openSession()得到的SqlSession去提交事务, 那当然是不行的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?