SpringBoot整合mybatis的基本配置

时间:2025-04-25 20:28:38

Springboot整合mybatis

添加依赖

<!-- mybatis 集成 -->
 <dependency>
 	<groupId></groupId>
 	<artifactId>mybatis-spring-boot-starter</artifactId>
 	<version>2.1.1</version>
 </dependency>
 <!-- mysql 驱动 -->
 <dependency>
 	<groupId>mysql</groupId>
 	<artifactId>mysql-connector-java</artifactId>
 </dependency>
 <!-- c3p0 数据源 -->
 <dependency>
 	<groupId></groupId>
 	<artifactId>c3p0</artifactId>
 	<version>0.9.5.5</version>
 </dependency>

添加整合配置

注意空格与及缩进这些都会影响数据的读取

## 数据源配置
spring:
  datasource: # 数据库的数据配置
    type: .v2.
    driver-class-name: 
    # url不用换行,由于数据太长了这里换行了
    url: jdbc:mysql://localhost:3306/ppl?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456
## mybatis 配置
mybatis:
  mapper-locations: classpath:/mappers/*.xml # 扫描mybatis的实现dao接口*.xml文件
  type-aliases-package:  #实体类取别名
  configuration:
  ## 下划线转驼峰配置
    map-underscore-to-camel-case: true
## 显示dao 执⾏sql语句
logging:
  level:
   : debug # 改成你的dao接口文件所在包路径可以打印出sql语句

项目启动入口

需要在主程序中添加扫描dao层实现接口

@SpringBootApplication
@MapperScan("")
public class Starter{
    public static void main(String[] args) {
        SpringApplication.run(Starter.class);
    }
}