Spring与Mybatis的整合

Spring与Mybatis的整合,第1张

Spring与Mybatis的整合

Spring与Mybatis整合避免了解数据库时重复创建工厂,可将工厂的创建交给Spring管理

原始:

public void save(Account account) {

        

        try {
            InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sessionFactory.openSession();
            AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
            mapper.save(account);
            sqlSession.commit();
            sqlSession.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

整合后:

@Autowired
private AccountMapper accountMapper;

public void save(Account account) {

        accountMapper.save(account);

    }

需要在pom中导入


  org.mybatis
  mybatis-spring
  1.3.1

接下来需要在applicationContext.xml中配置

   
    
        
        
        
        
    

    
    
        
        
    

    
    
        
    

sqlMapConfig将会变的更加简洁

原始:





    
    
       
    

    
        
            
            
                
                
                
                
            
        
    
    
        
    




    
    
        
    

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5435045.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存