模拟班级和学生 一对多的关系
- package com.atguigu.spring.pojo;
-
- public class Clazz {
- private Integer cid;
-
- private String cname;
-
- public Clazz() {
- }
-
- public Clazz(Integer cid, String cname) {
- this.cid = cid;
- this.cname = cname;
- }
-
- public Integer getCid() {
- return cid;
- }
-
- public void setCid(Integer cid) {
- this.cid = cid;
- }
-
- public String getCname() {
- return cname;
- }
-
- public void setCname(String cname) {
- this.cname = cname;
- }
-
- @Override
- public String toString() {
- return "Clazz{" +
- "cid=" + cid +
- ", cname='" + cname + '\'' +
- '}';
- }
- }
通过实体类来表示学生和班级之间的关系
对一:对应的是对象 对多:对应的是集合
具体做法:
在学生(多)里面设置一个班级(一)类型的属性 在设置get/set方法 重写tostring方法
- package com.atguigu.spring.pojo;
-
- public class Student implements Person {
-
- private Integer sid;
-
- private String sname;
-
- private Integer age;
-
- private String gender;
-
- private Double score;
-
- private Clazz clazz;
-
-
- public Student() {
- }
-
- public Student(Integer sid, String sname ,String gender , Integer age ) {
- this.sid = sid;
- this.sname = sname;
- this.age = age;
- this.gender = gender;
- }
-
-
- public Student(Integer sid, String sname, String gender, Double score) {
- this.sid = sid;
- this.sname = sname;
- this.gender = gender;
- this.score = score;
- }
-
- public Integer getSid() {
- return sid;
- }
-
- public void setSid(Integer sid) {
- this.sid = sid;
- }
-
- public String getSname() {
- return sname;
- }
-
- public void setSname(String sname) {
- this.sname = sname;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
-
- public Double getScore() {
- return score;
- }
-
- public void setScore(Double score) {
- this.score = score;
- }
-
-
-
-
- public Clazz getClazz() {
- return clazz;
- }
-
- public void setClazz(Clazz clazz) {
- this.clazz = clazz;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "sid=" + sid +
- ", sname='" + sname + '\'' +
- ", age=" + age +
- ", gender='" + gender + '\'' +
- ", score=" + score +
- ", clazz=" + clazz +
- '}';
- }
- }
-
不能够用value赋值 因为value对应的是字面量 而clazz 不是字面量,clazz对应的是一个对象
我们要给类类型赋值的话 我们有三种方式:
-
- <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="1111">property>
- <property name="cname" value="最强王者班">property>
- bean>
-
- <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
- <property name="sid" value="1004">property>
- <property name="sname" value="赵六">property>
- <property name="age" value="26">property>
- <property name="gender" value="男">property>
-
- <property name="clazz" ref="clazzOne">property>
- bean>
合起来:
-
- <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
- <property name="sid" value="1004">property>
- <property name="sname" value="赵六">property>
- <property name="age" value="26">property>
- <property name="gender" value="男">property>
-
- <property name="clazz" ref="clazzOne">property>
- bean>
-
-
- <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="1111">property>
- <property name="cname" value="最强王者班">property>
- bean>
测试
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- Student student = ioc.getBean("studentFive", Student.class);
- System.out.println(student);
- }
-
-
- <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
- <property name="sid" value="1004">property>
- <property name="sname" value="赵六">property>
- <property name="age" value="26">property>
- <property name="gender" value="男">property>
-
- <property name="clazz" ref="clazzOne">property>
- <property name="clazz.cid" value="2222">property>
- <property name="clazz.cname" value="远大前程班">property>
-
- bean>
-
-
- <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="1111">property>
- <property name="cname" value="最强王者班">property>
- bean>
测试
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- Student student = ioc.getBean("studentFive", Student.class);
- System.out.println(student);
- }
-
- <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
- <property name="sid" value="1004">property>
- <property name="sname" value="赵六">property>
- <property name="age" value="26">property>
- <property name="gender" value="男">property>
- <property name="clazz">
-
- <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="2222">property>
- <property name="cname" value="远大前程班">property>
- bean>
- property>
- bean>
-
-
- <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="1111">property>
- <property name="cname" value="最强王者班">property>
- bean>
测试:
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- Student student = ioc.getBean("studentFive", Student.class);
- System.out.println(student);
- }
内部bean能够通过ioc直接获取获取吗?
不能,内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取
测试:
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- // Student student = ioc.getBean("studentTwo", Student.class);
- Student student = ioc.getBean("studentFive", Student.class);
- Clazz clazz = ioc.getBean("clazzInner", Clazz.class);
- System.out.println(clazz);
- }
报错:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'clazzInner' available
没有一个bean叫做clazzInner被发现 没有一个叫做clazzInner的bean可用
available:可用
- package com.atguigu.spring.pojo;
-
- import java.util.Arrays;
-
- public class Student implements Person {
-
- private Integer sid;
-
- private String sname;
-
- private Integer age;
-
- private String gender;
-
- private Double score;
-
- private Clazz clazz;
-
- private String[] hobby;
-
-
-
-
- public Student() {
- }
-
- public Student(Integer sid, String sname ,String gender , Integer age ) {
- this.sid = sid;
- this.sname = sname;
- this.age = age;
- this.gender = gender;
- }
-
-
- public Student(Integer sid, String sname, String gender, Double score) {
- this.sid = sid;
- this.sname = sname;
- this.gender = gender;
- this.score = score;
- }
-
- public Integer getSid() {
- return sid;
- }
-
- public void setSid(Integer sid) {
- this.sid = sid;
- }
-
- public String getSname() {
- return sname;
- }
-
- public void setSname(String sname) {
- this.sname = sname;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
-
- public Double getScore() {
- return score;
- }
-
- public void setScore(Double score) {
- this.score = score;
- }
-
-
- public Clazz getClazz() {
- return clazz;
- }
-
- public void setClazz(Clazz clazz) {
- this.clazz = clazz;
- }
-
-
- public String[] getHobby() {
- return hobby;
- }
-
- public void setHobby(String[] hobby) {
- this.hobby = hobby;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "sid=" + sid +
- ", sname='" + sname + '\'' +
- ", age=" + age +
- ", gender='" + gender + '\'' +
- ", score=" + score +
- ", clazz=" + clazz +
- ", hobby=" + Arrays.toString(hobby) +
- '}';
- }
- }
-
-
- <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
- <property name="sid" value="1004">property>
- <property name="sname" value="赵六">property>
- <property name="age" value="26">property>
- <property name="gender" value="男">property>
-
- <property name="clazz">
-
- <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="2222">property>
- <property name="cname" value="远大前程班">property>
- bean>
- property>
-
-
- <property name="hobby">
- <array>
- <value>抽烟value>
- <value>喝酒value>
- <value>烫头value>
- array>
- property>
-
-
- bean>
-
-
- <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="1111">property>
- <property name="cname" value="最强王者班">property>
- bean>
- package com.atguigu.spring.pojo;
-
- import java.util.List;
-
- public class Clazz {
- private Integer cid;
-
- private String cname;
-
- private List
students; -
-
- public Clazz() {
- }
-
- public Clazz(Integer cid, String cname) {
- this.cid = cid;
- this.cname = cname;
- }
-
- public Integer getCid() {
- return cid;
- }
-
- public void setCid(Integer cid) {
- this.cid = cid;
- }
-
- public String getCname() {
- return cname;
- }
-
- public void setCname(String cname) {
- this.cname = cname;
- }
-
-
- public List
getStudents() { - return students;
- }
-
- public void setStudents(List
students) { - this.students = students;
- }
-
- @Override
- public String toString() {
- return "Clazz{" +
- "cid=" + cid +
- ", cname='" + cname + '\'' +
- ", students=" + students +
- '}';
- }
- }
-
- <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="1111">property>
- <property name="cname" value="最强王者班">property>
-
- <property name="students">
- <list>
- <ref bean="studentOne">ref>
- <ref bean="studentTwo">ref>
- <ref bean="studentThree">ref>
- list>
- property>
-
- bean>
测试:
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- Clazz clazz = ioc.getBean("clazzOne", Clazz.class);
- System.out.println(clazz);
另一种方式 配置一个集合类型的bean,需要使用util的约束
xmlns:util="http://www.springframework.org/schema/util"
-
- <bean id="clazzOne" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="1111">property>
- <property name="cname" value="最强王者班">property>
- <property name="students" ref="studentList">property>
-
-
-
- bean>
-
-
-
- <util:list id="studentList">
- <ref bean="studentOne">ref>
- <ref bean="studentTwo">ref>
- <ref bean="studentThree">ref>
- util:list>
测试
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- Clazz clazz = ioc.getBean("clazzOne", Clazz.class);
- System.out.println(clazz);
- package com.atguigu.spring.pojo;
-
- /**
- * Date:2022/7/1
- * Author:ybc
- * Description:
- */
- public class Teacher {
-
- private Integer tid;
-
- private String tname;
-
- @Override
- public String toString() {
- return "Teacher{" +
- "tid=" + tid +
- ", tname='" + tname + '\'' +
- '}';
- }
-
- public Integer getTid() {
- return tid;
- }
-
- public void setTid(Integer tid) {
- this.tid = tid;
- }
-
- public String getTname() {
- return tname;
- }
-
- public void setTname(String tname) {
- this.tname = tname;
- }
-
- public Teacher() {
- }
-
- public Teacher(Integer tid, String tname) {
- this.tid = tid;
- this.tname = tname;
- }
- }
-
- package com.atguigu.spring.pojo;
-
- import java.util.Arrays;
- import java.util.Map;
-
- public class Student implements Person {
-
- private Integer sid;
-
- private String sname;
-
- private Integer age;
-
- private String gender;
-
- private Double score;
-
- private Clazz clazz;
-
- private String[] hobby;
-
- private Map
teacherMap; -
-
-
-
- public Student() {
- }
-
- public Student(Integer sid, String sname ,String gender , Integer age ) {
- this.sid = sid;
- this.sname = sname;
- this.age = age;
- this.gender = gender;
- }
-
-
- public Student(Integer sid, String sname, String gender, Double score) {
- this.sid = sid;
- this.sname = sname;
- this.gender = gender;
- this.score = score;
- }
-
- public Integer getSid() {
- return sid;
- }
-
- public void setSid(Integer sid) {
- this.sid = sid;
- }
-
- public String getSname() {
- return sname;
- }
-
- public void setSname(String sname) {
- this.sname = sname;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getGender() {
- return gender;
- }
-
- public void setGender(String gender) {
- this.gender = gender;
- }
-
-
- public Double getScore() {
- return score;
- }
-
- public void setScore(Double score) {
- this.score = score;
- }
-
-
- public Clazz getClazz() {
- return clazz;
- }
-
- public void setClazz(Clazz clazz) {
- this.clazz = clazz;
- }
-
-
- public String[] getHobby() {
- return hobby;
- }
-
- public void setHobby(String[] hobby) {
- this.hobby = hobby;
- }
-
-
- public Map
getTeacherMap() { - return teacherMap;
- }
-
- public void setTeacherMap(Map
teacherMap) { - this.teacherMap = teacherMap;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "sid=" + sid +
- ", sname='" + sname + '\'' +
- ", age=" + age +
- ", gender='" + gender + '\'' +
- ", score=" + score +
- ", clazz=" + clazz +
- ", hobby=" + Arrays.toString(hobby) +
- ", teacherMap=" + teacherMap +
- '}';
- }
- }
-
- <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
- <property name="sid" value="1004">property>
- <property name="sname" value="赵六">property>
- <property name="age" value="26">property>
- <property name="gender" value="男">property>
- <property name="clazz">
-
- <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="2222">property>
- <property name="cname" value="远大前程班">property>
- bean>
- property>
- <property name="hobby">
- <array>
- <value>抽烟value>
- <value>喝酒value>
- <value>烫头value>
- array>
- property>
-
- <property name="teacherMap">
- <map>
- <entry key="10086" value-ref="teacherOne">entry>
- <entry key="10010" value-ref="teacherTwo">entry>
- map>
- property>
- bean>
-
-
- <bean id="teacherOne" class="com.atguigu.spring.pojo.Teacher">
- <property name="tid" value="10086">property>
- <property name="tname" value="大宝">property>
- bean>
-
- <bean id="teacherTwo" class="com.atguigu.spring.pojo.Teacher">
- <property name="tid" value="10010">property>
- <property name="tname" value="小宝">property>
- bean>
- beans>
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- Student student = ioc.getBean("studentFive", Student.class);
- System.out.println(student);
- }
-
- <bean id="studentFive" class="com.atguigu.spring.pojo.Student">
- <property name="sid" value="1004">property>
- <property name="sname" value="赵六">property>
- <property name="age" value="26">property>
- <property name="gender" value="男">property>
- <property name="clazz">
-
- <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
- <property name="cid" value="2222">property>
- <property name="cname" value="远大前程班">property>
- bean>
- property>
- <property name="hobby">
- <array>
- <value>抽烟value>
- <value>喝酒value>
- <value>烫头value>
- array>
- property>
- <property name="teacherMap" ref="teacherMap">property>
-
- bean>
-
-
- <bean id="teacherOne" class="com.atguigu.spring.pojo.Teacher">
- <property name="tid" value="10086">property>
- <property name="tname" value="大宝">property>
- bean>
-
- <bean id="teacherTwo" class="com.atguigu.spring.pojo.Teacher">
- <property name="tid" value="10010">property>
- <property name="tname" value="小宝">property>
- bean>
-
- <util:map id="teacherMap">
- <entry key="10086" value-ref="teacherOne">entry>
- <entry key="10010" value-ref="teacherTwo">entry>
- util:map>
- @Test
- public void testDI(){
- //获取IOC容器
- ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
- //获取bean
- Student student = ioc.getBean("studentFive", Student.class);
- System.out.println(student);
-
-
- }