第八周作业 struts,spring,jpa整合简单测试

suruts,Spring,jpa整合的简单测试

    首先说说jpa几个比较常用的注解。

      @ManyToOne(cascade=CascadeType.REFRESH) 表示多对一级联刷新
      @JoinColumn(name="brandid")本表的外键名称

   //optional=false意思是产品必须有产品类型吗,默认是true意思就是可有可无,设置为false就是必须有产品类型
  @ManyToOne(cascade=CascadeType.REFRESH,optional=false)
  @JoinColumn(name="typeid")//设置外键的名字

     option=false表示在多对一的一方是必须有的,默认是一的一方可有可无,即optional=true。

     

    @Enumerated(EnumType.STRING)表是该列是使用枚举类型,而且是字符串存入数据库。

   @OneToMany(cascade={CascadeType.REMOVE,CascadeType.PERSIST},mappedBy="product")

   表明是级联删除和级联保存,被映射的一端是另一个表的product字段

  下面展示一个简单的整合。

  实体类:

  1 package cn.hp.bean.product;
  2 
  3 import java.io.Serializable;
  4 import java.util.Date;
  5 import java.util.HashSet;
  6 import java.util.Set;
  7 
  8 import javax.persistence.CascadeType;
  9 import javax.persistence.Column;
 10 import javax.persistence.Entity;
 11 import javax.persistence.EnumType;
 12 import javax.persistence.Enumerated;
 13 import javax.persistence.GeneratedValue;
 14 import javax.persistence.Id;
 15 import javax.persistence.JoinColumn;
 16 import javax.persistence.Lob;
 17 import javax.persistence.ManyToOne;
 18 import javax.persistence.OneToMany;
 19 import javax.persistence.OrderBy;
 20 import javax.persistence.Temporal;
 21 import javax.persistence.TemporalType;
 22 import javax.persistence.Transient;
 23 
 24 @Entity
 25 public class ProductInfo implements Serializable{
 26     private static final long serialVersionUID = -8860864584425256200L;
 27     private Integer id;
 28     /** 货号 **/
 29     private String code;
 30     /** 产品名称 **/
 31     private String name;
 32     /** 品牌 **/
 33     private Brand brand;
 34     /** 型号 **/
 35     private String model;
 36     /** 底价(采购进来的价格) **/
 37     private Float baseprice;
 38     /** 市场价 **/
 39     private Float marketprice;
 40     /** 销售价 **/
 41     private Float sellprice;
 42     /** 重量 单位:克 **/
 43     private Integer weight;
 44     /** 产品简介 **/
 45     private String description;
 46     /** 购买说明 **/
 47     private String buyexplain;
 48     /** 是否可见 **/
 49     private Boolean visible = true;
 50     /** 产品类型 **/
 51     private ProductType type;
 52     /** 上架日期 **/
 53     private Date createdate = new Date();
 54     /** 人气指数 **/
 55     private Integer clickcount = 1;
 56     /** 销售量 **/
 57     private Integer sellcount = 0;
 58     /** 是否推荐 **/
 59     private Boolean commend = false;
 60     /** 性别要求 **/
 61     private Sex sexrequest = Sex.NONE;//默认知识没有性别要求的
 62     private Set<ProductStyle> styles=new HashSet<ProductStyle>();
 63     public ProductInfo(Integer id) {
 64         this.id = id;
 65     }
 66     public ProductInfo() {}
 67     
 68     @Transient
 69     public Float getSavedPrice(){
 70         return marketprice-sellprice;
 71     }
 72     
 73     /**
 74      * 从样式集合中删除指定样式
 75      * @param style
 76      */
 77     /**
 78      * 添加样式到样式集合
 79      * @param style
 80      */
 81     @Id @GeneratedValue
 82     public Integer getId() {
 83         return id;
 84     }
 85     public void setId(Integer id) {
 86         this.id = id;
 87     }
 88     //货号,默认允许为空
 89     @Column(length=30)
 90     public String getCode() {
 91         return code;
 92     }
 93     public void setCode(String code) {
 94         this.code = code;
 95     }
 96     //商品的名字长度为50,不为空
 97     @Column(length=50,nullable=false)
 98     public String getName() {
 99         return name;
100     }
101     public void setName(String name) {
102         this.name = name;
103     }
104     //表示的是级联
105     @ManyToOne(cascade=CascadeType.REFRESH)
106     @JoinColumn(name="brandid")
107     public Brand getBrand() {
108         return brand;
109     }
110     public void setBrand(Brand brand) {
111         this.brand = brand;
112     }
113     @Column(length=20)
114     public String getModel() {
115         return model;
116     }
117     public void setModel(String model) {
118         this.model = model;
119     }
120     @Column(nullable=false)
121     public Float getBaseprice() {
122         return baseprice;
123     }
124     public void setBaseprice(Float baseprice) {
125         this.baseprice = baseprice;
126     }
127     @Column(nullable=false)
128     public Float getMarketprice() {
129         return marketprice;
130     }
131     public void setMarketprice(Float marketprice) {
132         this.marketprice = marketprice;
133     }
134     @Column(nullable=false) 
135     public Float getSellprice() {
136         return sellprice;
137     }
138     public void setSellprice(Float sellprice) {
139         this.sellprice = sellprice;
140     }
141 
142     public Integer getWeight() {
143         return weight;
144     }
145     public void setWeight(Integer weight) {
146         this.weight = weight;
147     }
148     @Lob @Column(nullable=false)
149     public String getDescription() {
150         return description;
151     }
152     public void setDescription(String description) {
153         this.description = description;
154     }
155     @Column(length=30)
156     public String getBuyexplain() {
157         return buyexplain;
158     }
159     public void setBuyexplain(String buyexplain) {
160         this.buyexplain = buyexplain;
161     }
162     @Column(nullable=false)
163     public Boolean getVisible() {
164         return visible;
165     }
166     public void setVisible(Boolean visible) {
167         this.visible = visible;
168     }
169     //optional=false意思是产品必须有产品类型吗,默认是true意思就是可有可无,设置为false就是必须有产品类型
170     @ManyToOne(cascade=CascadeType.REFRESH,optional=false)
171     @JoinColumn(name="typeid")//设置外键的名字
172     public ProductType getType() {
173         return type;
174     }
175     public void setType(ProductType type) {
176         this.type = type;
177     }
178     //降价日期精确到天就可以了
179     @Temporal(TemporalType.DATE)
180     public Date getCreatedate() {
181         return createdate;
182     }
183     public void setCreatedate(Date createdate) {
184         this.createdate = createdate;
185     }
186     @Column(nullable=false)
187     public Integer getClickcount() {
188         return clickcount;
189     }
190     public void setClickcount(Integer clickcount) {
191         this.clickcount = clickcount;
192     }
193     @Column(nullable=false)
194     public Integer getSellcount() {
195         return sellcount;
196     }
197     public void setSellcount(Integer sellcount) {
198         this.sellcount = sellcount;
199     }
200     @Column(nullable=false)
201     public Boolean getCommend() {
202         return commend;
203     }
204     public void setCommend(Boolean commend) {
205         this.commend = commend;
206     }
207     @Enumerated(EnumType.STRING) @Column(length=5,nullable=false)
208     public Sex getSexrequest() {
209         return sexrequest;
210     }
211     public void setSexrequest(Sex sexrequest) {
212         this.sexrequest = sexrequest;
213     }
214     
215     @Override
216     public int hashCode() {
217         final int prime = 31;
218         int result = 1;
219         result = prime * result + ((id == null) ? 0 : id.hashCode());
220         return result;
221     }
222     @Override
223     public boolean equals(Object obj) {
224         if (this == obj)
225             return true;
226         if (obj == null)
227             return false;
228         if (getClass() != obj.getClass())
229             return false;
230         final ProductInfo other = (ProductInfo) obj;
231         if (id == null) {
232             if (other.id != null)
233                 return false;
234         } else if (!id.equals(other.id))
235             return false;
236         return true;
237     }
238     //级联保存的方法是CascadeType.PERSIST
239     @OneToMany(cascade={CascadeType.REMOVE,CascadeType.PERSIST},mappedBy="product")
240     public Set<ProductStyle> getStyles() {
241         return styles;
242     }
243     public void setStyles(Set<ProductStyle> styles) {
244         this.styles = styles;
245     }
246     //添加产品类型的方法
247     public void addProductStyle(ProductStyle style)
248     {
249         if(!this.styles.contains(style))
250         {
251             this.styles.add(style);
252             style.setProduct(this);//设置产品类型为本产品信息
253         }
254     }
255     
256     //从样式集合中删除指定的样式
257     public void removeProductType(ProductStyle style)
258     {
259         if(this.styles.contains(style))
260         {
261             this.styles.remove(style);
262             style.setProduct(null);
263         }
264     }
265 }
View Code

