Spring Boot

Spring-Boot-JPA进阶及实战

2018-10-22  本文已影响0人  飞逝1

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

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

First(后端代码)

<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中配置数据库相关信息

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注解 结合使用,否则 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包

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

Service包

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

impl包

@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);
    }

}
@Service
public class PhotoServiceImpl implements PhotoService {
    @Resource
    private PhotoRepositry photoRepositry;

    @Override
    public List<Photo> getAll() {
        return photoRepositry.findAll();
    }
}

Controller包(重要!)

@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注解

上一篇下一篇

猜你喜欢

热点阅读