Springboot整合mybatis

添加依赖pom.xml

<!-- mybatis 集成 -->
 <dependency>
 	<groupId>org.mybatis.spring.boot</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>com.mchange</groupId>
 	<artifactId>c3p0</artifactId>
 	<version>0.9.5.5</version>
 </dependency>

添加整合配置application.yml

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

## 数据源配置
spring:
  datasource: # 数据库的数据配置
    type: com.mchange.v2.c3p0.ComboPooledDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 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: com.ppl.springboot.entity #实体类取别名
  configuration:
  ## 下划线转驼峰配置
    map-underscore-to-camel-case: true
## 显示dao 执⾏sql语句
logging:
  level:
   com.ppl.springboot.dao: debug # 改成你的dao接口文件所在包路径可以打印出sql语句

项目启动入口

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

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

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