前文我们使用SPRING INITIALIZR快速构建了一个空的Spring Boot项目,并增加web模块实现了HTTP请求。

这一篇继续在该demo的基础上,增加JPA的功能。

JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象关系表的映射关系,并将运行时的实体对象持久化到数据库中。

引入JPA模块

1.在POM文件中添加:

  1. <!-- JPA模块 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-jpa</artifactId>
  5. </dependency>
  1. <!-- mysql -->

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>

  1.  

2.在application.properties属性文件中定义好数据源(传统做法,这里不演示了)

2.(我们使用Spring推荐的yml来配置)新建application.yml文件,内容如下:

  1. server:
  2. port: 8000
  3. spring:
  4. application:
  5. name: demo
  6. jpa:
  7. properties:
  8. hibernate:
  9. cache:
  10. use_second_level_cache: false
  11. use_query_cache: false
  12. generate_statistics: true
  13. cache.region.factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory
  14. generate-ddl: false
  15. hibernate:
  16. ddl-auto: update
  17. show-sql: true
  18. datasource:
  19. platform: mysql
  20. url: jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf8
  21. username: root
  22. password: 你的数据库密码
  23. # schema: classpath:schema.sql
  24. # data: classpath:data.sql
  25. logging:
  26. level:
  27. root: info

相应的,在本地创建一个叫demo的数据库,用于我们的测试。

定义2个实体

1.新建包com.v5ent.demo.domain

2.在这个包下新建两个实体类Tag和Note,代码如下

  1. /*
  2. * Copyright 2012-2017 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package com.v5ent.demo.domain;
  18.  
  19. import java.util.List;
  20.  
  21. import javax.persistence.Entity;
  22. import javax.persistence.GeneratedValue;
  23. import javax.persistence.Id;
  24. import javax.persistence.ManyToMany;
  25. import javax.persistence.SequenceGenerator;
  26.  
  27. @Entity
  28. public class Note {
  29.  
  30. @Id
  31. @SequenceGenerator(name="note_generator", sequenceName="note_sequence", initialValue = 5)
  32. @GeneratedValue(generator = "note_generator")
  33. private long id;
  34.  
  35. private String title;
  36.  
  37. private String body;
  38.  
  39. @ManyToMany
  40. private List<Tag> tags;
  41.  
  42. protected Note() {};
  43.  
  44. public Note(String title, String body) {
  45. this.title = title;
  46. this.body = body;
  47. }
  48.  
  49. public long getId() {
  50. return this.id;
  51. }
  52.  
  53. public void setId(long id) {
  54. this.id = id;
  55. }
  56.  
  57. public String getTitle() {
  58. return this.title;
  59. }
  60.  
  61. public void setTitle(String title) {
  62. this.title = title;
  63. }
  64.  
  65. public String getBody() {
  66. return this.body;
  67. }
  68.  
  69. public void setBody(String body) {
  70. this.body = body;
  71. }
  72.  
  73. public List<Tag> getTags() {
  74. return this.tags;
  75. }
  76.  
  77. public void setTags(List<Tag> tags) {
  78. this.tags = tags;
  79. }
  80.  
  81. }

Tag:

  1. /*
  2. * Copyright 2012-2017 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16.  
  17. package com.v5ent.demo.domain;
  18.  
  19. import java.util.List;
  20.  
  21. import javax.persistence.Entity;
  22. import javax.persistence.GeneratedValue;
  23. import javax.persistence.Id;
  24. import javax.persistence.ManyToMany;
  25. import javax.persistence.SequenceGenerator;
  26.  
  27. @Entity
  28. public class Tag {
  29.  
  30. @Id
  31. @SequenceGenerator(name="tag_generator", sequenceName="tag_sequence", initialValue = 4)
  32. @GeneratedValue(generator = "tag_generator")
  33. private long id;
  34.  
  35. private String name;
  36.  
  37. @ManyToMany(mappedBy = "tags")
  38. private List<Note> notes;
  39.  
  40. public long getId() {
  41. return this.id;
  42. }
  43.  
  44. public void setId(long id) {
  45. this.id = id;
  46. }
  47.  
  48. public String getName() {
  49. return this.name;
  50. }
  51.  
  52. public void setName(String name) {
  53. this.name = name;
  54. }
  55.  
  56. public List<Note> getNotes() {
  57. return this.notes;
  58. }
  59.  
  60. public void setNotes(List<Note> notes) {
  61. this.notes = notes;
  62. }
  63.  
  64. }

这里我们定义了两个多对多的实体,笔记和标签。

定义2个实体的Jpa

1.新建包com.v5ent.demo.repository

2.在这个包下新建两个Jpa类TagRepository和NoteRepository,代码如下

  1. package com.v5ent.demo.repository;
  2.  
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4.  
  5. import com.v5ent.demo.domain.Tag;
  6.  
  7. /**
  8. * Spring Data JPA repository for the Tag entity.
  9. */
  10. public interface TagRepository extends JpaRepository<Tag,Long> {
  11.  
  12. }
  1. package com.v5ent.demo.repository;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.data.jpa.repository.JpaRepository;
  6.  
  7. import com.v5ent.demo.domain.Note;
  8.  
  9. /**
  10. * Spring Data JPA repository for the Note entity.
  11. */
  12. public interface NoteRepository extends JpaRepository<Note,Long> {
  13.  
  14. }

