2-hour Learning Spring boot learning-database operations

Source: Internet
Author: User

SPRING-DATA-JPA integration is hibernate.

The JPA (Java persistence API) defines a set of criteria for object persistence, and the products that currently implement this specification are hibernate toplink, etc.

MySQL Start command mysql.server start

1. Add Dependent Pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

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

2. Modify the configuration Application.yml
 
DataSource:
Driver-class-name:com.mysql.jdbc.driver
Url:jdbc:mysql://127.0.0.1:3306/dbgirl
Username:root
Password:after
JPA:
Hibernate:
Ddl-auto:update//create re-created
SHOW-SQL:TRUE

3. Create a new object class that automatically corresponds to the property field in the Build database
Girl.java

@Entity  //Add this note
public class Girl {
@Id//id
@GeneratedValue//self-increment
Private Integer ID;
Private String cupsize;
Private Integer age;

Public Girl () {
}

Public Integer getId () {
return ID;
}

public void SetId (Integer id) {
This.id = ID;
}

Public String getcupsize () {
return cupsize;
}

public void Setcupsize (String cupsize) {
This.cupsize = cupsize;
}

Public Integer Getage () {
return age;
}

public void Setage (Integer age) {
This.age = age;
}
}




Girlreposistory.java
jparepository<Girl,Integer> {
Search by age
Public list<girl> Findbyage (Integer age); This interface is an extension interface, note naming rules Findby[xxx]
}
5. Create a new controller class and complete the annotations
@RestController
@RequestMapping (value = "/girls")
public class Girlcontroller {

@Autowired
private girlreposistory girlreposistory;

Get a list of girls
@GetMapping (value = "/list")
Public list<girl> girllist () {
ReturnGirlreposistory.findall ();
}

Add a Girl
@PostMapping (value = "/add")
Public Girl Girladd (@RequestParam (value = "Cupsize") String cupsize,
@RequestParam (value = "Age"), Integer age) {
Girl Girl = new Girl ();
Girl.setcupsize (cupsize);
Girl.setage (age);

Returngirlreposistory.save (girl);
}

Find a girl by ID
@GetMapping (value = "/search/{id}")
Public Girl Girlsearch (@PathVariable ("id") Integer ID) {
ReturnGirlreposistory.findbyid (ID). get ();
}

Update
@PutMapping (value = "/update/{id}")
Public Girl girlupdate (@PathVariable ("id") Integer ID,
@RequestParam ("Cupsize") String cupsize,
@RequestParam ("Age"), Integer age) {
Girl Girl = new Girl ();
Girl.setid (ID);
Girl.setcupsize (cupsize);
Girl.setage (age);

Returngirlreposistory.save (girl);
}

Delete
@DeleteMapping (value = "/delete/{id}")
public void Girldelete (@PathVariable ("id") Integer ID) {
Girlreposistory.deletebyid (ID);
}

Find a list of girls by age
@GetMapping (value = "/age/{age}")
Public list<girl> girllistbyage (@PathVariable (' age ') Integer age) {
ReturnGirlreposistory.findbyage (age);
}

}











2-hour Learning Spring boot learning-database operations

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.