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