单元测试类:

 1 package junit.test;
 2 
 3 import org.junit.BeforeClass;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import cn.hp.bean.product.Brand;
 9 import cn.hp.bean.product.ProductInfo;
10 import cn.hp.bean.product.ProductStyle;
11 import cn.hp.bean.product.ProductType;
12 import cn.hp.bean.product.Sex;
13 import cn.hp.service.product.ProductInfoService;
14 
15 
16 public class ProductInfoTest {
17     private static ClassPathXmlApplicationContext cxt;
18     private static ProductInfoService productInfoService;
19 
20     @BeforeClass
21     public static void setUpBeforeClass() throws Exception {
22         try {
23             cxt = new ClassPathXmlApplicationContext("beans.xml");
24             productInfoService = (ProductInfoService)cxt.getBean("productInfoServiceBean");
25             System.out.println(productInfoService);
26         } catch (RuntimeException e) {
27             e.printStackTrace();
28         }
29     }
30     @Test
31     public void xxxx() {}
32     
33 //    @Test
34 //    public void testGetTopSell() {
35 //        List<ProductInfo> products = productInfoService.getTopSell(1, 2);
36 //        for(ProductInfo p : products){
37 //            System.out.println(p.getName());
38 //        }
39 //    }
40     
41     @Test
42     public void testSave() {
43         ProductInfo product = new ProductInfo();
44         product.setName("足球sss");
45         product.setBaseprice(100f);
46         product.setBrand(new Brand("17bf4b43-1f3c-44ca-ad52-a71388d6cdf9"));
47         product.setCode("UI002");
48         product.setDescription("好产品");
49         product.setMarketprice(600f);
50         product.setModel("T60");
51         product.setSellprice(300f);
52         product.setSexrequest(Sex.NONE);
53         product.addProductStyle(new ProductStyle("红色", "xxx.gif"));
54         product.setType(new ProductType(3));
55         product.setWeight(50);
56         productInfoService.save(product);
57         System.out.println(product.getId());
58     }
59 
60 }
View Code

测试的结果。

数据插入成功。搭建完成。

原文地址:https://www.cnblogs.com/he-123/p/5441596.html