spring-data-jpa operation of the database

1, spring-data-jpa spring integration is to hibernate
2, spring boot project to add annotations in the entity class, add two dependencies, write the configuration file, can automatically generate a database table
Entity classes:
@Entity // Create a data table based on the class mark
public class People {
@Id
@GeneratedValue // increment primary keys
private Long id;
private String name;
People public () { // no-argument constructor must
}
}
 
application.yml default configuration file:
# The default configuration file
spring:
profiles:
active: default
datasource: # Configuration Database
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/hap_dev
username: root
password: tiger
jpa:
hibernate:
ddl-auto: automatically create database tables based on entity classes create # runtime
show-sql: true
 
note:
ddl-auto: create, i.e. automatically created based on the runtime entity class people database tables, the database tables if
people it exists delete and then create
ddl-auto: update, i.e. automatically created based on the runtime entity class people database tables, the database tables if
people already there is no operation
ddl-auto: create-drop, according to the entity class that is created automatically run when people database table, when the application stops
When only delete the table
ddl-auto: none,即运行时什么也不干,无数据库表的操作
ddl-auto: validate,即运行时验证实体类属性与数据库表字段是否一致,不一致
则抛异常
 
 
pom.xml依赖(基本的spring boot依赖要有)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
 
3、由spring-data-jpa操作数据库(结合restful风格)
Service接口类:
// 继承jpa操作数据库的接口,并指定实体类名和主键类型
public interface PeopleRepository extends JpaRepository<People,Long> {
public List<People> findByName(String name);//自定义的方法命名要符合这个格式
}
Controller类:
@RestController
public class PeopleController {
@Autowired
private PeopleRepository peopleRepository;
 
//查询people表所有数据
@GetMapping("/peoples")
public List<People> getAll(){
return peopleRepository.findAll();
}
 
// 插入一条数据
@PostMapping("/peoples")
public People addOne(@RequestParam("name") String name){
People p=new People();
p.setName(name);
return peopleRepository.save(p);
}
 
//由id获取一条数据
@GetMapping("/peoples/{id}")
public People getOne(@PathVariable("id") Long id){
return peopleRepository.findOne(id);
}
 
//由id更新、插入一条数据
@PutMapping("/peoples/{id}")
public People putOne(@PathVariable("id") Long id,@RequestParam("name") String name){
People p=new People();
p.setId(id);
p.setName(name);
return peopleRepository.save(p);
}
 
// 由id删除一条数据
@DeleteMapping("/peoples/{id}")
public void delOne(@PathVariable("id") Long id){
peopleRepository.delete(id);
}
 
//通过name查询一条或多条数据
@GetMapping("/peoples/name/{name}")
public List<People> findByName(@PathVariable("name") String name){
return peopleRepository.findByName(name);
}
}
 
 
 
 

Guess you like

Origin www.cnblogs.com/afei1759/p/11286040.html