当前位置: 首页 > news >正文

【SSM框架】为集合类型属性赋值

🍓个人主页:个人主页

🍒系列专栏:SSM框架

目录

1.为集合类型属性赋值

①为List集合类型属性赋值

②为Map集合类型属性赋值

2.p命名空间

3.引入外部属性文件 


1.为集合类型属性赋值

①为List集合类型属性赋值

Clazz 类中添加以下代码:
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
方法1:配置bean
若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可
    <bean id="clazzTwo" class="com.atguigu.spring.pojo.Clazz">
        <property name="clazzId" value="1"></property>
        <property name="clazzName" value="计算机科学与技术"></property>
        <property name="students">
            <list>
                <ref bean="studentOne"></ref>
                <ref bean="studentTwo"></ref>
                <ref bean="studentThree"></ref>
            </list>
        </property>
    </bean>

    <bean id="studentOne" class="com.atguigu.spring.pojo.Student">
        <property name="id" value="1001"></property>
        <property name="name" value="赵1"></property>
        <property name="age" value="22"></property>
        <property name="sex" value="女"></property>

        <property name="hobbies">
            <array>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>打豆豆</value>
            </array>
        </property>
    </bean>
    <bean id="studentTwo" class="com.atguigu.spring.pojo.Student">
        <property name="id" value="1002"></property>
        <property name="name" value="赵2"></property>
        <property name="age" value="24"></property>
        <property name="sex" value="女"></property>

        <property name="hobbies">
            <array>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>打豆豆</value>
            </array>
        </property>
    </bean>

    <bean id="studentThree" class="com.atguigu.spring.pojo.Student">
        <property name="id" value="1004"></property>
        <property name="name" value="赵3"></property>
        <property name="age" value="21"></property>
        <property name="sex" value="女"></property>

        <property name="hobbies">
            <array>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>打豆豆</value>
            </array>
        </property>

        
    </bean>