至此,所有的工作就完成了。

大家可以看一下仓库类的父类,常用的数据库访问操作都默认实现了。

JPA的查询方法

在JPA中有三种方式可以进行数据的查询(1,方法命名查询 2,@NamedQuery查询 3,@Query查询),

第一种:方法命名查询

1. Spring Data JPA支持通过定义在Repository接口的方法来定义查询,方法名字是根据实体类的属性名来确定的,示例如下:

  1. /**
  2. * Spring Data JPA repository for the Note entity.
  3. */
  4. public interface NoteRepository extends JpaRepository<Note,Long> {
  5.  
  6. List<Note> findByBody(String body);
  1.   List<Note> findByTitleAndBody(String title,String body);
  1. }

从代码可以看出,使用findBy这样的关键字,就可以直接实现根据字段查询。(这里的findBy也可以用find,getBy,query,read代替)而And就相当于sql语句中的and。

2.用关键字限制结果数量,用top和first来实现,示例:
  1. /*
  2. *查询符合条件的前十条记录
  3. */
  4. List<Note> findFirst10ByTitle(String title)
  5. /*
  6. *查询符合条件的前30条记录
  7. */
  8. List<Note> findTop30ByTitle(String title);

第二种:@NamedQuery查询

Spring Data JPA 支持@NameQuery来定义查询方法,即一个名称映射一个查询语句(要在实体类上写,不是接口里写),示例如下:

  1. @Entity
  2. @NamedQuery(name="Note.findByTitle",
  3. query="select p from Note p where p.title=?1")
  4. public class Note{
  5.  
  6. }

如果要将多个方法都进行重新定义,可以使用@NameQueries标签,示例如下:

  1. @Entity
  2. @NamedQueries({
  3. @NamedQuery(name="Note.findByTitle",
  4. query="select p from Note p where p.title=?1"),
  5. @NamedQuery(name = "Note.withTitleAndBodyQuery",
  6. query = "select p from Note p where p.title=?1 and body=?2")
  7. })
  8. public class Note{
  9.  
  10. }

这个时候,接口里定义的findByName方法就是上面的方法了,不再是方法命名查询的方法了。

第三种:@Query查询

Spring Data JPA 支持@Query来定义查询方法,使用方法是将@Query写在接口的方法上面,示例如下:

  1. public interface NoteRepository extends JpaRepository<Note, Long> {
  2. @Query("select p from Note p where p.title like ?1 ")
  3. List<Note> anyQuery(String title);
  4.  
  5. }

这里的参数是根据索引号来进行查询的。

当然我们也是可以根据名称来进行匹配,然后进行查询的,示例如下:

  1. public interface NoteRepository extends JpaRepository<Note, Long> {
  2. @Query("select p from Note p where p.title like :title")
  3. List<Note> anyQuery(@Param("title")String title); }

如果是模糊匹配呢,这么写:

  1. @Query("select p from Note p where p.title like %:title%")
  2. List<Note> anyQuery(@Param("title")String title);

我们再来看看更新是如何写的。

Spring Data JPA支持使用@Modifying和@Query注解组合来进行更新查询,示例如下:

  1. @Modifying
  2. @Transcational
  3. @Query("update Note p set p.title=?1 ")
  4. int setTitle(String title);

