Hibernate入门(五)
一 Hibernate继承映射
我们可以将继承层次结构类与数据库的表映射。
1. 每个层次类一张表
这个层次结构有三个类,Employee
是Regular_Employee
和Contract_Employee
类的超类(父类)。
此层次结构的表结构如下所示:
XML实现
1)创建持久化类
Employee.java
package cn; public class Employee { private int id; private String name; 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;
} }
Regular_Employee.java
package cn;
public class Regular_Employee extends Employee{
private float salary;
private int bonus;
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
Contract_Employee.java
package cn;
public class Contract_Employee extends Employee{
private float pay_per_hour;
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}
}
2)创建映射文件
在每个类层次结构一个表的情况下,hibernate框架添加了一个标识符列,该框架指定了记录的类型。 它主要用于区分记录。要指定它,必须指定类的 discriminator
子元素。
类的子类subelement
指定子类。 在这种情况下,Regular_Employee
和Contract_Employee
是Employee
类的子类。
Employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="cn.Employee" table="employee">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator column="type" type="string"></discriminator>
<property name="name"></property> <subclass name = "cn.Regular_Employee" discriminator-value="reg_emp">
<property name="salary"></property>
<property name="bonus"></property>
</subclass> <subclass name = "cn.Contract_Employee" discriminator-value="con_emp">
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</subclass> </class> </hibernate-mapping>
3)在配置文件中增加Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- List of XML mapping files -->
<mapping resource="cn/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4)测试
package cn; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.User; public class Test { public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction(); Employee emp = new Employee();
emp.setName("emp"); Regular_Employee reg = new Regular_Employee();
reg.setName("reg_emp");
reg.setSalary(10000);
reg.setBonus(1); Contract_Employee con = new Contract_Employee();
con.setName("con_emp");
con.setPay_per_hour(200);
con.setContract_duration("20h"); session.persist(emp);
session.persist(reg);
session.persist(con); session.getTransaction().commit();
session.close();
}
}
注解实现
1)创建持久化类
Employee.java
package cn; import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table; @Entity
@Table(name="employee")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="employeee")
public class Employee { @Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id; @Column(name="name")
private String name; 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;
} }
Regular_Employee.java
package cn; import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity; @Entity
@DiscriminatorValue(value="reg")
public class Regular_Employee extends Employee{ @Column(name="salary")
private float salary; @Column(name="bonus")
private int bonus; public float getSalary() {
return salary;
} public void setSalary(float salary) {
this.salary = salary;
} public int getBonus() {
return bonus;
} public void setBonus(int bonus) {
this.bonus = bonus;
} }
Contract_Employee.java
package cn; import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity; @Entity
@DiscriminatorValue(value="con")
public class Contract_Employee extends Employee{ @Column(name="pay_per_hour")
private float pay_per_hour;
@Column(name="contract_duration")
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
} }
2)配置文件中增加持久化类
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<property name="show_sql">true</property>
<mapping class="cn.Employee"/>
<mapping class="cn.Regular_Employee"/>
<mapping class="cn.Contract_Employee"/>
</session-factory>
</hibernate-configuration>
3)测试
package cn; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration; public class Test { public static void main(String[] args) { SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction(); t.begin(); Employee emp = new Employee();
emp.setName("emp"); Regular_Employee reg = new Regular_Employee();
reg.setName("reg_emp");
reg.setSalary(10000);
reg.setBonus(1); Contract_Employee con = new Contract_Employee();
con.setName("con_emp");
con.setPay_per_hour(200);
con.setContract_duration("20h"); session.persist(emp);
session.persist(reg);
session.persist(con); t.commit(); session.close(); } }
2. 每个具体类一张表
数据库中将有三个表,彼此之间没有关系。
XML实现
1) 创建持久化类
Employee.java
package cnn; public class Employee { private int id; private String name; 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;
} }
Regular_Employee.java
package cnn; public class Regular_Employee extends Employee{ private float salary; private int bonus; public float getSalary() {
return salary;
} public void setSalary(float salary) {
this.salary = salary;
} public int getBonus() {
return bonus;
} public void setBonus(int bonus) {
this.bonus = bonus;
} }
Contract_Employee.java
package cnn; public class Contract_Employee extends Employee {
private float pay_per_hour;
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
} }
2)employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="cnn.Employee" table="employee">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property> <union-subclass name = "cnn.Regular_Employee" table="regular" >
<property name="salary"></property>
<property name="bonus"></property>
</union-subclass> <union-subclass name = "cnn.Contract_Employee" table="contract">
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</union-subclass> </class> </hibernate-mapping>
3) 在配置文件中增加hbm文件的映射
<!-- List of XML mapping files -->
<mapping resource="cnn/Employee.hbm.xml"/>
4)测试
package cnn; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.User; public class Test { public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction(); Employee emp = new Employee();
emp.setName("emp"); Regular_Employee reg = new Regular_Employee();
reg.setName("reg_emp");
reg.setSalary(10000);
reg.setBonus(1); Contract_Employee con = new Contract_Employee();
con.setName("con_emp");
con.setPay_per_hour(200);
con.setContract_duration("20h"); session.persist(emp);
session.persist(reg);
session.persist(con); session.getTransaction().commit();
session.close();
}
}
注解实现
1)创建持久化类
Employee.java
package cnn; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table; @Entity
@Table(name="employee")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Employee { @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id; @Column(name = "name")
private String name; 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;
} }
Regular_Employee.java
package cnn; import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table; @Entity
@Table(name = "regular")
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "id")),
@AttributeOverride(name = "name", column = @Column(name = "name")) })
public class Regular_Employee extends Employee{ @Column(name = "salary")
private float salary; @Column(name = "bonus")
private int bonus; public float getSalary() {
return salary;
} public void setSalary(float salary) {
this.salary = salary;
} public int getBonus() {
return bonus;
} public void setBonus(int bonus) {
this.bonus = bonus;
} }
Contract_Employee.java
package cnn; import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table; @Entity
@Table(name = "contract")
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "id")),
@AttributeOverride(name = "name", column = @Column(name = "name")) })
public class Contract_Employee extends Employee {
@Column(name = "pay_per_hour")
private float pay_per_hour;
@Column(name = "contract_duration")
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
} }
2)在配置文件中添加hbm文件映射
<mapping class="cnn.Employee"/>
<mapping class="cnn.Regular_Employee"/>
<mapping class="cnn.Contract_Employee"/>
3)测试类
package cnn; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration; public class Test { public static void main(String[] args) { SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction(); t.begin(); Employee emp = new Employee();
emp.setName("emp"); Regular_Employee reg = new Regular_Employee();
reg.setName("reg_emp");
reg.setSalary(20000);
reg.setBonus(12); Contract_Employee con = new Contract_Employee();
con.setName("con_emp");
con.setPay_per_hour(100);
con.setContract_duration("30h"); session.persist(emp);
session.persist(reg);
session.persist(con); t.commit(); session.close(); } }
3. 每个子类一张表
在每个子类一张表的情况下,子类映射表与主键和外键的关系与父类映射表相关。
XML实现
1)创建持久化类
Employee.java
package cnnn; public class Employee { private int id; private String name; 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;
} }
Regular_Employee.java
package cnnn; public class Regular_Employee extends Employee{ private float salary; private int bonus; public float getSalary() {
return salary;
} public void setSalary(float salary) {
this.salary = salary;
} public int getBonus() {
return bonus;
} public void setBonus(int bonus) {
this.bonus = bonus;
} }
Contract_Employee.java
package cnnn; public class Contract_Employee extends Employee{ private float pay_per_hour;
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
} }
2)Employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="cnnn.Employee" table="employee">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="name"></property> <joined-subclass name = "cnnn.Regular_Employee" >
<key column="reg_id"></key>
<property name="salary"></property>
<property name="bonus"></property>
</joined-subclass> <joined-subclass name = "cnnn.Contract_Employee" >
<key column="con_id"></key>
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</joined-subclass> </class> </hibernate-mapping>
3)在配置文件中添加hbm文件的映射
<mapping resource="cnnn/Employee.hbm.xml"/>
4)测试
package cnnn; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; import com.User; public class Test { public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction(); Employee emp = new Employee();
emp.setName("emp"); Regular_Employee reg = new Regular_Employee();
reg.setName("reg_emp");
reg.setSalary(10000);
reg.setBonus(1); Contract_Employee con = new Contract_Employee();
con.setName("con_emp");
con.setPay_per_hour(200);
con.setContract_duration("20h"); session.persist(emp);
session.persist(reg);
session.persist(con); session.getTransaction().commit();
session.close();
}
}
注解实现
1)创建持久化类
Employee.java
package cnnn; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table; @Entity
@Table(name="employee")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee { @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id; @Column(name = "name")
private String name; 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;
} }
Regular_Employee.java
package cnnn; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table; @Entity
@Table(name = "regular")
@PrimaryKeyJoinColumn(name = "ID")
public class Regular_Employee extends Employee{ @Column(name = "salary")
private float salary; @Column(name = "bonus")
private int bonus; public float getSalary() {
return salary;
} public void setSalary(float salary) {
this.salary = salary;
} public int getBonus() {
return bonus;
} public void setBonus(int bonus) {
this.bonus = bonus;
} }
package cnnn; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table; @Entity
@Table(name = "contract")
@PrimaryKeyJoinColumn(name = "ID")
public class Contract_Employee extends Employee {
@Column(name = "pay_per_hour")
private float pay_per_hour;
@Column(name = "contract_duration")
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
} }
2)在配置文件中增加配置
<mapping class="cnnn.Employee"/>
<mapping class="cnnn.Regular_Employee"/>
<mapping class="cnnn.Contract_Employee"/>
3)测试
package cnnn; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration; public class Test { public static void main(String[] args) { SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction t = session.beginTransaction(); t.begin(); Employee emp = new Employee();
emp.setName("emp"); Regular_Employee reg = new Regular_Employee();
reg.setName("reg_emp");
reg.setSalary(20000);
reg.setBonus(12); Contract_Employee con = new Contract_Employee();
con.setName("con_emp");
con.setPay_per_hour(100);
con.setContract_duration("30h"); session.persist(emp);
session.persist(reg);
session.persist(con); t.commit(); session.close(); } }
Hibernate入门(五)的更多相关文章
- Hibernate入门(五)---------事务管理
在Hibernate中,可以通过代码来操作管理事务,如通过“Transaction tx = session.beginTransaction()”,开启一个事务,持久化操作后,通过"tx. ...
- openresty 前端开发入门五之Mysql篇
openresty 前端开发入门五之Mysql篇 这章主要演示怎么通过lua连接mysql,并根据用户输入的name从mysql获取数据,并返回给用户 操作mysql主要用到了lua-resty-my ...
- 三大框架之hibernate入门
hibernate入门 1.orm hibernate是一个经典的开源的orm[数据访问中间件]框架 ORM( Object Relation Mapping)对象关 ...
- Hibernate入门案例及增删改查
一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...
- Hibernate入门案例 增删改
一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...
- Hibernate入门6.Hibernate检索方式
Hibernate入门6.Hibernate检索方式 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv Hibernate的整体框架已经 ...
- Hibernate入门5持久化对象关系和批量处理技术
Hibernate入门5持久化对象关系和批量处理技术 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv 前言: 前面学习了Hiberna ...
- Hibernate入门4.核心技能
Hibernate入门4.核心技能 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv 前言: 前面学习了Hibernate3的基本知识, ...
- Hibernate入门3.配置映射文件深入
Hibernate入门3.配置映射文件深入 2013.11.27 前言: 之前的两节是在Java项目中如何使用hibernate,并且通过一个简单地项目实践,期间有很多的错误,一般都是因为配置包的问题 ...
- 简单的Hibernate入门简介
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...
随机推荐
- mui开发app之webview是什么
WebView(网络视图)能加载显示网页,可以将其视为一个浏览器,webview被封装在html5+,plus对象中,底层由java,OC实现. 先来谈谈我对webview的理解: 使用mui开发的a ...
- bootstrap+masonry.js写瀑布流
最近在用bootstrap写一个网站,其中有个图文展示的页面要用到瀑布流的效果.因为项目要求,项目要以bootstrap为基准,不准私自添加内联样式.内部样式,所以,自己写瀑布流就不行了,所以,根据要 ...
- OD常用断点之CC断点
在做Windows平台软件逆向时,Ollydbg是极其常用的逆向工具,动态调试功能非常强大.在调试过程中,断点的使用有助于确定关键的破解位置,今天讲解的内容是OD常用断点中的CC断点. CC断点有很多 ...
- Windows上Ruby开发环境的配置
最近公司项目上有需要,需要开发一个puppet的自动化工具,这个工具需要操作存储设备上的各种资源,而鉴于puppet不是善于完成这个任务的首选语言,于是我们选择了puppet的“爹”,Ruby. 熟悉 ...
- eclipse配置相关
1,配置gradle,操作都正确,就是报gradle dependencies Uninitialized或者gradle dependencies怎么都不出现,此时的解决方案是在外部cmd下执行gr ...
- Docker - 定制镜像
Dockerfile Docker Hub拥有大量高质的官方镜像:可直接使用的服务类镜像.语言应用镜像.基础操作系统镜像等,满足绝大部分需求. 此外,可以通过定制镜像的方式来满足实际使用中的特定需求. ...
- js快速去除数组重复项
function unique1(arr) { var tmp = new Array(); tmp.push(arr[0]); for(var i=0;i<arr.length;i++) { ...
- iOS 伐码猿真爱—「偷懒 || 效率 工具类」
自检 代码不会可以多敲几次,学习的重点是思想:-- 认同. BUT 如果你把自己会的.熟知的.可以说写的似流水的代码,不管是在工作 或是 自学习中你还是一点一点的敲出来,是不是有点...,copy & ...
- poj1743 Musical Theme 后缀数组的应用(求最长不重叠重复子串)
题目链接:http://poj.org/problem?id=1743 题目理解起来比较有困难,其实就是求最长有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1 ...
- ng-class改变css样式
而在这所谓的样子当然就是改变其css的属性,而实现能动态的改变其属性值,必然只能是更换其class属性 这里有三种方法: 第一种:通过数据的双向绑定(不推荐) 第二种:通过对象数组 第三种:通过key ...