SpringBoot入門二:CURD増删改查
34285 ワード
文書ディレクトリ
SpringBootはシステム環境を添削することを実現する:エディタ:Idea 2018.1オペレーティングシステム:win 7 maven:1.8.0_171
手順の概要:
ステップ
一、pom導入依存
pomにmysqlとjpaの依存を追加し、pomファイルを開き、dependenciesノードを見つけ、内部に次のコードを追加します.
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
dependencies>
二、アプリケーションpropertiesとデータベース構成
アプリケーションを開きます.properties、mysqlの構成を追加
spring.datasource.url=jdbc:mysql://localhost/test?characterEncoding=UTF8&characterSetResults=UTF8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
加characterXXX配置原因参考:Java操作Mysql時なぜ加characterXXX加serverTimezone原因参考:DataSource中加serverTimezone原因The server time zone value XXXエラー解決
三、studentsテーブルの作成
テーブル構造は、自己増加プライマリ・キーid、非空文字列nameのみを含む
CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=utf8
四、ファイルディレクトリ構造の作成
com.example.demoの下でディレクトリを作成する:controller、entity、repository、serviceの各ディレクトリの役割:controllerにルートentity担当データベースDaoマッピングrepositoryを追加する
五、記入コード
package com.example.demo.entity;
import javax.persistence.*;
@Entity
@Table(name = "students")
public class StudentEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public StudentEntity() {
}
public StudentEntity(String name) {
this.name = name;
}
public StudentEntity(StudentRequestEntity studentRequestEntity) {
this.name = studentRequestEntity.getName();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo.entity;
public class StudentRequestEntity{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo.repository;
import com.example.demo.entity.StudentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<StudentEntity, Integer> {
}
package com.example.demo.service;
import com.example.demo.entity.StudentEntity;
import com.example.demo.entity.StudentRequestEntity;
import com.example.demo.repository.StudentRepository;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class StudentService{
private StudentRepository studentRepository;
/**
* @Autowired , ,
* @param studentRepository
*/
public StudentService(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
public Optional<StudentEntity> findOne(int id){
StudentEntity studentEntity = new StudentEntity();
studentEntity.setId(id);
return studentRepository.findOne(Example.of(studentEntity));
}
public Optional<StudentEntity> findById(int id){
return studentRepository.findById(id);
}
public List<StudentEntity> findAll(){
return studentRepository.findAll();
}
public StudentEntity save(StudentRequestEntity studentRequestEntity){
StudentEntity studentEntity = Optional.of(studentRequestEntity).map(StudentEntity::new).get();
return studentRepository.save(studentEntity);
}
public StudentEntity save(String name, int id){
Optional<StudentEntity> optionalStudentEntity = studentRepository.findById(id);
if(optionalStudentEntity.isPresent()) {
StudentEntity studentEntity = optionalStudentEntity.get();
studentEntity.setName(name);
return studentRepository.save(studentEntity);
}
return null;
}
public void delete(int id){
studentRepository.deleteById(id);
}
}
package com.example.demo.controller;
import com.example.demo.entity.StudentEntity;
import com.example.demo.entity.StudentRequestEntity;
import com.example.demo.service.StudentService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
/**
* ,
*/
@RestController
public class StudentController {
private StudentService studentService;
public StudentController(StudentService studentService) {
this.studentService = studentService;
}
/**
* id
* @param id int
* @return
*/
@RequestMapping(value = "/student/{id}", method = RequestMethod.GET)
public Optional<StudentEntity> findById(@PathVariable int id) {
return studentService.findById(id);
}
/**
* id
* @param id int
* @return
*/
@RequestMapping(value = "/student/one", method = RequestMethod.GET)
public Optional<StudentEntity> findOne(@RequestParam int id){
return studentService.findOne(id);
}
/**
*
* @return
*/
@RequestMapping(value = "/student", method = RequestMethod.GET)
public List<StudentEntity> findAll(){
return studentService.findAll();
}
/**
*
* @param studentRequestEntity
* @return
*/
@RequestMapping(value = "/student", method = RequestMethod.POST)
public StudentEntity save(@RequestBody StudentRequestEntity studentRequestEntity){
return studentService.save(studentRequestEntity);
}
/**
* id
* @param id
*/
@RequestMapping(value = "/student/{id}", method = RequestMethod.DELETE)
public void delete(@PathVariable int id){
studentService.delete(id);
}
/**
* id
* @param name
* @param id
* @return
*/
@RequestMapping(value = "/student/{id}", method = RequestMethod.PUT)
public StudentEntity save(@RequestParam String name, @PathVariable int id){
return studentService.save(name, id);
}
/**
* id
* @param name
* @param id
* @return
*/
@RequestMapping(value = "/student/one/{id}", method = RequestMethod.PUT)
public StudentEntity saveOne(@RequestBody String name, @PathVariable int id){
return studentService.save(name, id);
}
}
六、コードを実行し、Restfulインタフェースをテストする
結果:略