int表示的是更新语句所影响的行数。

来测试看看

1.我们不准使用之前的测试类,我准备直接在启动的时候测试。首先在DemoApplication中增加如下测试代码:

  1.   @Bean
  2. public CommandLineRunner demo(final NoteRepository repository) {
  3. return new CommandLineRunner() {
  4.  
  5. @Override
  6. public void run(String... args) throws Exception {
  7. // save a couple of Notes
  8. repository.save(new Note("Have a nice Day", "Don’t let mental blocks control you. Set yourself free. Confront your fear and turn the mental blocks into building blocks."));
  9. repository.save(new Note("x-Notes", "Chloe O'Brian,Kim Bauer"));
  10. repository.save(new Note("Some Others", "No news is good news"));
  11.  
  12. // fetch all Notes
  13. log.info("Notes found with findAll():");
  14. log.info("-------------------------------");
  15. for (Note note : repository.findAll()) {
  16. log.info(note.toString());
  17. }
  18.  
  19. // fetch an individual note by ID
  20. Note note = repository.findOne(1L);
  21. log.info("Note found with findOne(1L):");
  22. log.info("--------------------------------");
  23. log.info(note.toString());
  24.  
  25. // fetch Notes by last name
  26. log.info("Note found with anyQuery(\"Some\"):");
  27. log.info("--------------------------------------------");
  28. for (Note bauer : repository.anyQuery("Some")) {
  29. log.info(bauer.toString());
  30. }
  31. }
  32. };
  33. }

2.执行main方法跑起来,我们可以看到控制台输出了我们的操作日志。

  1. -- ::07.820 INFO --- [ main] com.v5ent.demo.DemoApplication : Note found with findOne(1L):
  2. -- ::07.820 INFO --- [ main] com.v5ent.demo.DemoApplication : --------------------------------
  3. -- ::07.820 INFO --- [ main] com.v5ent.demo.DemoApplication : com.v5ent.demo.domain.Note@7893c715
  4. -- ::07.820 INFO --- [ main] com.v5ent.demo.DemoApplication : Note found with anyQuery("Some"):
  5. -- ::07.820 INFO --- [ main] com.v5ent.demo.DemoApplication : --------------------------------------------

补充

不得不提,在此基础上,实现Rest化API很简单。下面来展开实验:

在当前的情况下,我们看一下mvc方面的日志:

  1. -- ::45.980 INFO --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.v5ent.demo.web.HelloController.index()
  2. -- ::45.985 INFO --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
  3. -- ::45.985 INFO --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
  4. -- ::46.024 INFO --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  5. 2017-05-18 12:13:46.024 INFO 12664 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  6. 2017-05-18 12:13:46.078 INFO 12664 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  7. -- ::46.354 INFO --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  8. -- ::46.421 INFO --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): (http)

可以看到/hello这个我们之前实现的controller,如果我们要实现note和tag的对外api,当然也要自己写controller。但是Sring就是专门把这些固定的做法实现的机构,所以Spring已经封装了rest风格api所需要的controller实现。

简单来讲,我们只需要在pom文件中增加:

  1.      <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-rest</artifactId>
  4. </dependency>

