hibernate自動建學生表
最近學習了hibernate,現在來整理一下做的一個小demo,根據配置文件在數據庫自動建立學生表。
準備工作:
IDE工具:mysql+eclipse
entity實體類+對應的配置文件:Student.java,Student.hbm.xml。實體類用於記錄實體Student的屬性和方法,實體類配置文件用於配置數據庫表與實體的對應關係。
utils工具包:HibernateSessionFactory.java,用於獲取session。
hibernate.cfg.xml總配置文件:配置屬性property,指定連接數據庫的driver,url,username,password,hbm2ddl.auto,show_sql。以及配置映射文件mapping。
execute包:executeService.java,執行main方法。
建立數據庫test:數據庫要手動建立,hibernate只會自動建表,不會自動建庫。
具體代碼如下:
Student.java:
package entity;
public class Student {
private String student_id;
private String sname;
private int age;
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [student_id=" + student_id + ", sname=" + sname + ", age=" + age + "]";
}
Student.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entity.Student" table="student">
<id name="student_id" column="student_id">
<!-- 自動匹配MySQL或Oracle -->
<!-- <generator class="native" /> -->
<!-- 程序指定 -->
<generator class="assigned" />
</id>
<property name="sname" column="sname" type="java.lang.String" ></property>
<property name="age" column="age" type="int" ></property>
</class>
</hibernate-mapping>
HibernateSessionFactory.javapackage utils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
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="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Hibernate格式化SQL語句 -->
<property name="hibernate.format_sql">true</property>
<!-- Hibernate的方言:作用,根據配置的方言生成相應的SQL語句-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 是否啓動表結構自動生成 -->
<property name="hbm2ddl.auto">update</property>
<!-- 是否在控制檯顯示SQL語句 -->
<property name="show_sql">true</property>
<!-- 2018-07-06 -->
<mapping resource="entity/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
executeService.java:
package execute;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class executeService {
public static void main(String[] args) {
Configuration conf = new Configuration().configure();// 讀取並解析配置文件
SchemaExport export = new SchemaExport(conf);// 創建SchemaExport
// 執行生成表。參數1:是否顯示sql語句,參數2:是否到數據庫裏執行
export.create(true, true);
}
}
執行main方法,會發現數據庫自動建立了一個student表,至此成功入門hibernate,完成其配置文件的編寫。後面會將運用hibernate操作數據庫,實現增刪改查操作。