这个Demo非常基础,用来学习和调试使用。
一、IDEA新建一个Spring Initializer项目,并将JPA和MySQL加入Starter中。依赖如下:
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
二、修改配置文件application.properties
server.port=8080
server.servlet.context-path=/spring-data-jpa-demo
#数据库
spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
三、开始业务代码
3.1 实体类:
package com.yuxx.springboot.springdatajpademo.entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="person")
public class Person implements Serializable {
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Id
private Long id;
private String name;
private Integer age;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
public Person(Long id, String name, Integer age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public Person() {
}
}
3.2 Repository接口
package com.yuxx.springboot.springdatajpademo.repository;
import com.yuxx.springboot.springdatajpademo.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface PersonRepository extends JpaRepository {
//根据名称准确查询
List findByName(String name);
}
3.3 Controller
package com.yuxx.springboot.springdatajpademo.controller;
import com.yuxx.springboot.springdatajpademo.entity.Person;
import com.yuxx.springboot.springdatajpademo.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonRepository personRepository;
@RequestMapping(value = "/hello")
public String hello(){
return "Hello!";
}
@RequestMapping(value = "/save")
public Person savePerson(String name,String address,Integer age){
return personRepository.save(new Person(null,name,age,address));
}
@RequestMapping(value = "/findByName")
public List findByName(String name){
return personRepository.findByName(name);
}
}
四、JPA配置类
package com.yuxx.springboot.springdatajpademo;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@EnableJpaRepositories("com.yuxx.springboot.springdatajpademo.repository")
public class JpaConfiguration {
}
五、数据库表设置
六、项目启动
七、测试
7.1 新增数据
7.2 根据姓名查询