SpringBoot
# 配置端口号
server.port: 8085
# 项目名称
spring.application.name: user-service
数据库配置
JDBC连接数据库
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
application.yml
# 配置jdbc 连接
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/hm49?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
username: root
password: root
application.properties 比较喜欢这种
# 配置jdbc 连接
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/hm49?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
spring.datasource.username: root
spring.datasource.password: root
# 默认配置的数据源为class com.zaxxer.hikari.HikariDataSource
Mybatis 配置
# 服务端口号
server:
port: 8080
# 数据库地址
datasource:
url: localhost:3306/blog_test
spring:
datasource: # 数据库配置
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${datasource.url}?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
username: root
password: 123456
hikari:
maximum-pool-size: 10 # 最大连接池数
max-lifetime: 1770000
mybatis:
# 指定别名设置的包为所有entity
type-aliases-package: com.itcodai.course10.entity
configuration:
map-underscore-to-camel-case: true # 驼峰命名规范
mapper-locations: # mapper映射文件位置
- classpath:mapper/*.xml
# 服务端口号
server.port: 8080
# 数据库地址
datasource.url: localhost:3306/blog_test
# 数据库配置
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://${datasource.url}?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.username: root
spring.datasource.password: 123456
# 默认配置的数据源为class com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.maximum-pool-size: 10 # 最大连接池数
spring.datasource.hikari.max-lifetime: 1770000
# 指定别名设置的包为所有entity
mybatis.type-aliases-package: com.itcodai.course10.entity
mybatis.configuration.map-underscore-to-camel-case: true # 驼峰命名规范
# mapper映射文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
配置数据库连接池Druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
#第一步连接数据库
#第二步配置连接池+type
#第三部测试查询jdbcTemplate.queryForList(sql);
#第四部添加连接池配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/hm49?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
# 配置jdbc 连接
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/hm49?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
spring.datasource.username: root
spring.datasource.password: root
# 特指 Druid 连接池
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
增强配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/hm49?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
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
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,
# Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#第一步连接数据库
#第二步配置连接池+type
#第三部测试查询jdbcTemplate.queryForList(sql);
#第四部添加连接池配置
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
spring.datasource.initialSize: 5
spring.datasource.minIdle: 5
spring.datasource.maxActive: 20
spring.datasource.maxWait: 60000
spring.datasource.timeBetweenEvictionRunsMillis: 60000
spring.datasource.minEvictableIdleTimeMillis: 300000
spring.datasource.validationQuery: SELECT 1 FROM DUAL
spring.datasource.testWhileIdle: true
spring.datasource.testOnBorrow: false
spring.datasource.testOnReturn: false
spring.datasource.poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,
# Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
spring.datasource.filters: stat,wall,log4j
spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
spring.datasource.useGlobalDataSourceStat: true
spring.datasource.connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis-plus配置
只用mybatis 就去掉-plus的后缀即可
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
field-strategy: NOT_EMPTY
#数据库类型
db-type: MYSQL
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# mybatis-plus相关配置
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mybatis-plus.mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",
# ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
mybatis-plus.global-config.db-config.id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
mybatis-plus.global-config.db-config.field-strategy: NOT_EMPTY
#数据库类型
mybatis-plus.global-config.db-config.db-type: MYSQL
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
mybatis-plus.configuration.map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
mybatis-plus.configuration.call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 扫描通用枚举包
mybatis-plus.type-enums-package: com.common.enums
总结
-
添加依赖
<!--引入lombok 注解生成get,set 构造方法--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--引入mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> <!--引入Mybatis-plus 关联了MyBatis 的依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!--引入druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency>
-
简易配置
# 服务端口号 server.port: 8080 # 数据库地址 datasource.url: localhost:3306/blog_test # 配置jdbc 连接 spring.datasource.driver-class-name: com.mysql.jdbc.Driver spring.datasource.url: jdbc:mysql://localhost:3306/hm49?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8 spring.datasource.username: root spring.datasource.password: root # 特指 Druid 连接池 spring.datasource.type: com.alibaba.druid.pool.DruidDataSource # 指定别名设置的包为所有entity mybatis-plus.type-aliases-package: com.itcodai.course10.entity # mapper映射文件位置 去掉-plus是mybatis的配置 mybatis-plus.mapper-locations: classpath:mapper/*.xml # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Mybatis-plus 自动生成代码所需注解
<!--引入Mybatis-plus 自动生成代码-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<!--代码生成器模板-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
具体生成
推荐==》MyBatis-Plus代码生成器(新)使用
更详细=》Mybatis-plus官网