14.1新建一个User1实体类;
package com.eduask.entity3;
import java.util.List;
import java.util.Map;
public class User1 {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User1 [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
}
public User1(int id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
public User1() {
super();
// TODO Auto-generated constructor stub
}
}
14.2新建一个Demo1的实体类,引入List集合中的User1类;
package com.eduask.entity3;
import java.util.List;
//新建一个Demo1的实体类;
public class Demo1 {
private int id;
private String name;
private String pwd;
private List users1;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public List getUsers1() {
return users1;
}
public void setUsers1(List users1) {
this.users1 = users1;
}
@Override
public String toString() {
return "Demo1 [id=" + id + ", name=" + name + ", pwd=" + pwd + "]\nusers1="
+ users1 + "]";
}
public Demo1(int id, String name, String pwd, List users1) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
this.users1 = users1;
}
public Demo1() {
super();
// TODO Auto-generated constructor stub
}
}
14.3 新建一个TestDemo1的实体类;
package com.eduask.entity3;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo1 {
public static void main(String[] args) {
ClassPathXmlApplicationContext cx=new ClassPathXmlApplicationContext("entity3Xml/demo1.xml");
Demo1 demo1=(Demo1) cx.getBean("demo1");
System.out.println(demo1);
}
}
14.4 新建一个demo1.xml的配置文件;
14.5程序运行如下:
Demo1 [id=1, name=tom, pwd=123456]
users1=[User1 [id=1, name=name1, pwd=pwd1], User1 [id=2, name=name2, pwd=pwd2], User1 [id=2, name=name3, pwd=pwd3]]]