标签:打印 use pre ide builder comm bbb actor XML
目录结构
类
package com.hibernate.helloworld; public class School { private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public School(String name, String address) { super(); this.name = name; this.address = address; } public School() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "School [name=" + name + ", address=" + address + "]"; } } package com.hibernate.helloworld; import java.util.Date; public class Student { private Integer id; private String name; private Date birth; private School school; public Student() { // TODO Auto-generated constructor stub } public Student(String name, Date birth) { super(); this.name = name; this.birth = birth; } 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 Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public School getSchool() { return school; } public void setSchool(School school) { this.school = school; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", birth=" + birth + ", school=" + school + "]"; } }
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.username">root</property> <property name="connection.password">1</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8</property> <!-- 配置 hibernate 的基本信息 --> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 执行操作时是否在控制台打印 SQL --> <property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 指定关联的 .hbm.xml 文件 --> <mapping resource="com/hibernate/helloworld/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-8-7 21:15:19 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="com.hibernate.helloworld"> <class name="Student" table="STUDENT" dynamic-insert="true"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="birth" type="java.util.Date"> <column name="BIRTH" /> </property> <component name="school" class="School"> <property name="name" column="SCHOOL_NAME"></property> <property name="address" column="SCHOOL_ADDRESS"></property> </component> </class> </hibernate-mapping>
Test
package com.hibernate.helloworld; import java.sql.Date; 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.Test; public class test { @Test public void test() { System.out.println("test..."); //1. 创建一个 SessionFactory 对象 SessionFactory sessionFactory = null; //1). 创建 Configuration 对象: 对应 hibernate 的基本配置信息和 对象关系映射信息 Configuration configuration = new Configuration().configure(); //4.0 之前这样创建 // sessionFactory = configuration.buildSessionFactory(); //2). 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象 //hibernate 的任何配置和服务都需要在该对象中注册后才能有效. ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); //3). sessionFactory = configuration.buildSessionFactory(serviceRegistry); //2. 创建一个 Session 对象 Session session = sessionFactory.openSession(); //3. 开启事务 Transaction transaction = session.beginTransaction(); //4. 执行保存操作 Student student = new Student("刘备", new Date(new java.util.Date().getTime())); School school = new School("AAA","BBB"); student.setSchool(school); session.save(student); //5. 提交事务 transaction.commit(); //6. 关闭 Session session.close(); //7. 关闭 SessionFactory 对象 sessionFactory.close(); } }
标签:打印 use pre ide builder comm bbb actor XML
原文地址:http://www.cnblogs.com/lusufei/p/7323932.html