构造器注入的方式给Cart属性赋值 关系1:1;1:n
阅读原文时间:2023年07月08日阅读:1

1、通过Spring创建对象,现有Users和Cart实体类,关系为1:1

属性注入的方式给Users属性赋值

2、Cart和Product实体类,关系1:n

构造器注入的方式给Cart属性赋值

Cart中包含Set、Map

1 package com.spring.entity; 2 3 public class Users { 4 private int id; 5 6 private String username; 7 8 private String password; 9 10 private Cart cart; 11 12 public Cart getCart() { 13 return cart; 14 } 15 16 public void setCart(Cart cart) { 17 this.cart = cart; 18 } 19 20 public int getId() { 21 return id; 22 } 23 24 public void setId(int id) { 25 this.id = id; 26 } 27 28 public String getUsername() { 29 return username; 30 } 31 32 public void setUsername(String username) { 33 this.username = username; 34 } 35 36 public String getPassword() { 37 return password; 38 } 39 40 public void setPassword(String password) { 41 this.password = password; 42 } 43 44 @Override 45 public String toString() { 46 return "Users{" + 47 "id=" + id + 48 ", username='" + username + '\'' + 49 ", password='" + password + '\'' + 50 ", cart=" + cart + 51 '}'; 52 } 53 }

Users

1 package com.spring.entity; 2 3 import java.util.Map; 4 import java.util.Set; 5 6 public class Cart { 7 private int id; 8 private int uid; 9 private int pcount; 10 private Set productSet; 11 private Map productMap; 12 13 public int getId() { 14 return id; 15 } 16 17 public void setId(int id) { 18 this.id = id; 19 } 20 21 public int getUid() { 22 return uid; 23 } 24 25 public void setUid(int uid) { 26 this.uid = uid; 27 } 28 29 public int getPcount() { 30 return pcount; 31 } 32 33 public void setPcount(int pcount) { 34 this.pcount = pcount; 35 } 36 37 public Set getProductSet() { 38 return productSet; 39 } 40 41 public void setProductSet(Set productSet) { 42 this.productSet = productSet; 43 } 44 45 public Map getProductMap() { 46 return productMap; 47 } 48 49 public void setProductMap(Map productMap) { 50 this.productMap = productMap; 51 } 52 53 @Override 54 public String toString() { 55 return "Cart{" + 56 "id=" + id + 57 ", uid=" + uid + 58 ", pcount=" + pcount + 59 ", productSet=" + productSet + 60 ", productMap=" + productMap + 61 '}'; 62 } 63 }

Cart

1 package com.spring.entity; 2 3 public class Product { 4 private Integer id; 5 6 private String pname; 7 8 private Double pprice; 9 10 private Integer pinventory; 11 12 private String picon; 13 14 private String plocation; 15 16 private Integer pviews; 17 18 public Product() { 19 } 20 21 public Product(Integer id, String pname, Double pprice, Integer pinventory, String picon, String plocation, Integer pviews) { 22 this.id = id; 23 this.pname = pname; 24 this.pprice = pprice; 25 this.pinventory = pinventory; 26 this.picon = picon; 27 this.plocation = plocation; 28 this.pviews = pviews; 29 } 30 31 @Override 32 public String toString() { 33 return "Product{" + 34 "id=" + id + 35 ", pname='" + pname + '\'' + 36 ", pprice=" + pprice + 37 ", pinventory=" + pinventory + 38 ", picon='" + picon + '\'' + 39 ", plocation='" + plocation + '\'' + 40 ", pviews=" + pviews + 41 '}'; 42 } 43 }

Product

1 2 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

applicationContext.xml

1 package com.spring.test; 2 3 import com.spring.entity.Cart; 4 import com.spring.entity.Users; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class TestSpring { 9 public static void main(String[] args) { 10 System.out.println("----Users对象的创建----------"); 11 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); 12 Users user1 = (Users) ac.getBean("u1"); 13 System.out.println(user1); 14 15 16 System.out.println("-------购物车对象的创建-------"); 17 Cart cart1 = (Cart) ac.getBean("c1"); 18 System.out.println(cart1); 19 } 20 }

TestSpring