今天来演示下使用SchemaExport生成数据库表
先来看下项目的整体架构:
Score类
public class Score {
private int id;
private int stuId;//学生id
private int subjectId;//课程Id
private double result;//分数
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public int getSubjectId() {
return subjectId;
}
public void setSubjectId(int subjectId) {
this.subjectId = subjectId;
}
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
}
Score.hbm.xml配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.pojo">
<class name="Score">
<id name="id">
<generator class="native"></generator>
</id>
<property name="stuId" />
<property name="subjectId" />
<property name="result" />
</class>
</hibernate-mapping>
HibernateTest测试类,使用Junit进行测试
@Test
public void testCreateDB(){
Configuration cfg=new Configuration().configure();
SchemaExport se=new SchemaExport(cfg);
//第一个参数表示是否生成ddl脚本,第二个参数表示是否执行到数据库中
se.create(true, true);
}
/**
* 保存数据
*/
@Test
public void save(){
Session session=null;
Transaction tx=null;
try{
session=HibernateUtil.getSession();
tx=session.beginTransaction();
Score s=new Score();
s.setStuId(1);
s.setSubjectId(2);
s.setResult(89);
session.save(s);
tx.commit();
}catch(Exception e){
if(tx!=null)
tx.rollback();
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
}
}
HibernateUtil类
public class HibernateUtil {
private static Configuration cfg=null;
private static SessionFactory factory=null;
private static Session session=null;
static{
cfg=new Configuration().configure();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
}
public static Session getSession(){
if(factory!=null)
return factory.openSession();
factory=cfg.buildSessionFactory(new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build());
return factory.openSession();
}
public static void closeSession(){
if(session!=null&&session.isOpen()){
session.close();
}
}
}
hibernate.cfg.xml配置
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库配置信息 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql:///hibernatetest
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 映射文件 -->
<mapping resource="com/test/pojo/Score.hbm.xml" />
</session-factory>
</hibernate-configuration>
执行测试类里的testCreateDB方法,会在控制台打印如下结果:
打开数据库会发现Score表已被创建。