這篇使用mongoDB來持久化資料
1.pom.xml增加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.application.properties加入連線設定
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=liongogo
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.username=root
spring.data.mongodb.password=xxxxxxxx
3.加入document(需注意id為String類型)
package com.liongogo.domain;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import javax.persistence.Id;
@Document(collection = "product")
public class Product {
@Id
private String id;
@Field("product_id")
private String productId;
@Field("product_name")
private String productName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
4.加入Repository
package com.liongogo.repository;
import com.liongogo.domain.Product;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface ProductRepository extends MongoRepository<Product, String> {
List<Product> findAll();
Product findOneByProductId(String pProductId);
}
5.加入Service
package com.liongogo.service;
import com.liongogo.domain.Product;
import com.liongogo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ProductService {
@Autowired
ProductRepository productRepository;
public void save(Product pProduct){
productRepository.save(pProduct);
}
public List<Product> findAll(){
return productRepository.findAll();
}
public Product findOneByProductId(String pProductId){
return productRepository.findOneByProductId(pProductId);
}
}
6.測試程式
package com.liongogo.controller;
import com.liongogo.domain.Product;
import com.liongogo.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/products/add")
public String add() {
Product tMewtwo = new Product();
tMewtwo.setProductId("76023");
tMewtwo.setProductName("蝙蝠裝甲車");
productService.save(tMewtwo);
Product tCharizard = new Product();
tCharizard.setProductId("76139");
tCharizard.setProductName("1989年蝙蝠車");
productService.save(tCharizard);
return "OK";
}
@RequestMapping("/products/findAll")
public Object findAll() {
return productService.findAll();
}
@RequestMapping("/products/76139")
public Object findOnde() {
return productService.findOneByProductId("76139");
}
}