SpringBoot整合Mybatis踩坑记

SpringBoot整合MyBatis

        • 1、项目搭建,新建SpringBoot项目:
        • 2、整合MyBatis
        • 3、Could not autowire. No beans of 'OrdersMapper' type found. Inspection info:Checks autowiring problems in a bean class.

  • 最近在研究小程序开发,顺便又把之前落下的框架拿出来熟悉一下。由于公司一直使用的SSM,所以也没用到其它框架。前几天朋友让研究一下并发,顺便想搭建一个新项目试试,才发现自己活回去了。一路踩坑,哈哈。记录一下吧!

  • 我就从零开始架构搭建,新入坑的小伙伴也可以参考一下。前段时间研究了一下MyBatis-generator代码生成器,发现了MyBatis-plus要高级好用的多,发现自己还一直活在梦里哎,卑微码畜在线卖惨。新入坑的小伙伴可以学习一下。本来想用MyBatis-plus的。但是那个东西对于我也是新东西。等我把这个写完后面有时间再出一篇整合MyBatis-plus的博客吧。

1、项目搭建,新建SpringBoot项目:

  • 新建项目、第一步配置默认。
    SpringBoot整合Mybatis踩坑记_第1张图片

  • 填写基础包名
    SpringBoot整合Mybatis踩坑记_第2张图片

  • 选择几个默认的依赖,也可以不选。lombok是这几个基本是项目需要。具体作用可自己去了解。
    SpringBoot整合Mybatis踩坑记_第3张图片

  • 然后填项目名,finish,一个活生生的SpringBoot项目就建好了。
    SpringBoot整合Mybatis踩坑记_第4张图片

  • 删除多余碍眼文件,剩下如下
    SpringBoot整合Mybatis踩坑记_第5张图片

  • 接下来我们开始搭建项目骨架,整合相关架构和配置。

2、整合MyBatis

  • 可以看到、之前选的默认依赖已经加到pom文件里面去了。后面需要啥在慢慢加。
  • 我们来看application.yml的配置:
server:
  port: 8080

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
    
mybatis:
  mapper-locations: classpath*:com/springbootmybatis/sbm/**/dao/mapper/*.xml
  type-aliases-package: com.springbootmybatis.sbm.*.dao

#showSql
logging:
  level:
    com:
      example:
        mapper : debug
  • 快速创建两个实体类,可用代码生成工具直接生成,在我另一篇博客中有代码生成器详细使用教程>>不会的@here,新建个Service来测试数据库连接是否成功。对数据库表进行增删查改。
  • 这里就演示个简单的插入吧。新建一个测试类。

SpringBoot整合Mybatis踩坑记_第6张图片

@RunWith(SpringRunner.class)
@SpringBootTest
class OrderServiceTest {
    @Autowired
    private OrderService orderService;
    @Test
    public void test(){
        Orders orders = new Orders();
        orders.setId( "123" );
        orders.setMoney( new Double(12) );
        orders.setOrdertime( new Date() );
        orders.setPaystate( 1 );
        orders.setUserId( 222 );
        int result = orderService.insertOrderData( orders );
        if(0 == result){
            System.out.println("插入失败!!!!!!!!!!!!!!!!");
        }else{
            System.out.println("插入成功!!!!!!!!!!!!!!!!");
        }
    }

}

SpringBoot整合Mybatis踩坑记_第7张图片


  • 看看打印的日志,已经成功存进数据库了。如果你运行还有问题的话。看看后面的配置。

SpringBoot整合Mybatis踩坑记_第8张图片


  • 再看看数据库、数据也确实插入进去了。说明我们的配置基本是没问题的。

SpringBoot整合Mybatis踩坑记_第9张图片

3、Could not autowire. No beans of ‘OrdersMapper’ type found. Inspection info:Checks autowiring problems in a bean class.

  • description:bean注入失败。

SpringBoot整合Mybatis踩坑记_第10张图片
遇到这个问题是因为SpringBoot加载bean的时候没有扫描到对应的bean和xml文件。一般需要改四个地方:

  • 1、检查application.yml里面对Mybatis的配置。上面我已经加了的。检查这两个路径是否与你的项目对应正确。可回去看我的项目结构对应修改。第一个对应的是dao层对应的映射文件xml。第二各对应的是mapper接口的包名。
mybatis:
  mapper-locations: classpath*:com/springbootmybatis/sbm/**/dao/mapper/*.xml
  type-aliases-package: com.springbootmybatis.sbm.*.dao
  • 2、在对应的Mapper接口加上@Mapper注解,启动类加上@MapperScan(“com.springbootmybatis.sbm.*.dao”),这是让Springboot启动的时候扫描Mapper文件。MapperScan的路径就是1里面配置的第二个路径。

SpringBoot整合Mybatis踩坑记_第11张图片


SpringBoot整合Mybatis踩坑记_第12张图片

  • 3、在pom文件里面build里面加入以下配置。

SpringBoot整合Mybatis踩坑记_第13张图片

		
            
                src/main/java
                
                    **/*.xml
                    **/*.properties
                    **/*.yml
                
                false
            
            
                src/main/resources
                
                    **/*.xml
                    **/*.properties
                    **/*.yml
                
                false
            
        
  • 4、检查Mapper接口heMapper.xml文件的名字是否一致。名字不一致也会导致启动失败。

  • 5、如果你改完以上都还是红的,还是红色的。把这里等级改成warning即可。貌似也不影响代码运行。强迫症专用。但是这个问题治标不治本。如果代码配置有问题改了也白搭。
    SpringBoot整合Mybatis踩坑记_第14张图片
    以下是我的pom文件,可参考。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.0.RELEASE
         
    
    com.springbootMybatis
    sbm
    0.0.1-SNAPSHOT
    sbm
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.data
            spring-data-commons
        
        
            org.springframework.boot
            spring-boot-starter-web-services
        

        
            mysql
            mysql-connector-java
            5.1.46
            runtime
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            org.springframework.data
            spring-data-commons
            2.1.5.RELEASE
        
        
            junit
            junit
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.5
                
                    src/main/resources/mybatis-generator/mybatis-generator-cfg.xml
                    true
                    true
                
                
                
                
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.5
                    
                
            
        
        
            
                src/main/java
                
                    **/*.xml
                    **/*.properties
                    **/*.yml
                
                false
            
            
                src/main/resources
                
                    **/*.xml
                    **/*.properties
                    **/*.yml
                
                false
            
        
    


  • 注:此项目是自己随手搭建测试使用代码生成器用的。可能不太规范。仅供参考。大佬路过请指点。如有错漏、还望指正。需要源码的可留言or私信。

你可能感兴趣的:(Java攻城狮的成长之路,打Bug升级的程序猿,程序猿必备技能)