SpringBoot 2.1.4整合mybatis
项目结构:
maven依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> </dependencies>
application.yml 配置
spring: #数据库配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver type: com.zaxxer.hikari.HikariDataSource url: jdbc:mysql://129.204.106.179:3306/mybatis_practice?serverTimezone=GMT%2b8&characterEncoding=utf-8 username: root password: Liyueken1 hikari: #连接池最大连接数: cpu核心数*2 + 硬盘数, 默认10 一个连接池一般来讲连接数在10-20个, 根据部署的应用程序数量可以推测出mysql的max_connections值 maximum-pool-size: 15 #一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 idle-timeout: 600000 #一个连接的生命时长(毫秒),超时并且没被使用则被释放(retired),缺省:30分钟。建议设置比数据库超时时长少30秒,參考MySQL wait_timeout參数(show variables like '%timeout%';) max-lifetime: 1800000 #等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException。 缺省:30秒 connection-timeout: 30000 connection-test-query: SELECT 1 #mybatis配置 mybatis: #实体存放路径 type-aliases-package: com.liyk.mybatis.bean #xml存放路径 mapper-locations: classpath:mapper/*.xml #开启驼峰命名 configuration: map-underscore-to-camel-case: true
启动类:
@SpringBootApplication @MapperScan(basePackages = "com.liyk.mybatis.dao") public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } }