Spring-Boot-JPA进阶及实战

通过上一篇文章,我们简单地了解了JPA的意义以及用法,这次我们就通过一个具体的例子来详细了解JPA的应用。

这次我们要做的是仿极简图床网页,功能就是在网页上有几个图集,点击任意一个图集,会跳到对应的子页面,子页面上显示同类的图片组。

First(后端代码)

  • 代码语言:java语言

  • 编译工具:Intellij IDEA

  • 目录结构


    structure.png

    首先在pom.xml中加入要用到的依赖


        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            mysql
            mysql-connector-java
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.webjars
            bootstrap
            3.3.7-1
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.webjars
            jquery
            3.2.1
        
        
            com.spring4all
            swagger-spring-boot-starter
            1.8.0.RELEASE
        

    

然后在resourse下的applications.properties中配置数据库相关信息

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 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 getPhotos() {
        return photos;
    }

    public void setPhotos(List photos) {
        this.photos = photos;
    }
}

Entity注解必须与@Id注解 结合使用,否则 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(){

    }
}

写到这里,其实可以运行一下Application,这样就会在数据库中建立对应的表,可以直接往表里插入数据。如果这时不想添加数据的话,也可以在后面的代码中插入数据,两种方式都可以。

Respoitry包

  • AlbumRepositry
public interface AlbumRepositry extends JpaRepository {
    @Query("from Album a  order by a.likes desc ")
    List findHotAlbum();
    Album findByAlbumTitle(String albumTitle);
}
  • PhotoRepositry
public interface PhotoRepositry extends JpaRepository {
}

Service包

  • AlbumService
public interface AlbumService {
    List getAll();
    List findHotAlbum();
    Album findByAlbumTitle(String albumTitle);
}
  • PhotoService
public interface PhotoService {
    List getAll();
}

impl包

  • AlbumServiceImpl
@Service
public class AlbumServiceImpl implements AlbumService {
    @Resource
    private AlbumRepositry albumRepositry;
    @Override
    public List getAll() {
        return albumRepositry.findAll();
    }

    @Override
    public List 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 getAll() {
        return photoRepositry.findAll();
    }
}

Controller包(重要!)

  • AlbumController
@RestController
@CrossOrigin
@RequestMapping(value = "/album")
public class AlbumController {
    @Resource
    private AlbumService albumService;

    @GetMapping("/all")
    public List getAll(){
        return albumService.getAll();
    }

    @GetMapping("/order")
    public List findHotAlbum(){
        return albumService.findHotAlbum();
    }

    @GetMapping("/all/{name}")
    public Album findByAlbumTitle(@RequestParam String albumName){return albumService.findByAlbumTitle(albumName);}
}

说明:CrossOrigin注解

你可能感兴趣的:(Spring-Boot-JPA进阶及实战)