码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate学习五----------组件属性

时间:2017-05-25 19:45:37      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:str   t_sql   oct   format   name   style   apach   1.0   pom   

? 版权声明:本文为博主原创文章,转载请注明出处

实例

1.项目结构

技术分享

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  
  	<modelVersion>4.0.0</modelVersion>
  	
	<groupId>org.hibernate</groupId>
	<artifactId>Hibernate-Component</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Hibernate-Component Maven Webapp</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<hibernate.version>5.1.6.Final</hibernate.version>
	</properties>
	
	<dependencies>
		<!-- junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- hibernate -->
		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-core</artifactId>
		    <version>${hibernate.version}</version>
		</dependency>
		<!-- MySQL -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>5.1.42</version>
		</dependency>
	</dependencies>
  <build>
    <finalName>Hibernate-Component</finalName>
  </build>
</project>

3.Address.java

package org.hibernate.model;

public class Address {

	private String postCode;// 邮编
	private String phone;// 电话号码
	private String address;// 地址

	public Address() {
	}

	public Address(String postCode, String phone, String address) {
		this.postCode = postCode;
		this.phone = phone;
		this.address = 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;
	}

}

4.Student.java

package org.hibernate.model;

import java.util.Date;

public class Student {

	private long sid;// 学号
	private String sname;// 姓名
	private String gender;// 性别
	private Date birthday;// 生日
	private Address address;// 地址

	public Student() {
	}

	public Student(String sname, String gender, Date birthday, Address address) {
		this.sname = sname;
		this.gender = gender;
		this.birthday = birthday;
		this.address = address;
	}

	public long getSid() {
		return sid;
	}

	public void setSid(long sid) {
		this.sid = sid;
	}

	public String getSname() {
		return sname;
	}

	public void setSname(String sname) {
		this.sname = sname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	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;
	}

}

5.Student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

	<class name="org.hibernate.model.Student" table="STUDENT">
		<id name="sid" type="java.lang.Long">
			<column name="SID"/>
			<generator class="native"/>
		</id>
		<property name="sname" type="java.lang.String">
			<column name="SNAME"/>
		</property>
		<property name="gender" type="java.lang.String">
			<column name="GENDER"/>
		</property>
		<property name="birthday" type="date">
			<column name="BIRTHDAY"/>
		</property>
		<component name="address" class="org.hibernate.model.Address">
			<property name="postCode" column="POSTCODE"/>
			<property name="phone" column="PHONE"/>
			<property name="address" column="ADDRESS"/>
		</component>
	</class>

</hibernate-mapping>

6.hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

	<!-- 配置SessionFactory信息 -->
	<session-factory>
		<!-- 配置数据库连接信息 -->
		<property name="connection.username">root</property>
		<property name="connection.password">***</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">
			jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8
		</property>
		
		<!-- 常用配置 -->
		<property name="hbm2ddl.auto">create</property>
		<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		
		<!-- 引入映射文件 -->
		<mapping resource="hbm/Student.hbm.xml"/>
	</session-factory>

</hibernate-configuration>

7.ComponentTest.java

package org.hibernate.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.model.Address;
import org.hibernate.model.Student;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ComponentTest {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void before() {
		
		Configuration config = new Configuration().configure();// 获取配置对象
		sessionFactory = config.buildSessionFactory();// 创建SessionFactory对象
		session = sessionFactory.openSession();// 创建Session对象
		transaction = session.beginTransaction();// 开启事务
		
	}
	
	@After
	public void after() {
		
		transaction.commit();// 提交事务
		session.close();// 关闭Session
		sessionFactory.close();// 关闭SessionFactory
		
	}
	
	@Test
	public void testComponentSave() {
		
		Address address = new Address("100000", "18712345678", "北京市");// 创建Address对象
		Student student = new Student("张三", "男", new Date(), address);// 创建Student对象
		session.save(student);// 保存对象
		
		
	}
	
}

8.效果预览

技术分享

参考:http://www.imooc.com/video/7741

Hibernate学习五----------组件属性

标签:str   t_sql   oct   format   name   style   apach   1.0   pom   

原文地址:http://www.cnblogs.com/jinjiyese153/p/6905439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!