Hibernate学习---第五节:普通组件和动态组件
一、普通组件映射配置
1、创建组件类,代码如下:
- package learn.hibernate.bean;
- /**
- * 组件类
- */
- public class Phones {
- private String companyPhone;
- private String homePhone;
- private String personalPhone;
- public Phones() {
- }
- public Phones(String companyPhone, String homePhone, String personalPhone) {
- super();
- this.companyPhone = companyPhone;
- this.homePhone = homePhone;
- this.personalPhone = personalPhone;
- }
- @Override
- public String toString() {
- return "Phones [companyPhone=" + companyPhone + ", homePhone="
- + homePhone + ", personalPhone=" + personalPhone + "]";
- }
- public String getCompanyPhone() {
- return companyPhone;
- }
- public void setCompanyPhone(String companyPhone) {
- this.companyPhone = companyPhone;
- }
- public String getHomePhone() {
- return homePhone;
- }
- public void setHomePhone(String homePhone) {
- this.homePhone = homePhone;
- }
- public String getPersonalPhone() {
- return personalPhone;
- }
- public void setPersonalPhone(String personalPhone) {
- this.personalPhone = personalPhone;
- }
- }
- package learn.hibernate.bean;
- /**
- * 组件类
- */
- public class Address {
- private String zipCode;
- private String address;
- public Address() {
- super();
- }
- public Address(String zipCode, String address) {
- super();
- this.zipCode = zipCode;
- this.address = address;
- }
- @Override
- public String toString() {
- return "Address [zipCode=" + zipCode + ", address=" + address + "]";
- }
- public String getZipCode() {
- return zipCode;
- }
- public void setZipCode(String zipCode) {
- this.zipCode = zipCode;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
2、将组件类作为属性放入到主类中,代码如下:
- package learn.hibernate.bean;
- import java.util.Date;
- /**
- * 持久化类
- */
- public class Person {
- private Integer id;
- private String name;
- private int age;
- private int passwork;
- private Date birthday;
- // 组件实例
- private Address addres;
- // 组件实例
- private Phones phone;
- public Person() {
- }
- public Person(String name, int age, int passwork, Date birthday) {
- super();
- this.name = name;
- this.age = age;
- this.passwork = passwork;
- this.birthday = birthday;
- }
- @Override
- public String toString() {
- return "Person [id=" + id + ", name=" + name + ", age=" + age
- + ", passwork=" + passwork + ", birthday=" + birthday + "]";
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getPasswork() {
- return passwork;
- }
- public void setPasswork(int passwork) {
- this.passwork = passwork;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Address getAddres() {
- return addres;
- }
- public void setAddres(Address addres) {
- this.addres = addres;
- }
- public Phones getPhone() {
- return phone;
- }
- public void setPhone(Phones phone) {
- this.phone = phone;
- }
- }
3、配置文件,代码如下:
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="learn.hibernate.bean">
- <class name="Person" table="t_person">
- <id name="id" column="person_id">
- <generator class="native"/>
- </id>
- <property name="name" column="t_name"/>
- <property name="age"/>
- <property name="passwork"/>
- <property name="birthday"/>
- <!--
- 组件类的映射配置
- component 指定需要映射的组件类
- name 指定 Person 中组件属性的变量名称
- name的值"addres" 与 Person 中定义的要一致
- -->
- <component name="addres">
- <property name="zipCode"/>
- <property name="address"/>
- </component>
- <component name="phone">
- <property name="companyPhone"/>
- <property name="homePhone"/>
- <property name="personalPhone"/>
- </component>
- </class>
- </hibernate-mapping>
4、测试代码:
- @Test
- public void testComponent() {
- Person p = new Person("sdf",23,123456,new Date());
- Address address = new Address("410000","湖南长沙");
- Phones phone = new Phones("07318678987","0731876567","15114565678");
- // person 与 address 关联
- p.setAddres(address);
- // person 与 phone 关联
- p.setPhone(phone);
- tx = session.beginTransaction();
- session.persist(p);
- tx.commit();
- }
二、动态组件映射配置
1、创建类,代码如下:
- package learn.hibernate.bean;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 持久化类设计
- * 注意:
- * 持久化类通常建议要有一个持久化标识符(ID)
- * 持久化标识符通常建议使用封装类(例如:Integer 因为基本类型存在默认值)
- * 持久化类通常建议手动添加一个无参构造函数 (因为有些操作是通过放射机制进行的)
- * 属性通常建议提供 getter/setter 方法
- * 持久化类不能使用 final 修饰
- * 持久化类中如果使用了集合类型数据,只能使用集合所对应的接口类型来声明(List/Map/Set)
- * 如下:ArrayList list = new ArrayList(); 不行
- * List list = new ArrayList(); 可行
- */
- public class Person {
- private Integer id;
- private String name;
- private int age;
- private int passwork;
- private Date birthday;
- // 动态组件实例
- private Map attribute = new HashMap();
- public Person() {
- }
- public Person(String name, int age, int passwork, Date birthday) {
- super();
- this.name = name;
- this.age = age;
- this.passwork = passwork;
- this.birthday = birthday;
- }
- @Override
- public String toString() {
- return "Person [id=" + id + ", name=" + name + ", age=" + age
- + ", passwork=" + passwork + ", birthday=" + birthday + "]";
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getPasswork() {
- return passwork;
- }
- public void setPasswork(int passwork) {
- this.passwork = passwork;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Map getAttribute() {
- return attribute;
- }
- public void setAttribute(Map attribute) {
- this.attribute = attribute;
- }
- }
2、映射配置文件,代码如下:
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="learn.hibernate.bean">
- <class name="Person" table="t_person">
- <id name="id" column="person_id">
- <generator class="native"/>
- </id>
- <property name="name" column="t_name"/>
- <property name="age"/>
- <property name="passwork"/>
- <property name="birthday"/>
- <!--
- 动态组件类的映射配置
- dynamic-component 指定需要映射的组件类
- name="attribute" 对应持久化类中集合的变量名称
- property Map 集合中key映射配置
- name 对 Map 集合的key
- column 存储 key 所对应的值
- type 字段的数据类型
- -->
- <dynamic-component name="attribute">
- <property name="key1" column="t_key1" type="string"/>
- <property name="key2" column="t_key1" type="integer"/>
- </dynamic-component>
- </class>
- </hibernate-mapping>
3、测试代码:
- @Test
- public void testComponent() {
- Person p = new Person("sdf",23,123456,new Date());
- // 在 Person 中只有声明,也有创建
- Map attribute = p.getAttribute();
- attribute.put("key1", "hibernate");
- attribute.put("key2", 123);
- tx = session.beginTransaction();
- session.persist(p);
- tx.commit();
- }
4、如果在 Person 类中只声明了动态组件,并未创建,如下:
- package learn.hibernate.bean;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 持久化类设计
- * 注意:
- * 持久化类通常建议要有一个持久化标识符(ID)
- * 持久化标识符通常建议使用封装类(例如:Integer 因为基本类型存在默认值)
- * 持久化类通常建议手动添加一个无参构造函数 (因为有些操作是通过放射机制进行的)
- * 属性通常建议提供 getter/setter 方法
- * 持久化类不能使用 final 修饰
- * 持久化类中如果使用了集合类型数据,只能使用集合所对应的接口类型来声明(List/Map/Set)
- * 如下:ArrayList list = new ArrayList(); 不行
- * List list = new ArrayList(); 可行
- */
- public class Person {
- private Integer id;
- private String name;
- private int age;
- private int passwork;
- private Date birthday;
- // 动态组件实例
- private Map attribute;
- public Person() {
- }
- public Person(String name, int age, int passwork, Date birthday) {
- super();
- this.name = name;
- this.age = age;
- this.passwork = passwork;
- this.birthday = birthday;
- }
- @Override
- public String toString() {
- return "Person [id=" + id + ", name=" + name + ", age=" + age
- + ", passwork=" + passwork + ", birthday=" + birthday + "]";
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getPasswork() {
- return passwork;
- }
- public void setPasswork(int passwork) {
- this.passwork = passwork;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Map getAttribute() {
- return attribute;
- }
- public void setAttribute(Map attribute) {
- this.attribute = attribute;
- }
- }
5、那么在测试代码需要创建一个,代码如下:
- @Test
- public void testAttribute() {
- Person p = new Person("sdf",23,123456,new Date());
- // 在 Person 中只有声明,没有创建
- Map attribute = new HashMap();
- attribute.put("key1", "hibernate");
- attribute.put("key2", 123);
- p.setAttribute(attribute);
- tx = session.beginTransaction();
- session.persist(p);
- tx.commit();
- }
6、查询,测试代码:
- @Test
- public void testGetAttribute() {
- Person p = (Person)session.get(Person.class, 1);
- System.out.println(p);
- // 高效操作 map 集合
- Iterator<Map.Entry> it = p.getAttribute().entrySet().iterator();
- for(;it.hasNext();){
- Map.Entry map = it.next();
- System.out.println(map.getKey()+"---------"+map.getValue());
- }
- }
Hibernate学习---第五节:普通组件和动态组件的更多相关文章
- VUE2.0实现购物车和地址选配功能学习第五节
第五节 单件商品金额计算和单选全选功能 1.vue精髓在于操作data模型来改变dom,渲染页面,而不是直接去改变dom 2.加减改变总金额功能: html:<div class="c ...
- Vue组件的操作-自定义组件,动态组件,递归组件
作者 | Jeskson 来源 | 达达前端小酒馆 v-model双向绑定 创建双向数据绑定,v-model指令用来在input,select,checkbox,radio等表单控件.v-model指 ...
- Vue两种组件类型介绍:递归组件和动态组件
一递归组件 递归组件的特性就是可以在自己的template模板中调用自己本身.值得注意的它必须设置name属性. // 递归组件 recursive.vue <template> < ...
- [Vue]组件——实现动态组件:keep-alive的使用
1.在app.vue中用一个 <keep-alive> 元素将其动态组件包裹起来: keepAlive为true时,第一次被创建的时候缓存下来,为false时,不会缓存 <keep- ...
- Vue 组件4 动态组件
动态组件 通过使用保留的<component>元素,动态的绑定到它的is特性,我们让多个组件同时使用同一个挂载点,并动态切换: var vm = new Vue({ el: '#examp ...
- Vue_(组件通讯)动态组件结合keep-alive
keep-alive 传送门 <keep-alive> 包裹动态组件时,会缓存不活动的组件实例,而不是销毁它们.和 <transition> 相似,<keep-alive ...
- Vue_(组件通讯)动态组件
动态组件 传送门 在一个元素上挂载多个组件,根据不同状态进行切换的时候,可以使用动态组件 动态组件的使用:需要使用内置组件<component></component>,根据 ...
- Vue.js的组件(slot/动态组件等)、单文件组件、递归组件使用
一.组件 1> 组件命名方式有两种(注意:在DOM模板中只有kebab-case命名方法才生效): html中引用组件: <!-- 在DOM模板中,只有 kebab-case命名才生效 - ...
- Hibernate征途(五)之继承映射和组件映射
之所以把这两种映射放到一起说,是因为二者都是以复用为目的,减少了代码和配置量,这是相同点:二者之间的不同点类似继承和实现的区别:继承的类是一个事物的抽象,而实现的接口仅仅是功能的抽象. 继承映射 如上 ...
随机推荐
- IEF could not decode Chinese character in IE history well
My friend is working on some case, and she looks not in the mood. I ask her what's going on. She wan ...
- Spring MVC防御CSRF、XSS和SQL注入攻击
参考: http://www.myhack58.com/Article/html/3/7/2012/36142_6.htm http://blog.csdn.net/jasontome/article ...
- SQL笔记-第一章,数据库入门
DBMS的分类DB2.Oracle.Microsoft SQL Server.Sybase SQLServer.Informix.MySQL数据库的结构元素库 database表 table列 col ...
- 字符串匹配KMP算法
1. 字符串匹配的KMP算法 2. KMP算法详解 3. 从头到尾彻底理解KMP
- CSS中属性position位置详解功能讲解与实例分析
position有五个值:static.relative.absolute.fixed.inherit. static 是默认值.就是按正常的布局流从上到下从左到右布局,平常我们做网页制作时,没有指定 ...
- CentOS6.4安装Smokeping节点监控软件
Smokeping:它是rrdtool的作者制作的,在图形显示方面很漂亮,可以用来很好的检测网络状态和稳定性,下面简单说一下Smokeping的安装以及配置方法. 0.首先关闭selinux和防火墙 ...
- HTML 5 Canvas
HTML 5 Canvas HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. <canvas id="myCanvas" width=&q ...
- C# 运行时编辑 节点重命名
方法一: ; bool nodeChanged = false; //右键点击,就进入修改状态 private void treeView1_NodeMouseClick(object sender, ...
- 关于SQL表联接
以SQL2008为例,Microsoft SQL Server 2008支持四种表运算符-JOIN,APPLY,PIVOT,UNPIVOT.JOIN表运算符是ANSI标准,而其他三种是T-SQL对标准 ...
- while循环中不支持循环使用curl
<?php $link = mysql_connect('localhost', 'sms', 'sms'); mysql_select_db('sms', $link); mysql_quer ...