SpringBoot使用JPA,创建web服务

阅读本文之前,首先请基于SpringBoot创建一个Web项目,参考文章http://www.jianshu.com/p/157eb1ab8524

技术框架

  • SpringBoot
  • Spring Data JPA
  • H2 DataBase
  • Maven

开发过程

1.在pom.xml中继续添加jpa和h2的依赖,使用内嵌数据库h2,不需要设置任何datasource属性,但是每次重启数据库都会被重置,一般用作演示最好不过了。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>
</dependency>

2.创建实体Person.java,实体类一般是通过myeclipse的hibernate插件反转功能生成的,或者其他的一些方式,不通的方式生成的实体类略有差别。

@Entity
public class Person implements java.io.Serializable {
  private static final long serialVersionUID = 18723482374628374L;
  private Long id;
  private String name;
  private int age;
  
  public Person(){
  }
  
  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  @Id
  @GeneratedValue(strategy = GenerationTYpe.AUTO)
  @Column(name= "id" unique = true, nullable = false)
  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }
  
  /*省略剩余的get set*/
}

3.创建PersonRespository.java

public interface PersonRespository extends JpaRepository<Person, Long> {
    Person findByName(String name);

    Person findByNameAndAge(String name, Integer age);

    @Query("from Person p where p.name=:name")
    Person findPerson(@Param("name")
    String name);
}

4.创建PersonController.java

@Controller
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonRespository personRespository;

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView();
        List<Person> plist = personRespository.findAll();
        view.addObject("plist", plist);
        view.setViewName("person/list");

        return view;
    }

    @RequestMapping("/add")
    public String add() {
        Person p = new Person();
        Random r = new Random();
        p.setAge(r.nextInt(50));
        p.setName("shu" + r.nextInt(50));
        personRespository.save(p);

        return "forward:/person/list";
    }
}

5.在src/main/resources/templates/person/下创建list.html

<!DOCTYPE HTML>
<html>
 <head lang="en"> 
  <meta charset="UTF-8" /> 
  <title></title> 
 </head> 
 <body> 
  <table border="1"> 
   <tbody>
    <tr> 
     <th>id</th> 
     <th>name</th> 
     <th>age</th> 
    </tr> 
    <tr th:each="item,iterStat:${plist}"> 
     <th th:text="${item.id}"></th> 
     <th th:text="${item.name}"></th> 
     <th th:text="${item.age}"></th> 
    </tr> 
   </tbody>
  </table> 
 </body>
</html>

6.运行方式,启动SpringBoot的主函数,Application.java的main函数
访问http://localhost:8080/myweb/person/add/ ,便可以看到运行结果

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,941评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,881评论 18 139
  • 最近发现很多朋友都在找关于runloop的资料,今天有幸总结了一下,希望可以在面试的过程中帮着到你。 runloo...
    best_su阅读 412评论 0 4
  • ##markdown
    凯牛阅读 161评论 0 0
  • 大家好我是来自青白江区华严小学致远校区五二班的唐蓉,我爱读书是一个书虫,就让我们一起领略书的海洋吧! 多看书不仅能...
    唐蓉妈妈阅读 478评论 0 0