ok,现在启动看看日志:

  1. -- ::36.876 INFO --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@72a7c7e0: startup date [Thu May :: CST ]; root of context hierarchy
  2. -- ::36.954 INFO --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.v5ent.demo.web.HelloController.index()
  3. -- ::36.958 INFO --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
  4. -- ::36.959 INFO --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
  5. -- ::36.994 INFO --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  6. 2017-05-18 12:55:36.995 INFO 7124 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  7. 2017-05-18 12:55:37.010 INFO 7124 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in repositoryRestExceptionHandler
  8. 2017-05-18 12:55:37.102 INFO 7124 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
  9. -- ::37.262 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@72a7c7e0: startup date [Thu May :: CST ]; root of context hierarchy
  10. -- ::37.275 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/ || ],methods=[OPTIONS],produces=[application/hal+json || application/json]}" onto public org.springframework.http.HttpEntity<?> org.springframework.data.rest.webmvc.RepositoryController.optionsForRepositories()
  11. -- ::37.276 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/ || ],methods=[HEAD],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryController.headForRepositories()
  12. -- ::37.277 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/ || ],methods=[GET],produces=[application/hal+json || application/json]}" onto public org.springframework.http.HttpEntity<org.springframework.data.rest.webmvc.RepositoryLinksResource> org.springframework.data.rest.webmvc.RepositoryController.listRepositories()
  13. -- ::37.280 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}],methods=[DELETE],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryEntityController.deleteItemResource(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,org.springframework.data.rest.webmvc.support.ETag) throws org.springframework.data.rest.webmvc.ResourceNotFoundException,org.springframework.web.HttpRequestMethodNotSupportedException
  14. -- ::37.280 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}],methods=[PATCH],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryEntityController.patchItemResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.PersistentEntityResource,java.io.Serializable,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler,org.springframework.data.rest.webmvc.support.ETag,java.lang.String) throws org.springframework.web.HttpRequestMethodNotSupportedException,org.springframework.data.rest.webmvc.ResourceNotFoundException
  15. -- ::37.280 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}],methods=[OPTIONS],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryEntityController.optionsForCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation)
  16. -- ::37.281 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}],methods=[HEAD],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryEntityController.headCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable) throws org.springframework.web.HttpRequestMethodNotSupportedException
  17. -- ::37.281 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}],methods=[GET],produces=[application/hal+json || application/json]}" onto public org.springframework.hateoas.Resources<?> org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.data.rest.webmvc.ResourceNotFoundException,org.springframework.web.HttpRequestMethodNotSupportedException
  18. -- ::37.281 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}],methods=[GET],produces=[application/x-spring-data-compact+json || text/uri-list]}" onto public org.springframework.hateoas.Resources<?> org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResourceCompact(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.data.rest.webmvc.ResourceNotFoundException,org.springframework.web.HttpRequestMethodNotSupportedException
  19. -- ::37.281 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}],methods=[POST],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryEntityController.postCollectionResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.PersistentEntityResource,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler,java.lang.String) throws org.springframework.web.HttpRequestMethodNotSupportedException
  20. -- ::37.282 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}],methods=[OPTIONS],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryEntityController.optionsForItemResource(org.springframework.data.rest.webmvc.RootResourceInformation)
  21. -- ::37.282 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}],methods=[HEAD],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositoryEntityController.headForItemResource(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws org.springframework.web.HttpRequestMethodNotSupportedException
  22. -- ::37.282 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}],methods=[GET],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<org.springframework.hateoas.Resource<?>> org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler,org.springframework.http.HttpHeaders) throws org.springframework.web.HttpRequestMethodNotSupportedException
  23. -- ::37.283 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}],methods=[PUT],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<? extends org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryEntityController.putItemResource(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.data.rest.webmvc.PersistentEntityResource,java.io.Serializable,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler,org.springframework.data.rest.webmvc.support.ETag,java.lang.String) throws org.springframework.web.HttpRequestMethodNotSupportedException
  24. -- ::37.285 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}/{property}],methods=[DELETE],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<? extends org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.deletePropertyReference(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,java.lang.String) throws java.lang.Exception
  25. -- ::37.286 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}/{property}],methods=[GET],produces=[application/x-spring-data-compact+json || text/uri-list]}" onto public org.springframework.http.ResponseEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.followPropertyReferenceCompact(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,java.lang.String,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws java.lang.Exception
  26. -- ::37.286 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}/{property}],methods=[GET],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.followPropertyReference(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,java.lang.String,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws java.lang.Exception
  27. -- ::37.286 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}/{property}/{propertyId}],methods=[GET],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.followPropertyReference(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,java.lang.String,java.lang.String,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler) throws java.lang.Exception
  28. -- ::37.286 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}/{property}/{propertyId}],methods=[DELETE],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.deletePropertyReferenceId(org.springframework.data.rest.webmvc.RootResourceInformation,java.io.Serializable,java.lang.String,java.lang.String) throws java.lang.Exception
  29. -- ::37.287 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/{id}/{property}],methods=[PATCH || PUT || POST],consumes=[application/json || application/x-spring-data-compact+json || text/uri-list],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<? extends org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.createPropertyReference(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.http.HttpMethod,org.springframework.hateoas.Resources<java.lang.Object>,java.io.Serializable,java.lang.String) throws java.lang.Exception
  30. -- ::37.288 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/search],methods=[GET],produces=[application/hal+json || application/json]}" onto public org.springframework.data.rest.webmvc.RepositorySearchesResource org.springframework.data.rest.webmvc.RepositorySearchController.listSearches(org.springframework.data.rest.webmvc.RootResourceInformation)
  31. -- ::37.288 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/search/{search}],methods=[HEAD],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<java.lang.Object> org.springframework.data.rest.webmvc.RepositorySearchController.headForSearch(org.springframework.data.rest.webmvc.RootResourceInformation,java.lang.String)
  32. -- ::37.289 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/search/{search}],methods=[GET],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.util.MultiValueMap<java.lang.String, java.lang.Object>,java.lang.String,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler,org.springframework.http.HttpHeaders)
  33. -- ::37.289 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/search],methods=[HEAD],produces=[application/hal+json || application/json]}" onto public org.springframework.http.HttpEntity<?> org.springframework.data.rest.webmvc.RepositorySearchController.headForSearches(org.springframework.data.rest.webmvc.RootResourceInformation)
  34. -- ::37.290 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/search/{search}],methods=[OPTIONS],produces=[application/hal+json || application/json]}" onto public org.springframework.http.ResponseEntity<java.lang.Object> org.springframework.data.rest.webmvc.RepositorySearchController.optionsForSearch(org.springframework.data.rest.webmvc.RootResourceInformation,java.lang.String)
  35. -- ::37.291 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/search/{search}],methods=[GET],produces=[application/x-spring-data-compact+json]}" onto public org.springframework.hateoas.ResourceSupport org.springframework.data.rest.webmvc.RepositorySearchController.executeSearchCompact(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.http.HttpHeaders,org.springframework.util.MultiValueMap<java.lang.String, java.lang.Object>,java.lang.String,java.lang.String,org.springframework.data.rest.webmvc.support.DefaultedPageable,org.springframework.data.domain.Sort,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler)
  36. -- ::37.291 INFO --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/{repository}/search],methods=[OPTIONS],produces=[application/hal+json || application/json]}" onto public org.springframework.http.HttpEntity<?> org.springframework.data.rest.webmvc.RepositorySearchController.optionsForSearches(org.springframework.data.rest.webmvc.RootResourceInformation)
  37. -- ::37.294 INFO --- [ main] o.s.d.r.w.BasePathAwareHandlerMapping : Mapped "{[/profile],methods=[GET]}" onto org.springframework.http.HttpEntity<org.springframework.hateoas.ResourceSupport> org.springframework.data.rest.webmvc.ProfileController.listAllFormsOfMetadata()
  38. -- ::37.295 INFO --- [ main] o.s.d.r.w.BasePathAwareHandlerMapping : Mapped "{[/profile],methods=[OPTIONS]}" onto public org.springframework.http.HttpEntity<?> org.springframework.data.rest.webmvc.ProfileController.profileOptions()
  39. -- ::37.295 INFO --- [ main] o.s.d.r.w.BasePathAwareHandlerMapping : Mapped "{[/profile/{repository}],methods=[GET],produces=[application/alps+json || */*]}" onto org.springframework.http.HttpEntity<org.springframework.data.rest.webmvc.RootResourceInformation> org.springframework.data.rest.webmvc.alps.AlpsController.descriptor(org.springframework.data.rest.webmvc.RootResourceInformation)
  40. -- ::37.295 INFO --- [ main] o.s.d.r.w.BasePathAwareHandlerMapping : Mapped "{[/profile/{repository}],methods=[OPTIONS],produces=[application/alps+json]}" onto org.springframework.http.HttpEntity<?> org.springframework.data.rest.webmvc.alps.AlpsController.alpsOptions()
  41. -- ::37.296 INFO --- [ main] o.s.d.r.w.BasePathAwareHandlerMapping : Mapped "{[/profile/{repository}],methods=[GET],produces=[application/schema+json]}" onto public org.springframework.http.HttpEntity<org.springframework.data.rest.webmvc.json.JsonSchema> org.springframework.data.rest.webmvc.RepositorySchemaController.schema(org.springframework.data.rest.webmvc.RootResourceInformation)
  42. -- ::37.465 INFO --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  43. -- ::37.602 INFO --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): (http)

发现多了很多rest api的映射,这表示成功了。

访问http://localhost:8000/notes/1,可以看到rest api已经直接可用了。hal+json 的格式,如果有疑问可留言。

Spring Boot-JPA的更多相关文章

  1. spring boot jpa 使用update 报错解决办法

    在spring boot jpa 中自定义sql,执行update操作报错解决办法: 在@Query(...)上添加 @Modifying@Transactional注解

  2. Spring Boot(五):Spring Boot Jpa 的使用

    在上篇文章Spring Boot(二):Web 综合开发中简单介绍了一下 Spring Boot Jpa 的基础性使用,这篇文章将更加全面的介绍 Spring Boot Jpa 常见用法以及注意事项. ...

  3. Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题

      (转载)Spring Boot + JPA(hibernate 5) 开发时,数据库表名大小写问题   这几天在用spring boot开发项目, 在开发的过程中遇到一个问题hibernate在执 ...

  4. Spring Boot Jpa 的使用

    Spring Boot Jpa 介绍 首先了解 Jpa 是什么? Jpa (Java Persistence API) 是 Sun 官方提出的 Java 持久化规范.它为 Java 开发人员提供了一种 ...

  5. (转)Spring Boot(五):Spring Boot Jpa 的使用

    http://www.ityouknow.com/springboot/2016/08/20/spring-boot-jpa.html 在上篇文章Spring Boot(二):Web 综合开发中简单介 ...

  6. Spring boot JPA 用自定义主键策略 生成自定义主键ID

    最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...

  7. Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例

    Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...

  8. spring boot JPA中实体类常用注解

    spring boot jpa中的注解很多,参数也比较多.没必要全部记住,但是经常查看官方文档也比较麻烦,记录一下一些常用的注解.通过一些具体的例子来帮助记忆. @Entity @Table(name ...

  9. spring boot + jpa + kotlin入门实例

    spring boot +jpa的文章网络上已经有不少,这里主要补充一下用kotlin来做. kotlin里面的data class来创建entity可以帮助我们减少不少的代码,比如现在这个User的 ...

  10. Spring Boot JPA 连接数据库

    本文将介绍怎样在Spring Boot project中加入JPA作为持久化方式. 改动 pom.xml 依赖 与上一篇介绍的 jdbc 不同的是 spring-boot-starter-jdbc 改 ...

随机推荐

  1. 如何给网站添加SSL证书(免费)

    上篇讲了如何将网站部署到服务器上,这篇就讲如何给网站添加SSL证书. 1.先到腾讯云ssl证书认证那里申请一个证书 2.DNS认证 3.下载解压nginx里面的文件 4. 在服务器上/www目录下创建 ...

  2. python递归查找文件目录

    # -*- coding:utf-8 -*- import os allfile = []def get_all_file(path): allfilelist = os.listdir(path) ...

  3. bzoj:1692 [Usaco2007 Dec]队列变换&&1640 [Usaco2007 Nov]Best Cow Line 队列变换

    Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席 ...

  4. Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)(A.思维题,B.思维题)

    A. Vicious Keyboard time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  5. hdu3076—概率dp

    hdu3076-概率dp 标签 : 概率dp 题目链接 题意: 2个人分别有AB的血数,轮流扔骰子,数小的自减一血,平的不变,谁先到减0, 谁输,问A赢的概率. 题解: dp[i][j]表示的是第一个 ...

  6. Travelling(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...

  7. Spring框架学习笔记(1)——HelloWorld

    1.创建一个新的项目,并添加Spring框架 2.创建HelloWorld.java package com.broadtext.beans.helloworld; public class Hell ...

  8. hash算法 (hashmap 实现原理)

    Hash ,一般翻译做" 散列" ,也有直接音译为" 哈希" 的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出 ...

  9. 我的第一个python web开发框架(21)——小结

    这个小网站终于成功上线,小白除了收获一笔不多的费用外,还得到女神小美的赞赏,心中满满的成就感.这一天下班后,他请老菜一起下馆子,兑现请吃饭的承诺,顺便让老菜点评一下. 小白:老大,在你的指导下终于完成 ...

  10. for循环,数组

    for (var a=0; a<10; a++ ) for循环 声明a为0 循环a=9时(10次) 每循环一次a的值+1; { 循环体 } var a=Attr();var a=[];数组; / ...