hibernate课程 初探单表映射3-4 组件属性
本节内容:
1 简介组件属性
2 demo
1 简介组件属性:
<component name = "address" class = "Address">
<property name = "postcard" column = "POSTCARD"></property>
<property name = "address" column = "ADDRESS"></property>
</component>
hibernate 组件component标签 其实就是Student实体类中有Address对象。
2 demo:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bendi</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource = "Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-12-20 0:42:12 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.ddwei.student.Student" table="STUDENT">
<id name="pid" type="int">
<column name="PID" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" />
</property>
<property name="birthday" type="date">
<!-- <property name="birthday" type="time"> -->
<!-- <property name="birthday" type="timestamp"> -->
<column name="BIRTHDAY" />
</property>
<!-- <property name="address" type="java.lang.String"> -->
<!-- <column name="ADDRESS" /> -->
<!-- </property> -->
<property name="picture" type="java.sql.Blob">
<column name="Picture" />
</property>
<component name="address" class="com.ddwei.student.Address">
<property name="postcode" column="POSTCODE"/>
<property name="phone" column="PHONE"/>
<property name="address" column="ADDRESS"/>
</component>
</class>
</hibernate-mapping>
StudentTest.java
package hibernate_001;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.SQLException; import java.util.Date;
import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test;
import com.ddwei.student.Address; import com.ddwei.student.Student;
public class StudentTest { private SessionFactory sessionFactory; private Session session; private Transaction trasaction; @Test public void testSaveStudent(){ // Student student =new Student(1,"周恩来","男",new Date(),"绍兴");//创建学生对象 Student student = new Student(); student.setName("秦始皇"); student.setSex("男"); student.setBirthday(new Date()); // student.setAddress("阿房宫"); Address address = new Address("105789","18912345678","河北敦煌教区"); student.setAddress(address); session.save(student);//会话保存学生对象进入数据库 } @Before public void init(){ //1 创建配置对象 Configuration config = new Configuration().configure(); //2 创建服务对象 ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //3 创建会话工厂 sessionFactory = config.buildSessionFactory(serviceRe); //4 打开会话 session = sessionFactory.openSession(); //5 创建事务 trasaction = session.beginTransaction(); } @After public void destroy(){ trasaction.commit(); session.close(); sessionFactory.close(); }
}
Student.java
package com.ddwei.student;
import java.sql.Blob; import java.util.Date;
public class Student {
// java beans 的设计原则 /** * 1 公有的类 2 共有不带参数构造方法 3 私有属性 4 属性setter/getter方法 */
private int pid;// 学号 private String name;// 姓名 private String sex;// 性别 private Date birthday;// 出生日期 private Address address;// 家庭地址 private Blob picture;//相片
public Student() {
}
public Student(int pid, String name, String sex, Date birthday) { // super(); this.pid = pid; this.name = name; this.sex = sex; this.birthday = birthday; // this.address = address; }
@Override public String toString() { return "Student [pid=" + pid + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; }
public int getPid() { return pid; }
public void setPid(int pid) { this.pid = pid; }
public Date getBirthday() { return birthday; }
public void setBirthday(Date birthday) { this.birthday = birthday; }
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public Blob getPicture() { return picture; }
public void setPicture(Blob picture) { this.picture = picture; }
}
Address.java
package com.ddwei.student;
//地址类 public class Address { private String postcode;//邮编 private String phone;//电话 private String address;//地址 public String getPostcode() { return postcode; } public void setPostcode(String postcode) { this.postcode = postcode; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } //无参构造方法 public Address(){ } //有参构造方法 public Address(String postcode, String phone, String address) { super(); this.postcode = postcode; this.phone = phone; this.address = address; } }
最新文章
- getComputedStyle/currentStyle/style之间的爱恨情仇
- Quartz 入门详解
- JavaScript查找数组是否存在指定元素
- cache buffers
- linux 中文件夹的文件按照时间倒序或者升序排列
- sum()over()和count()over()分析函数
- LightOj1418 - Trees on My Island(Pick定理)
- C# Socket编程(1)基本的术语和概念
- HBase(五): HBase运维管理
- C#基础|初探反射
- [C# 基础知识系列]专题四:事件揭秘
- 修改textField的placeholder的字体颜色、大小
- BootStrap 模态框禁用空白处点击关闭,手动显示隐藏,垂直居中
- 一步一步实现FormsAuthentic验证登录
- UWP 常用文件夹
- python高级编程和算法
- Java中的String、StringBuilder以及StringBuffer
- angular打包后路由和文件路径不对
- 百度API获取经纬度使用
- LOJ#2302 整数
热门文章
- tar压缩解压缩
- (转)64位系统安装Delphi7提示Can’t load package:dclite70.bpl 以及 提示地址错误
- hibernate&;nbsp;hql&;nbsp;查询指定…
- windows下primer3-py安装
- Session和cookie有什么区别?
- 利用Hadoop自带example实现wordCount
- js常用util
- EasyOffice-.NetCore一行代码导入导出Excel,生成Word
- uva 10817(数位dp)
- 我的省选 Day -14