Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 10 章 Spring Data with MySQL

目录

10.1. 选择数据库表引擎
10.2. 声明实体
10.2.1. @Entity 声明实体
10.2.2. @Table 定义表名
10.2.3. @Id 定义主键
10.2.4. @Column 定义字段:
10.2.5. 非数据库字段
10.2.6. @Lob 注解属性将被持久化为 Blog 或 Clob 类型
10.2.7. @NotNull 不能为空声明
10.2.8. @Temporal 日期定义
10.2.9. 创建日期
10.2.10. @DateTimeFormat 处理日期时间格式
10.2.11. Enum 枚举数据类型
10.2.12. SET 数据结构
10.2.13. JSON 数据类型
10.2.14. 嵌入
10.2.15. @JsonIgnore
10.2.16. @EnableJpaAuditing 开启 JPA 审计功能
10.2.17. 注释 @Comment
10.2.18. @Pattern 数据匹配
10.2.19. 实体继承
10.2.20. 虚拟列
10.2.21. 语言区域
10.3. 映射集合属性
10.3.1. List 集合
10.3.2. 数组集合
10.3.3. Map 集合
10.3.4. Set 集合
10.3.5. 外键名称
10.3.6. 集合表子查询
10.4. 外键
10.4.1. @JoinColumn
10.4.2. @OneToOne
10.4.3. OneToMany 一对多
10.4.4. ManyToMany 多对多
10.4.5. 外键级联删除
10.4.6. 外键级联操作
10.4.7. @JoinTable
10.4.8. @OrderBy
10.4.9. @JsonIgnoreProperties
10.4.10. 允许外键为 NULL
10.4.11. 只要外键,不要数据结构
10.5. 索引
10.5.1. 普通索引
10.5.2. 组合索引
10.5.3. 唯一索引
10.6. Repository/CrudRepository/PagingAndSortingRepository/JpaRepository
10.6.1. JpaRepository
10.6.2. CrudRepository
10.6.3. PagingAndSortingRepository
10.6.4. findByXXX
10.6.5. count 操作
10.6.6. delete 删除操作
10.6.7. IsNull
10.6.8. In/NotIn
10.6.9. TRUE / FALSE
10.6.10. 比较数值大小
10.6.11. 判断日期和时间 Before / After
10.6.12. Between
10.6.13. Exists
10.6.14. OrderBy
10.6.15. GreaterThan
10.6.16. Sort 排序操作操作
10.6.17. Pageable 翻页操作
10.6.18. @DynamicInsert 与 @DynamicUpdate
10.6.19. 继承已存在的 Repository
10.6.20. Optional
10.6.21. stream 用法
10.7. TransactionTemplate
10.8. JPQL @Query
10.8.1. @Modifying 更新/删除
10.8.2. 参数传递
10.8.3. 原生 SQL 操作
10.8.4. @Query 与 Pageagble
10.8.5. 自定义返回字段
10.8.6. 返回指定字段
10.8.7. 返回指定的模型
10.8.8. 通过定义接口,返回指定字段
10.8.9. 修改返回数据
10.8.10. 事务 @Transactional
10.8.11. IN / NOT 子查询
10.9. FAQ 常见问题
10.9.1. Could not write JSON: failed to lazily initialize a collection of role
10.9.2. Query did not return a unique result: 2 results were returned
10.9.3. Executing an update/delete query
10.9.4. could not initialize proxy [cn.netkiller.domain.Device#16] - no Session
10.9.5. this is incompatible with sql_mode=only_full_group_by
10.9.6. A TupleBackedMap cannot be modified

10.1. 选择数据库表引擎

正常创建表会使用数据库默认引擎,有时数据库默认引擎并不是我们需要的,通过下面配置可以指定表引擎

			
# Spring boot 1.x.x
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

# Spring boot 2.0.2
spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect