一、整合Druid

添加依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>druid-spring-boot-starter</artifactId>
  4. <version>${druid.version}</version>
  5. </dependency>

配置数据源

  1. datasource:
  2. type: com.alibaba.druid.pool.DruidDataSource
  3. driverClassName: com.mysql.cj.jdbc.Driver
  4. druid:
  5. # 主数据库
  6. master:
  7. url: jdbc:mysql://localhost:3306/wisteria?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
  8. username: root
  9. password: '...'
  10. slave:
  11. # 从数据源开关/默认关闭
  12. enabled: false
  13. url:
  14. username:
  15. password:
  16. # 初始连接数
  17. initialSize: 5
  18. # 最小连接池数量
  19. minIdle: 10
  20. # 最大连接池数量
  21. maxActive: 20
  22. # 配置获取连接等待超时的时间
  23. maxWait: 60000
  24. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  25. timeBetweenEvictionRunsMillis: 60000
  26. # 配置一个连接在池中最小生存的时间,单位是毫秒
  27. minEvictableIdleTimeMillis: 300000
  28. # 配置一个连接在池中最大生存的时间,单位是毫秒
  29. maxEvictableIdleTimeMillis: 900000
  30. # 配置检测连接是否有效
  31. validationQuery: SELECT 1 FROM DUAL
  32. testWhileIdle: true
  33. testOnBorrow: false
  34. testOnReturn: false
  35. webStatFilter:
  36. enabled: true
  37. statViewServlet:
  38. enabled: true
  39. # 设置白名单,不填则允许所有访问
  40. allow:
  41. url-pattern: /druid/*
  42. # 控制台管理用户名和密码
  43. login-username: ym
  44. login-password: ym@ly
  45. filter:
  46. stat:
  47. enabled: true
  48. # 慢SQL记录
  49. log-slow-sql: true
  50. slow-sql-millis: 1000
  51. merge-sql: true
  52. wall:
  53. config:
  54. multi-statement-allow: true

————————-(前面写了被吞了)

如果出现依赖循环,检查启动类

  1. @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })

二、mapper扫描问题

方法一:如果不编写配置类,需要在interface上加上@Mapper
方法二: 新增如下配置类

  1. package cn.ym.framework.config;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.EnableAspectJAutoProxy;
  7. import java.util.TimeZone;
  8. @Configuration
  9. // 表示通过aop框架暴露该代理对象,AopContext能够访问
  10. @EnableAspectJAutoProxy(exposeProxy = true)
  11. // 指定要扫描的Mapper类的包的路径
  12. @MapperScan("cn.ym.**.mapper")
  13. public class ApplicationConfig
  14. {
  15. /**
  16. * 时区配置
  17. */
  18. @Bean
  19. public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization()
  20. {
  21. return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
  22. }
  23. }

三、mybatis 添加数据后返回数据ID
利用mybatis插入后返回的id,进行查询。

  1. @Override
  2. public Competition insert(Competition competition) {
  3. this.competitionDao.insert(competition);
  4. return this.queryById(competition.getCompetitionId());
  5. }

mybatis设置:

四、mybatis 的Transactional注解

需要指定异常回滚的

  1. @Transactional(rollbackFor = SqlExveption.class)
  2. @Override
  3. public Competition update(Competition competition) {
  4. this.competitionDao.update(competition);
  5. return this.queryById(competition.getCompetitionId());
  6. }

五、常用类映射

MySQL JDBCType JavaType 备注
char CHAR String 定长字符
varchar VARCHAR String 变长字符串
tinyint TINYINT byte 1字节
smallint SMALLINT short 2字节
int INTEGER int 4字节
float FLOAT float 4字节
bigint BIGINT long 8字节
double DOUBLE double 8字节
bit BOOLEAN boolean 布尔类型
MySQL JDBCType JavaType 备注
date Date util.Date,sql.Date YYYY-MM-DD
time TIME util.Date,sql.Date HH:MM:SS
timestamp TIMESTAMP util.Date,sql.Date YYYY-MM-DD HH:MM:SS
text VARCHAR String 文本类型【2^16-1字节】
longtext LONGVARCHAR String 长文本类型【2^32-1字节