美文网首页SpringBootWEB前端笔记本
SpringBoot+JPA+Swagger2测试

SpringBoot+JPA+Swagger2测试

作者: HeloWxl | 来源:发表于2020-02-22 18:01 被阅读0次

    1、新建一个Spring Boot项目

    SpringBoot项目

    2、添加依赖文件pom.xml

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    

    3、新建数据库

    新建数据库

    4、Model层

    @Entity  //表示是一个实体类,对应数据库中一个表
    @Data //Lombok,使用这个注解不用自己写Getter/Setter
    @Table(name = "book_06")
    public class Book06 {
        /**
         * 书籍ID
         */
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        /**
         * 书籍名称
        */ 
        private String title;
        /**
         * 图片URL
         */
        private String imgUrl;
        /**
         * 作者
         */
        private String author;
        /**
         * 翻译者
         */
        private String translator;
        /**
         * 价格
         */
        private double price;
        /**
         * 出版社名称
         */
        private String publisherName;
    }
    
    

    5、Dao层

    @Repository
    public interface Book06Repository extends JpaRepository<Book06, Integer> {
    }
    

    6、Controller层

    @Api("书籍管理接口")
    @RestController
    public class Book06Controller {
    
        @Autowired
        private Book06Repository productRepository;
    
        /**
        * @Description: 查询所有
        * @Author: wangxianlin
        * @Date: 2020/2/22 4:25 PM
        */
        @ApiOperation("查询所有书籍")
        @GetMapping("/product")
        private List<Book06> list() {
            return productRepository.findAll();
        }
    
        /**
        * @Description: 新增
        * @Author: wangxianlin
        * @Date: 2020/2/22 4:25 PM
        */
        @ApiOperation("新增书籍")
        @PostMapping(value = "/product")
        private Book06 add(Book06 item) {
            return productRepository.save(item);
        }
    
        /**
        * @Description: 根据ID查询
        * @Author: wangxianlin
        * @Date: 2020/2/22 4:26 PM
        */
        @ApiOperation("根据ID查询书籍")
        @GetMapping(value = "/product/{id}")
        public Book06 getOne(@PathVariable("id") Integer id) {
            return productRepository.findById(id).get();
        }
    
        /**
        * @Description: 根据ID删除
        * @Author: wangxianlin
        * @Date: 2020/2/22 4:26 PM
        */
        @ApiOperation("根据ID删除书籍")
        @DeleteMapping(value = "/product/{id}")
        public void delete(@PathVariable("id") Integer id) {
            productRepository.deleteById(id);
        }
    
        /**
        * @Description: 更新
        * @Author: wangxianlin
        * @Date: 2020/2/22 4:26 PM
        */
        @ApiOperation("更新书籍")
        @PutMapping(value = "/product")
        public Book06 put(Book06 item) {
            return productRepository.save(item);
        }
    
    }
    

    7、config层

    @Configuration
    public class Swagger2 {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.dmt.jpa06"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("JPA测试")
                    .description("简单优雅的restful风格")
                    .termsOfServiceUrl("www.wangxianlin@icloud.com")
                    .version("1.0")
                    .build();
        }
    
    }
    

    8、测试

    Swagger2测试页面
    • 新增书籍


      新增书籍
    • 查询所有的书籍


      查询所有书籍

    --修改书籍


    修改书籍

    -根据ID查询书籍


    image.png
    • 删除书籍


      根据ID删除书籍

    9、发现问题解决办法

    • 项目启动报如下的错误
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'book06Controller': Unsatisfied dependency expressed through field 'productRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'book06Repository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.dmt.jpa06.model.Book06
    
    • 解决办法:
      这个包导入错误!


      image.png

    正确包:javax.persistence.Entity

    相关文章

      网友评论

        本文标题:SpringBoot+JPA+Swagger2测试

        本文链接:https://www.haomeiwen.com/subject/mzalqhtx.html