Hibernate的@Table注解

1.新建一个java project项目,里面新建一个lib文件夹,lib文件夹里面放置要用的一些jar文件,然后全部选中导入到项目中去。整体的框架如下图所示:

Hibernate的@Table注解


2.Students.java里面的代码如下图所示:

[java] view plain copy
 print?
  1. package entity;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.persistence.Entity;  
  6. import javax.persistence.Id;  
  7.   
  8. @Entity
    @Table(name = "t_students", schema = "hibernate")
  9. public class Students {  
  10.   
  11.     private int sid;  
  12.     private String sname;  
  13.     private String gender;  
  14.     private Date birthday;  
  15.     private String major;  
  16.     private String address;  
  17.   
  18.     public Students() {  
  19.   
  20.     }  
  21.   
  22.     public Students(int sid, String sname, String gender, Date birthday,  
  23.             String major, String address) {  
  24.         this.sid = sid;  
  25.         this.sname = sname;  
  26.         this.gender = gender;  
  27.         this.birthday = birthday;  
  28.         this.major = major;  
  29.         this.address = address;  
  30.     }  
  31.   
  32.     @Id  
  33.     public int getSid() {  
  34.         return sid;  
  35.     }  
  36.   
  37.     public void setSid(int sid) {  
  38.         this.sid = sid;  
  39.     }  
  40.   
  41.     public String getSname() {  
  42.         return sname;  
  43.     }  
  44.   
  45.     public void setSname(String sname) {  
  46.         this.sname = sname;  
  47.     }  
  48.   
  49.     public String getGender() {  
  50.         return gender;  
  51.     }  
  52.   
  53.     public void setGender(String gender) {  
  54.         this.gender = gender;  
  55.     }  
  56.   
  57.     public Date getBirthday() {  
  58.         return birthday;  
  59.     }  
  60.   
  61.     public void setBirthday(Date birthday) {  
  62.         this.birthday = birthday;  
  63.     }  
  64.   
  65.     public String getMajor() {  
  66.         return major;  
  67.     }  
  68.   
  69.     public void setMajor(String major) {  
  70.         this.major = major;  
  71.     }  
  72.   
  73.     public String getAddress() {  
  74.         return address;  
  75.     }  
  76.   
  77.     public void setAddress(String address) {  
  78.         this.address = address;  
  79.     }  
  80.   
  81. }  

3.hibernate.cfg.xml里面的代码如下图所示:

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  8.         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8</property>  
  9.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  10.         <property name="connection.username">root</property>  
  11.         <property name="connection.password">root</property>  
  12.         <property name="show_sql">true</property>  
  13.         <property name="format_sql">true</property>  
  14.         <property name="hbm2ddl.auto">create</property>  
  15.         <property name="hibernate.current_session_context_clss">thread</property>  
  16.   
  17.         <mapping class="entity.Students" />  
  18.     </session-factory>  
  19. </hibernate-configuration>  

4.TestStudents.java里面的代码如下图所示:

[java] view plain copy
 print?
  1. package entity;  
  2.   
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.cfg.Configuration;  
  5. import org.hibernate.service.ServiceRegistry;  
  6. import org.hibernate.service.ServiceRegistryBuilder;  
  7. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  8. import org.junit.Test;  
  9.   
  10. public class TestStudents {  
  11.   
  12.     @Test  
  13.     public void testShemaExport() {  
  14.         Configuration config = new Configuration().configure();  
  15.         ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()  
  16.                 .applySettings(config.getProperties()).buildServiceRegistry();  
  17.         SessionFactory sessionFactory = config  
  18.                 .buildSessionFactory(serviceRegistry);  
  19.         SchemaExport export = new SchemaExport(config);  
  20.         export.create(truetrue);  
  21.     }  
  22.   
  23. }  

5.在Navicat数据库里面新建一个数据库,数据库的名称要与上面 的数据库的名称相同。

Hibernate的@Table注解


6.运行testShemaExport类,数据库里面会自动创建一张表。

Hibernate的@Table注解