做网站的三个软件,网站建设一般满足什么需求,wordpress大图片,西安网评论以上笔记来源#xff1a; 尚硅谷Spring零基础入门到进阶#xff0c;一套搞定spring6全套视频教程#xff08;源码级讲解#xff09;https://www.bilibili.com/video/BV1kR4y1b7Qc 12 依赖注入之注入Map集合类型属性
12.1 创建Student类和Teacher类
Student类中创建了run…以上笔记来源 尚硅谷Spring零基础入门到进阶一套搞定spring6全套视频教程源码级讲解https://www.bilibili.com/video/BV1kR4y1b7Qc 12 依赖注入之注入Map集合类型属性
12.1 创建Student类和Teacher类
Student类中创建了run方法展示了被注入的普通属性和map属性的值
Teacher类则创建了map类型的getter,setter方法以及方便输出的tostring方法
package com.atguigu.spring6.iocxml.dimap;import java.util.Map;/*** package: com.atguigu.spring6.iocxml.dimap* className: Student* Description:* author: haozihua* date: 2024/8/20 15:20*/
public class Student {//一个学生对应很多个老师一个老师也可以对应很多个学生private MapString,Teacher teacherMap;private String sid;private String sname;public String getSid() {return sid;}public void setSid(String sid) {this.sid sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname sname;}public MapString, Teacher getTeacherMap() {return teacherMap;}public void setTeacherMap(MapString, Teacher teacherMap) {this.teacherMap teacherMap;}public void run() {System.out.println(学生编号 sid 学生名称 sname);System.out.println(teacherMap);}
}
package com.atguigu.spring6.iocxml.dimap;/*** package: com.atguigu.spring6.iocxml.dimap* className: Teacher* Description:* author: haozihua* date: 2024/8/20 15:21*/
public class Teacher {private Integer teacherId;private String teacherName;public Integer getTeacherId() {return teacherId;}public void setTeacherId(Integer teacherId) {this.teacherId teacherId;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName teacherName;}Overridepublic String toString() {return Teacher{ teacherId teacherId , teacherName teacherName \ };}public Teacher(Integer teacherId, String teacherName) {this.teacherId teacherId;this.teacherName teacherName;}public Teacher() {}}
12.2 创建XML配置文件
由于在Student类中创建了关于Teacher类的map集合因此在student的bean中配置信息首先创建普通类型属性根据实体类中的变量名称设置name属性值
之后创建Map集合类型属性时注意entrykeyvalue或者ref标签的嵌套
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--1.创建两个类2.注入普通类型属性3.在学生bean注入map集合类型属性--bean idteacherone classcom.atguigu.spring6.iocxml.dimap.Teacherproperty nameteacherId value100/propertyproperty nameteacherName value西门庆/property/beanbean idteachertwo classcom.atguigu.spring6.iocxml.dimap.Teacherproperty nameteacherId value200/propertyproperty nameteacherName value东方不败/property/beanbean idstudent classcom.atguigu.spring6.iocxml.dimap.Studentproperty namesid value2000/propertyproperty namesname value李四/propertyproperty nameteacherMapmapentrykeyvalue10010/value/keyref beanteacherone/ref/entryentrykeyvalue210/value/keyref beanteachertwo/ref/entry/map/property/bean
/beans
12.3 创建测试类方法 Testpublic void TestStu(){ApplicationContext context new ClassPathXmlApplicationContext(bean-dimap.xml);Student student context.getBean(student,Student.class);student.run();}
12.4 运行截图