栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring与MyBatis整合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring与MyBatis整合

一、问题提出

mybatis是根据mapper.xml文件动态生成mapper接口的实现类,按照Spring的常规组件扫描的方式不能添加到IOC容器中,需要做出相应的配置


二、解决方案

图片摘自尚硅谷尚筹网项目文档。

如图可知,需要在Spring和mybatis整合的配置文件中配置

启动Spring组件扫描数据源SqlsessionFactoryBeanMapperScannerConfigurer 三、MyBatis逆向工程 pom文件

    
        
            org.mybatis
            mybatis
            3.5.9
        
    

    
    

        
        
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.4.1
                
                
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.4.1
                    

                    
                        com.mchange
                        c3p0
                        0.9.5.5
                    

                    
                        mysql
                        mysql-connector-java
                        8.0.25
                    
                

            
        
    
generatorConfig.xml



    
    
        
            
            
        
        
        
        
        
        
            
        
        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        
        
         
生成结果: 

 把生成的文件移动到对应的工程文件中

四、Spring与mybatis的配置文件 主模块依赖jar包


        
            org.springframework
            spring-orm
            5.3.16
        

        
            org.springframework
            spring-webmvc
            5.3.16
        

        
            mysql
            mysql-connector-java
            8.0.25
        

        
            org.mybatis
            mybatis
            3.5.9
        

        
            com.alibaba
            druid
            1.2.8
        

        
            org.mybatis
            mybatis-spring
            2.0.7
        

    
spring-persist-mybatis.xml



    
    

    
    

    
    
        
        
        
        
        
    

    
    
        
        
        
        
        
        
    

    
    
        
        
    
 文件目录

五、测试
//junit与Spring整合
@RunWith(SpringJUnit4ClassRunner.class)
//加载Spring配置文件
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml"})
public class DataSourceTest {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private LoginMapper loginMapper;

    @Test
    public void connTest() throws SQLException {
        System.out.println(dataSource.getConnection());
    }

    @Test
    public void insertTest(){
        Login login = new Login(2, "wdh", "123", false);
        int i = loginMapper.insert(login);
        System.out.println("受影响的行数:"+ i);
    }
}
 结果:

 

 六、总结

 

弱小和无知不是生存的障碍,傲慢才是

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号