1.导入JDBC驱动
因为我的是Mysql数据库 版本是8.0.20 导入对应版本的驱动即可
<!--mysql 依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
2.导入数据库连接池
SpringBoot 推荐我们使用HikariDataSource数据库连接池,也是SpringBoot默认支持的数据库连接池
<!--开启SpringBootJdbc,默认为HikariDataSource源--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
3.导入Mybatis-starter
<!--Mybatis starter--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
配置:
1.mapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--绑定一个对应的接口--> <mapper namespace="com.example.blog.mapper.xxxMapper"> </mapper>
2.配置文件
spring: #数据源基本配置 datasource: username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/learn?serverTimezone=UTC type: com.zaxxer.hikari.HikariDataSource # mybatis配置 mybatis: #加载xml配置文件 config-location: classpath:mybatis-config.xml #扫描包为Bean配置别名 type-aliases-package: com.example.bean #配置mapper.xml文件的位置 mapper-locations: classpath:mapper/*.xml configuration: #开启驼峰命名 map-underscore-to-camel-case: true
标签:xml,mapper,Springoot,spring,boot,mybatis,整合,mysql,MyBatis From: https://www.cnblogs.com/ErenYeager/p/17013335.html