Hibernate与hbm2ddl.auto工具

1.hibernate.cfg.xml

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">pass</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="myeclipse.connection.profile">mysql</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping resource="com/jdbc/Student.hbm.xml"/>
        <mapping class="com.jdbc.Person"/>
    </session-factory>
 
</hibernate-configuration>

 

2. Student.java

package com.jdbc;

public class Student
{
 private int id;
 private String name;
 private int age;
 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;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
}

 

 

3.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">

<hibernate-mapping>
 <class name="com.jdbc.Student" table="_student">
  <id name="id" />
  <property name="name" />
  <property name="age" />
    </class>
 
</hibernate-mapping>

 

4. 测试程序

package com.jdbc;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.sessionFacory.HibernateSessionFactory;

public class TestMain
{
 public static void main(String[] args)
 {
  SessionFactory sessionFactory=HibernateSessionFactory.getSessionFactory();
  Student stu=new Student();
  stu.setId(1);
  stu.setAge(25);
  stu.setName("YuQiang");
  
  Session session=sessionFactory.openSession();
  Transaction tx=session.beginTransaction();
  session.save(stu);
  tx.commit();
  System.out.println("新增人员成功!");
  session.close();
  sessionFactory.close();
 }

}

 

运行该文件时候会自动产生一张Student表

方式二

1.

package com.jdbc;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "person", catalog = "hibernate")
public class Person implements java.io.Serializable {

 private static final long serialVersionUID = -7602554341075044106L;
 private Integer id;
 private String name;
 private Integer age;

 public Person() {
 }

 public Person(String name) {
  this.name = name;
 }

 public Person(String name, Integer age) {
  this.name = name;
  this.age = age;
 }

 @Id
 @GeneratedValue
 @Column(name = "id", unique = true, nullable = false)
 public Integer getId() {
  return this.id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 @Column(name = "name", nullable = false, length = 100)
 public String getName() {
  return this.name;
 }

 public void setName(String name) {
  this.name = name;
 }

 @Column(name = "age")
 public Integer getAge() {
  return this.age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

}

 

 

2.

package com.jdbc;

import java.sql.SQLException;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.sessionFacory.HibernateSessionFactory;

public class TeacherTest
{
 public static void main(String[] args) throws ClassNotFoundException, SQLException, SecurityException, NoSuchMethodException
 {
  Person per=new Person();
  per.setId(1);
  per.setAge(25);
  per.setName("laopo");
  
  SessionFactory sessionFactory=HibernateSessionFactory.getSessionFactory();
  Session session=sessionFactory.openSession();
  Transaction tx=session.beginTransaction();
  session.save(per);
  tx.commit();
  System.out.println("新增人员成功!");
  session.close();
  sessionFactory.close();
  
 }

}

运行就会在数据库中产生一张表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值