Spring Boot series Four: Spring Boot integrated JPA

Source: Internet
Author: User
Tags findone

We talked about the spring boot integration jdbctemplate for data persistence,

In this article, we will be using spring boot to integrate JPA to achieve the persistence of data.

First, the code implementation

  1. Modify Pom to introduce dependency
     <!--  Introducing JPA dependency  -->  <  dependency  Span style= "COLOR: #0000ff" >>  <  gr Oupid  >  org.springframework.boot</  groupid  >  <  artifactid  >  Spring-boot-starter-data-jpa</ artifactid  >  </ dependency  >  
  2. Modify Application.properties, configure related information
    #修改tomcat默认端口号server. port=8090# Modifying the context pathserver.context-path=/test# Configuring data source Information Spring.datasource.driver-class-name=com.mysql.jdbc.driverspring.datasource.url=jdbc:mysql://localhost : 3306/testspring.datasource.username=rootspring.datasource.password=root# Configuration jpaspring.jpa.hibernate.ddl-auto= Updatespring.jpa.show-sql=truespring.jackson.serialization.indent_output=true
  3. Create an entity class
     Packagecom.study.entity;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;Importjavax.persistence.Table; @Entity @table (name= "T_user") Public classUser {@Id @GeneratedValue (strategy=Generationtype.auto)PrivateInteger ID; PrivateString UserName; PrivateString password;  PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}
  4. Create the Repository interface and inherit the Crudrepository
     Packagecom.study.repository;ImportOrg.springframework.data.jpa.repository.Query;Importorg.springframework.data.repository.CrudRepository;ImportOrg.springframework.data.repository.query.Param;ImportCom.study.entity.User;/*** Note: * 1. Here is interface, not class * * 2.CrudRepository inside the generics, the first is the entity class, the second is the type of the primary key * 3. Since there are already some interfaces in crudrepository, such as Del Eteall,findone and so on, we call directly can * 4. Of course, we can also implement their own interface according to their own situation, such as the following GetUser () method, JPQL statement and HQL statement almost * **/ Public InterfaceUserrepositoryextendsCrudrepository<user, integer> {    /*** We only need to write the interface here, do not need to write the implementation, spring boot will help automatically implement * **/@Query ("From User where ID =:id")     PublicUser GetUser (@Param ("id") (Integer ID);}
  5. Create service
    1. Interface
       Package Com.study.service; Import Com.study.entity.User;  Public Interface UserService {    public  User getUser (Integer ID);}
    2. Realize
       PackageCom.study.service.impl;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;ImportCom.study.entity.User;Importcom.study.repository.UserRepository;ImportCom.study.service.UserService; @Service Public classUserserviceimplImplementsuserservice {@Autowired userrepository repository; @Override PublicUser GetUser (Integer id) {//There are two ways of doing this://1. Calling the Crudrepository interface//return Repository.findone (ID); //2. Call our own Write interface        returnRepository.getuser (ID); }    }
  6. Create a Controller
     PackageCom.study.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.study.entity.User;ImportCom.study.service.UserService; @RestController Public classUsercontroller {@Autowired userservice service; @RequestMapping ("/getuser/{id}")     PublicUser GetUser (@PathVariable ("id") (Integer ID) {returnService.getuser (ID); }}
  7. Test, page displays database values in JSON format

    

Ii. extension of knowledge points

About the repository knowledge point, you can go to the following article

1190000012346333

Spring Boot series Four: Spring Boot integrated JPA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.