通過上一篇文章,我們簡單地了解了JPA的意義以及用法,這次我們就通過一個具體的例子來詳細(xì)了解JPA的應(yīng)用。
這次我們要做的是仿極簡圖床網(wǎng)頁,功能就是在網(wǎng)頁上有幾個圖集,點(diǎn)擊任意一個圖集,會跳到對應(yīng)的子頁面,子頁面上顯示同類的圖片組。
First(后端代碼)
代碼語言:java語言
編譯工具:Intellij IDEA
-
目錄結(jié)構(gòu)
structure.png
首先在pom.xml中加入要用到的依賴
<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ù)庫相關(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實體類
@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(){
}
}
寫到這里,其實可以運(yùn)行一下Application,這樣就會在數(shù)據(jù)庫中建立對應(yīng)的表,可以直接往表里插入數(shù)據(jù)。如果這時不想添加數(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);}
}
說明:CrossOrigin注解
