JPA使用心得
SpringBoot 中集成JPA
https://blog.csdn.net/forezp/article/details/70545038
- 引入jpa依赖
- 配置jpa选项
- 修改实体类,引入注解
- 修改接口<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>spring.jpa.database-platform= org.hibernate.dialect.MySQL5Dialectjpa.hibernate.show-sql= true@lombok.Data@Entity@Table(name="fuser")public class User{@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private String id;public interface UserMapper extends JpaRepository<User, Long>{
注意事项
- 如果是Mysql且设置了数据库的数字自增的话,在主键字段需要添加GenerationType.IDENTITY注释
- 输出打印SQL配置: spring.jpa.show-sql= true
- @Column 默认驼峰法不需要特别声明 比如 private Integer numberOfPages; 对应 number_of_pages,则不需要特别声明
高级用法示例
- 分页查询和多字段的between and查询Pageable pageable = new PageRequest(page.getP_pageNumber()-1,page.getP_pagesize());Page<Record> pages=recordDao.findByHolderNameAndTimesBetweenOrderByTimesDesc(search.getUsername(),qssj,jssj, pageable);list=pages.getContent();
JPA的多表关联
作用
通过注解配置和外键的设置
- 实现自动的关联查询,查询A可以不需要写额外的sql获取到B或者B集合的实体类
- 实现级联操作,新增、修改、删除(对主键主体有效)
常用注解
- JoinColumn
@JoinColumn(name=”address_id”, referencedColumnName=”ref_id”)
- 第一个属性是本类的属性,referencedColumnName是关联表的属性,如果不写的话就是ID了
- 如果设置了外键关联,则可以不需要设置JoinColumn字段
一对一(反向关联)
Book主类(id,name,book_detail_id),设置了外键对BookDetail
@Entity@Data@Table(name = "book")public class Book {@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;private String name;@OneToOne(cascade = CascadeType.ALL)private BookDetail bookDetail;public Book() {}}BookDetail类(id ,numberOfPages),设置了反向关联mappedBy
@Entity@Table(name = "book_detail")@Datapublic class BookDetail {@Id@GeneratedValue(strategy = GenerationType.AUTO)private Integer id;private Integer numberOfPages;@OneToOne(mappedBy = "bookDetail")private Book book;public BookDetail() {}}设置了反向关联,可以通过从类找到主类,通过detail类可以找到 Book类
public void findBookDetailsTest() throws Exception{BookDetail detail=detailRepository.findOne(4);System.out.println(detail.getNumberOfPages());}
一对多
Book表 多个对应一个BookCategory,需要有book_category_id字段
@Data@Entitypublic class Book {@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;private String name;@ManyToOne@JoinColumn(name = "book_category_id")private BookCategory bookCategory;public Book(){}}CREATE TABLE `book` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`book_category_id` int(11) unsigned DEFAULT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;BookCategory,注意category表不需要有book字段,但是可以直接获取到Set
@Data@Entity@Table(name = "book_category")public class BookCategory {@Id@GeneratedValue(strategy = GenerationType.AUTO)private int id;private String name;@OneToMany(mappedBy = "bookCategory", cascade = CascadeType.ALL)private Set<Book> books;public BookCategory(){}}CREATE TABLE `book_category` (`id` int(11) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(255) NOT NULL,PRIMARY KEY (`id`,`name`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
多对多
通过JoinTable和MangToMany来实现,分别需要构建中间表和关联字段来实现
|
JPA中nativeQuery的使用
https://blog.csdn.net/qq_26819733/article/details/79661924
比如含有transient字段的时候,需要转换成实体类