测试:

    @org.junit.Test
    public void testHelloWorld(){

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Clazz clazzTwo = ac.getBean("clazzTwo", Clazz.class);
        System.out.println(clazzTwo);


   

方法2:配置一个集合类型的bean,需要使用util 的约束-

    <bean id="clazzTwo" class="com.atguigu.spring.pojo.Clazz">
        <property name="clazzId" value="1"></property>
        <property name="clazzName" value="计算机科学与技术"></property>
        <property name="students" ref="studentsList">
        </property>
    </bean>
    <util:list id="studentsList">
        <ref bean="studentOne"></ref>
        <ref bean="studentTwo"></ref>
        <ref bean="studentThree"></ref>
    </util:list>

效果一样的:

②为Map集合类型属性赋值

创建教师类Teacher

public class Teacher {
    private Integer tid;
    private String tname;

    public Teacher() {
    }

    public Teacher(Integer tid, String tname) {
        this.tid = tid;
        this.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;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", tname='" + tname + '\'' +
                '}';
    }
}

Student类中添加以下代码:
private Map<String, Teacher> teacherMap;
public Map<String, Teacher> getTeacherMap() {
return teacherMap;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {
this.teacherMap = teacherMap;
}
方法1:配置bean( 引用集合类型的 bean
    <bean id="student" class="com.atguigu.spring.pojo.Student">
        <property name="id" value="1004"></property>
        <property name="name" value="赵六"></property>
        <property name="age" value="26"></property>
        <property name="sex" value="女"></property>

        <property name="clazz">
            <!-- 在一个bean中再声明一个bean就是内部bean -->
            <!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 -->
            <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
                <property name="clazzId" value="2"></property>
                <property name="clazzName" value="软件工程"></property>
            </bean>
        </property>

        <property name="hobbies">
            <array>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>打豆豆</value>
            </array>
        </property>
        <property name="teacherMap">
            <map>
                <entry key="1" value-ref="teacherOne"></entry>
                <entry key="2" value-ref="teacherTwo"></entry>
            </map>
        </property>
    </bean>


    <bean id="teacherOne" class="com.atguigu.spring.pojo.Teacher">
        <property name="tid" value="11111"></property>
        <property name="tname" value="小王"></property>

    </bean>
    <bean id="teacherTwo" class="com.atguigu.spring.pojo.Teacher">
        <property name="tid" value="22222"></property>
        <property name="tname" value="小李"></property>

    </bean>

 测试:

    public void testHelloWorld(){

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = ac.getBean("student", Student.class);
        System.out.println(student);


    }

方法2:配置bean(util:map):

    <bean id="student" class="com.atguigu.spring.pojo.Student">
        <property name="id" value="1004"></property>
        <property name="name" value="赵六"></property>
        <property name="age" value="26"></property>
        <property name="sex" value="女"></property>

        <property name="clazz">
            <!-- 在一个bean中再声明一个bean就是内部bean -->
            <!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 -->
            <bean id="clazzInner" class="com.atguigu.spring.pojo.Clazz">
                <property name="clazzId" value="2"></property>
                <property name="clazzName" value="软件工程"></property>
            </bean>
        </property>

        <property name="hobbies">
            <array>
                <value>吃饭</value>
                <value>睡觉</value>
                <value>打豆豆</value>
            </array>
        </property>
        <property name="teacherMap" ref="teacherMap">
        </property>
    </bean>

    <util:map id="teacherMap">
        <entry key="1" value-ref="teacherOne"></entry>
        <entry key="2" value-ref="teacherTwo"></entry>
    </util:map>

2.p命名空间

引入p命名空间后,可以通过以下方式为bean的各个属性赋值

    <bean id="studentT" class="com.atguigu.spring.pojo.Student" p:name="李王" p:id="1009" p:teacherMap-ref="teacherMap">

    </bean>

3.引入外部属性文件 

①加入依赖

<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
②创建外部属性文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm? serverTimezone=UTC
jdbc.username=root
jdbc.password=root
③引入属性文件
 <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
④配置 bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd">


    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    
</bean>




</beans>
⑤测试

    public void test(){
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("spring-datasource.xml");
        DruidDataSource bean = ioc.getBean(DruidDataSource.class);
        try {
            System.out.println(bean.getConnection());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

相关文章:

  • 不想写日报、周报,这个报表自动化软件太牛了,仅需三分钟
  • 14:30面试,14:38就出来了 ,问的实在是太...
  • electron-vue项目从搭建、运行到打包(以及electron-vue的bug修改)
  • 使用小爱同学语音控制电脑关机 - Winform C#
  • [附源码]Python计算机毕业设计仿咸鱼二手物品交易系统Django(程序+LW)
  • 31.前端笔记-CSS-CSS3盒子模型和其他特性
  • C语言split分割字符串
  • Python篇之编译py文件为pyc文件的方法总结
  • Windows学习总结(25)—— Windows 11 cmd 命令大全
  • 识破贷后资金归集——关联网络
  • 关于sysdiag的利用
  • 【推送位置苹果群发iMessage推】如果Windows和Linux实现不同的传输层协议,那末因为数据格式的不同
  • 12.6、后渗透测试--Windows系统下信息收集模块
  • 含参PDE(偏微分方程)的神经网络并行编程mpi4py
  • C/C++程序的断点调试 - Visual Studio Code
  • 分享一款快速APP功能测试工具
  • Apache的80端口被占用以及访问时报错403
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • golang 发送GET和POST示例
  • Gradle 5.0 正式版发布
  • HashMap剖析之内部结构
  • Hibernate【inverse和cascade属性】知识要点
  • IndexedDB
  • JAVA_NIO系列——Channel和Buffer详解
  • java中具有继承关系的类及其对象初始化顺序
  • Linux各目录及每个目录的详细介绍
  • Netty 4.1 源代码学习:线程模型
  • Phpstorm怎样批量删除空行?
  • PHP面试之三:MySQL数据库
  • Python 基础起步 (十) 什么叫函数?
  • Spark RDD学习: aggregate函数
  • spring security oauth2 password授权模式
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • 测试开发系类之接口自动化测试
  • 理解IaaS, PaaS, SaaS等云模型 (Cloud Models)
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 深度解析利用ES6进行Promise封装总结
  • 通过git安装npm私有模块
  • 2017年360最后一道编程题
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • (3)STL算法之搜索
  • (C++17) optional的使用
  • (C语言)逆序输出字符串
  • (Java)【深基9.例1】选举学生会
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (四)linux文件内容查看
  • (五)MySQL的备份及恢复
  • (原創) 未来三学期想要修的课 (日記)
  • (转)为C# Windows服务添加安装程序
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • .Family_物联网
  • .gitignore
  • .Net 4.0并行库实用性演练
  • .Net core 6.0 升8.0