首先引入maven依赖:
<!-- spring-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
创建好数据库springboot,配置jpa数据源,application.yml:
server:
port: 8080
context-path: /spring-boot
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot
username: root
password:
jpa:
hibernate:
ddl-auto: update
show-sql: true
接下来我们创建实体类,将其映射到数据库表中:
package cn.qblank.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Author {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
private String address;
public Author() {
}
public Author(String name, Integer age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
//省略getter和setter
}
然后运行,自动建立表格(配置中ddl-auto在建表时改为create,建立完成后改为update),生成对应的表格,我们插入测试数据:
然后我们开始进行jpa的测试,创建一个接口AuthorRepository继承JpaRepository
package cn.qblank.entity;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AuthorRepository extends JpaRepository<Author, Integer>{
/**
* 自己扩展的方法
* @param name
* @return
*/
public List<Author> findByName(String name);
}
创建AuthorController类进行测试:
package cn.qblank.controller;
@RestController
public class AuthorController {
@Autowired
private AuthorRepository authorRepository;
/**
* 查询所有作者
* @return
*/
@GetMapping(value = "/authors")
public List<Author> authorList(){
return authorRepository.findAll();
}
/**
* 添加
* @param name
* @param age
* @param address
* @return
*/
@PostMapping(value = "/authorAdd")
public Author authorAdd(@RequestParam("name") String name,
@RequestParam("age") Integer age,@RequestParam("address") String address) {
Author author = new Author();
author.setName(name);
author.setAge(age);
author.setAddress(address);
return authorRepository.save(author);
}
/**
* 删除
* @param id
*/
@PutMapping(value = "/deleteAuthor")
public void deleteAuthor(@PathVariable("id") Integer id) {
authorRepository.delete(id);
}
/**
* 修改
* @param id
* @param name
* @param age
* @param address
*/
@PutMapping(value = "/updateAuthor")
public void updateAuthor(@RequestParam("id") Integer id,@RequestParam("name") String name,
@RequestParam("age") Integer age,@RequestParam("address") String address) {
Author author = new Author();
author.setId(id);
author.setName(name);
author.setAge(age);
author.setAddress(address);
authorRepository.save(author);
}
/**
* 通过id查询
* @param id
* @return
*/
@GetMapping(value = "/findAuthor/{id}")
@ResponseBody
public Author findAuthorById(@PathVariable("id") Integer id) {
System.out.println(id);
return authorRepository.findOne(id);
}
/**
* 通过名称查找信息
* @param name
* @return
*/
@GetMapping(value = "/findAuthorName/{name}")
@ResponseBody
public List<Author> findAuthorByName(@PathVariable("name") String name){
return authorRepository.findByName(name);
}
}
运行结果如下:
通过姓名查找
通过id查找:
查找全部: