通過(guò)上一篇文章佳窑,我們簡(jiǎn)單地了解了JPA的意義以及用法制恍,這次我們就通過(guò)一個(gè)具體的例子來(lái)詳細(xì)了解JPA的應(yīng)用神凑。
這次我們要做的是仿極簡(jiǎn)圖床網(wǎng)頁(yè)净神,功能就是在網(wǎng)頁(yè)上有幾個(gè)圖集,點(diǎn)擊任意一個(gè)圖集溉委,會(huì)跳到對(duì)應(yīng)的子頁(yè)面,子頁(yè)面上顯示同類(lèi)的圖片組瓣喊。
First(后端代碼)
代碼語(yǔ)言:java語(yǔ)言
編譯工具:Intellij IDEA
-
目錄結(jié)構(gòu)
structure.png
首先在pom.xml中加入要用到的依賴(lài)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
</dependencies>
然后在resourse下的applications.properties中配置數(shù)據(jù)庫(kù)相關(guān)信息
spring.datasource.url=jdbc:mysql://localhost:3306/springboot_demo?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=*****
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.tomcat.max-active=20
spring.datasource.tomcat.max-idle=8
spring.datasource.tomcat.min-idle=8
spring.datasource.initial=10
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto = update
spring.thymeleaf.cache=false
server.port=8080
其次在entity包下新建Album、Photo實(shí)體類(lèi)
@Entity
@Data
public class Album {
@Id
@GeneratedValue
private Integer id;
private String albumCover;
private String albumTitle;
private String albumDescription;
private Integer likes;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "albumId",referencedColumnName = "id")
private List<Photo> photos;
public Album(String albumCover, String albumTitle, String albumDescription, Integer likes) {
this.albumCover = albumCover;
this.albumTitle = albumTitle;
this.albumDescription = albumDescription;
this.likes = likes;
}
public Album(String albumTitle){
}
public List<Photo> getPhotos() {
return photos;
}
public void setPhotos(List<Photo> photos) {
this.photos = photos;
}
}
Entity注解必須與@Id注解 結(jié)合使用藻三,否則 No identifier specified for entity:
@Entity
@Data
public class Photo {
@Id
@GeneratedValue
private Integer id;
private String photoUrl;
private String photoName;
private String photoDescription;
public Photo(String photoUrl, String photoName, String photoDescription) {
this.photoUrl = photoUrl;
this.photoName = photoName;
this.photoDescription = photoDescription;
}
public void Photo(){
}
}
寫(xiě)到這里洪橘,其實(shí)可以運(yùn)行一下Application棵帽,這樣就會(huì)在數(shù)據(jù)庫(kù)中建立對(duì)應(yīng)的表熄求,可以直接往表里插入數(shù)據(jù)。如果這時(shí)不想添加數(shù)據(jù)的話逗概,也可以在后面的代碼中插入數(shù)據(jù)弟晚,兩種方式都可以。
Respoitry包
- AlbumRepositry
public interface AlbumRepositry extends JpaRepository<Album,Integer> {
@Query("from Album a order by a.likes desc ")
List<Album> findHotAlbum();
Album findByAlbumTitle(String albumTitle);
}
- PhotoRepositry
public interface PhotoRepositry extends JpaRepository<Photo,Integer> {
}
Service包
- AlbumService
public interface AlbumService {
List<Album> getAll();
List<Album> findHotAlbum();
Album findByAlbumTitle(String albumTitle);
}
- PhotoService
public interface PhotoService {
List<Photo> getAll();
}
impl包
- AlbumServiceImpl
@Service
public class AlbumServiceImpl implements AlbumService {
@Resource
private AlbumRepositry albumRepositry;
@Override
public List<Album> getAll() {
return albumRepositry.findAll();
}
@Override
public List<Album> findHotAlbum() {
return albumRepositry.findHotAlbum();
}
@Override
public Album findByAlbumTitle(String albumTitle) {
return albumRepositry.findByAlbumTitle(albumTitle);
}
}
- PhotoServiceImpl
@Service
public class PhotoServiceImpl implements PhotoService {
@Resource
private PhotoRepositry photoRepositry;
@Override
public List<Photo> getAll() {
return photoRepositry.findAll();
}
}
Controller包(重要卿城!)
- AlbumController
@RestController
@CrossOrigin
@RequestMapping(value = "/album")
public class AlbumController {
@Resource
private AlbumService albumService;
@GetMapping("/all")
public List<Album> getAll(){
return albumService.getAll();
}
@GetMapping("/order")
public List<Album> findHotAlbum(){
return albumService.findHotAlbum();
}
@GetMapping("/all/{name}")
public Album findByAlbumTitle(@RequestParam String albumName){return albumService.findByAlbumTitle(albumName);}
}
說(shuō)明:CrossOrigin注解