SpringBoot——整合mybatis
-
导入依赖
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
-
在
application.yml
中编写数据库配置spring: datasource: # 数据源基本配置 username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost/seckill?serverTimezone=Asia/Shanghai type: com.alibaba.druid.pool.DruidDataSource #数据源其他配置 initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true
-
在
resources
目录下创建mybatis
包并编写配置文件<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!--开启驼峰命名法--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--使用jdbc的useGeneratedKeys获取数据库自增主键值--> <setting name="useGeneratedKeys" value="true"/> </settings> </configuration>
-
在
application.yml
中编写mybatis配置#扫描mybatis主配置文件,*Mapper.xml文件 mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml
-
在
mybatis
包下面创建一个mapper
包方便编写实体类的sql -
service层调用dao层
-
controller层调用service层
-
开启服务测试!