首先,在pom.xml中添加对mysql mybatis的依赖
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mysql driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- jdbc连接mysql -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
其次,在application.yml中添加mysql连接和mybatis的配置,其中mybatis.mapperLocations配置的是mybatis sql语句xml路径,mybatis.configuration.log-impl配置的是打印sql log日志
server:
port: 8763
spring:
application:
name: springboot-demo
datasource:
url: jdbc:mysql://127.0.0.1:3306/province_city_county?useSSL=true&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: password
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapperLocations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
完整代码见: https://github.com/Abbreviation-g/springboot-demo.git