重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

SpringBoot中怎么整合Mybatis

这篇文章将为大家详细讲解有关SpringBoot中怎么整合Mybatis,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联-专业网站定制、快速模板网站建设、高性价比庆云网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式庆云网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖庆云地区。费用合理售后完善,十年实体公司更值得信赖。

第一步、pom.xml中引入Mybatis依赖:

(注意:我的SpringBoot版本是2.0.3)


    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.3.0
    
    
    
    
      mysql
      mysql-connector-java
    
    

第二步、application.yml中配置数据库信息,Mapper信息和mybatis-config信息:

spring:
 datasource:
  username: sblueice
  password: sblueice
  url: jdbc:mysql://localhost:3306/sblueice?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&allowMultiQueries=true&autoReconnect=true&useSSL=false
  driver-class-name: com.mysql.jdbc.Driver

mybatis:
 #Mapper所在位置
 mapper-locations: classpath:mybatis/mapping/*Mapper.xml
 config-location: classpath:mybatis/mybatis-config.xml

说明:下面是Mybatis对应的位置信息,对应yml中配置的路径和名称

SpringBoot中怎么整合Mybatis

第三步、配置mybatis-config(没有的创建下就OK):






   
      
    
    
  

  
    
  

第四步、SpringBoot启动类中添加注解@MapperScan("com.sblueice.mapper")

说明:这样会增加耦合,但是不加又扫描不到,目前没找到合适的方法解耦,暂时用这个注解

@MapperScan("com.sblueice.mapper")
@SpringBootApplication
public class DemoApplication {
 
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
 
}

关于SpringBoot中怎么整合Mybatis就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站标题:SpringBoot中怎么整合Mybatis
地址分享:http://cqcxhl.cn/article/jgpeij.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP