SpringBoot+JPA
一、设置配置文件
application-dev.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/missyou?characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: 123Abc456
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
show_sql:true
format_sql:true
二、maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<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>
三、JPA在SpringBoot中应用
1、实体映射到表
@Entity
package com.lin.missyou.model;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Entity
@Table(name="banner")
public class Banner extends BaseEntity {
@Id
private int id;
private String name;
private String description;
private String title;
private String img;
//....
}
2、@Column的常用属性总结:
name :字段名称;
unique :true/false—>唯一标识;
nullable :true/false---->是否可以为NULL;
insertable :true/false---->在执行insert时是否插入值;
updatable :true/false----->执行update时是否更新字段值;
table:字段所在地表名;
length:字段长度;
3、表关系定义
3.1、单向一对多
@OneToMany
@JoinColumn(name=“bannerId”)
当不通过@JoinColumn(name=“bannerId”)设置关联外键时JPA会自动生成一张关系表
//banner
package com.lin.missyou.model;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
@Table(name="banner")
public class Banner extends BaseEntity {
@Id
private Long id;
private String name;
private String description;
private String title;
private String img;
//一个banner对应多个BannerItem
@OneToMany(fetch=FetchType.LAZY) //急加载
@JoinColumn(name="bannerId") //指明外键的名称
private List<BannerItem> items;
}
//bannerItem
package com.lin.missyou.model;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
@Data
public class BannerItem extends BaseEntity {
@Id
private int id;
private String img;
private String keyword;
private short type;
private String name;
//外键
private Long bannerId;
}
3.2、双向一对多
关系被维护方(一端)
package com.lin.missyou.model;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
@Data
@Entity
@Table(name="banner")
public class Banner extends BaseEntity {
@Id
private int id;
private String name;
private String description;
private String title;
private String img;
@OneToMany(mappedBy = "banner",fetch = FetchType.LAZY)
private List<BannerItem> items;
}
关系维护方(多端)
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
@Data
public class BannerItem extends BaseEntity {
@Id
private int id;
private String img;
private String keyword;
private short type;
private String name;
@ManyToOne
private Banner banner;
}
4、JPA查询
4.1、自定义接口继承JpaRepository接口
JpaRepositor接收两个泛型
1)操作实体类的类型:这里还是以之前的Banner实体类为示例
2)实体类主键的类型
package com.lin.missyou.repository;
import com.lin.missyou.model.Banner;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BannerRepository extends JpaRepository<Banner,Long> {
Banner findOneById(Long id);
Banner findOneByName(String name);
}
4.2、service层调用接口进行查询
package com.lin.missyou.service;
import com.lin.missyou.model.Banner;
import com.lin.missyou.repository.BannerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BannerServiceImpl implements BannerService {
@Autowired
private BannerRepository bannerRepository;
@Override
public Banner getByName(String name) {
return bannerRepository.findOneByName(name);
}
@Override
public Banner getById(Long id) {
return bannerRepository.findOneById(id);
}
}
4.3、controller层调用service层返回到前端数据查询结果
@RestController
@RequestMapping("/banner")
@Validated
public class BannerController {
@Autowired
BannerService bannerService;
@GetMapping("/name/{name}")
public Banner getByName(@PathVariable @NotBlank String name) {
Banner banner = bannerService.getByName(name);
if(banner == null){
throw new NotFoundException(30005);
}
return banner;
}
}
结果展示:
这里附上后台打印的sql语句
Hibernate:
select
banner0_.id as id1_0_,
banner0_.create_time as create_t2_0_,
banner0_.delete_time as delete_t3_0_,
banner0_.update_time as update_t4_0_,
banner0_.description as descript5_0_,
banner0_.img as img6_0_,
banner0_.name as name7_0_,
banner0_.title as title8_0_
from
banner banner0_
where
banner0_.name=?
Hibernate:
select
items0_.banner_id as banner_i5_1_0_,
items0_.id as id1_1_0_,
items0_.id as id1_1_1_,
items0_.create_time as create_t2_1_1_,
items0_.delete_time as delete_t3_1_1_,
items0_.update_time as update_t4_1_1_,
items0_.banner_id as banner_i5_1_1_,
items0_.img as img6_1_1_,
items0_.keyword as keyword7_1_1_,
items0_.name as name8_1_1_,
items0_.type as type9_1_1_
from
banner_item items0_
where
items0_.banner_id=?
从打印的sql可以看到jpa查询了两张表这是因为我在导航属性上设置了急加载@OneToMany(fetch=FetchType.LAZY)。jpa默认情况下是懒加载的,这样会提高查询性能。