实体商品示例代码
package cn.maxhou.entity;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity//该注解表名该类为一个实体
public class Goods implements Serializable {
private static final long serialVersionUID = 5060150518287868308L;
@Id//id注解
@GeneratedValue(strategy=GenerationType.IDENTITY)//ID自增注解
private int id;
private String name;
private String specifications;
private BigDecimal price;
private int quantity;
@ManyToOne//表名商品和分类的关系是多对一
@JoinColumn(name = "classification_id")//设置加入类名
private Classification classification;
public Goods() {
}
public Goods(String name, String specifications, BigDecimal price, int quantity) {
super();
this.name = name;
this.specifications = specifications;
this.price = price;
this.quantity = quantity;
}
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;
}
public String getSpecifications() {
return specifications;
}
public void setSpecifications(String specifications) {
this.specifications = specifications;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Classification getClassification() {
return classification;
}
public void setClassification(Classification classification) {
this.classification = classification;
}
}
实体分类代码
package cn.maxhou.entity;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity//该注解表明该类是一个实体
public class Classification implements Serializable {
private static final long serialVersionUID = -7378975775604019219L;
@Id//I该注解表名改变量为数据库表ID
@GeneratedValue(strategy = GenerationType.IDENTITY)//id自增
private int id;
private String name;
private int pid;
@OneToMany(mappedBy = "classification" ,cascade = CascadeType.ALL )//表明分类和商品的关系是一对多,级联操作所有权限
private Set<Goods> goodes;
public Classification() {
}
public Classification(String name, int pid) {
super();
this.name = name;
this.pid = pid;
}
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;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
}
数据库操作类数据
package cn.maxhou.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import cn.maxhou.entity.Goods;
public interface GoodsRepository extends JpaRepository<Goods, Integer> {
}
实现JpaRepository接口后就能拥有增删改查的功能
驱动类
package cn.maxhou.demo;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import cn.maxhou.dao.ClassificationRepository;
import cn.maxhou.dao.GoodsRepository;
import cn.maxhou.entity.Classification;
import cn.maxhou.entity.Goods;
@SpringBootApplication
@EnableJpaRepositories(basePackages = "cn.maxhou.dao")//导入数据库操作类包注解
@EntityScan(basePackages = "cn.maxhou.entity")//导入实体包注解
public class Application implements CommandLineRunner {
@Autowired
private ClassificationRepository classificationRepository;
@Autowired
private GoodsRepository goodsRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
//清除表中已有数据
classificationRepository.deleteAllInBatch();//实现接口后调用删除所有方法
goodsRepository.deleteAllInBatch();//实现接口后调用删除所有方法
//新建分类
Classification classify = new Classification("总分类",0);
classificationRepository.save(classify);
Classification subClassify = new Classification("服装",1);
classificationRepository.save(subClassify);
//新建商品
Goods goods = new Goods("围巾","红色,1米5",new BigDecimal(200),300);
goods.setClassification(subClassify);
//Goods goods2 = new Goods("鞋子","43码黑色",new BigDecimal(388),100);
//goods.setClassification(classificationRepository.getOne(2));
goodsRepository.save(goods);
//goodsRepository.save(goods2);
}
}