前一章使用JDBC確定能與資料庫相連後,此章節使用JPA進行CRUD操作
1.pom.xml加入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.資料庫新增(需注意此處主鍵使用AUTO_INCREMENT
)
CREATE TABLE IF NOT EXISTS pokemon
(
id bigint(20) NOT NULL AUTO_INCREMENT COMMENT '識別碼',
pokemon_id VARCHAR(200) COMMENT '寶可夢英文',
pokemon_name VARCHAR(200) COMMENT '寶可夢名稱',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=UTF8MB4_BIN;
3.新增domain(需注意此處主鍵使用生成策略GenerationType.IDENTITY
)
package com.liongogo.domain;
import javax.persistence.*;
@Entity
@Table(name="pokemon")
public class Pokemon {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "pokemon_id")
private String pokemonId;
@Column(name = "pokemon_name")
private String pokemonName;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPokemonId() {
return pokemonId;
}
public void setPokemonId(String pokemonId) {
this.pokemonId = pokemonId;
}
public String getPokemonName() {
return pokemonName;
}
public void setPokemonName(String pokemonName) {
this.pokemonName = pokemonName;
}
}
3.增加Repository
package com.liongogo.repository;
import com.liongogo.domain.Pokemon;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface PokemonRepository extends JpaRepository<Pokemon, Long> {
List<Pokemon> findAll();
Pokemon findOneByPokemonId(String PokemonId);
}
4.增加Service
package com.liongogo.service;
import com.liongogo.domain.Pokemon;
import com.liongogo.domain.User;
import com.liongogo.repository.PokemonRepository;
import com.liongogo.repository.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PokemonService {
@Autowired
PokemonRepository pokemonRepository;
public void save(Pokemon pPokemon){
pokemonRepository.save(pPokemon);
}
public List<Pokemon> findAll(){
return pokemonRepository.findAll();
}
public Pokemon findOneByPokemonId(String pPokemonId){
return pokemonRepository.findOneByPokemonId(pPokemonId);
}
}
5.測試接口調用
package com.liongogo.controller;
import com.liongogo.domain.Pokemon;
import com.liongogo.domain.User;
import com.liongogo.service.PokemonService;
import com.liongogo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PokemonController {
@Autowired
PokemonService pokemonService;
@RequestMapping("/pokemons/add")
public String add() {
Pokemon tMewtwo = new Pokemon();
tMewtwo.setPokemonId("Mewtwo");
tMewtwo.setPokemonName("超夢");
pokemonService.save(tMewtwo);
Pokemon tCharizard = new Pokemon();
tCharizard.setPokemonId("Charizard");
tCharizard.setPokemonName("噴火龍");
pokemonService.save(tCharizard);
return "OK";
}
@RequestMapping("/pokemons/findAll")
public Object findAll() {
return pokemonService.findAll();
}
@RequestMapping("/pokemons/Mewtwo")
public Object findOnde() {
return pokemonService.findOneByPokemonId("Mewtwo");
}
}
6.可以在application.properties增加以下設定幫助排查問題
# 顯示Hibernate自動組成的SQL語法
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true