weixin_42829475
weixin_42829475
采纳率100%
2021-03-17 17:41 阅读 85
已采纳

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条回答 默认 最新

  • 已采纳
    qq_38167579 农夫丶果园 2021-03-17 17:46

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

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

    点赞 评论 复制链接分享

相关推荐