【5.1】SchemaExport的使用(创建表)和数据库方言的理解

1.SchemaExport的使用【创建表】

public void testCreateDB(){
		Configuration cfg = new Configuration().configure();
		SchemaExport se = new SchemaExport(cfg);
		//第一个参数 是否生成ddl脚本  第二个参数  是否执行到数据库中
		se.create(true, true);
	}

例子:

1.1 Score对象类:

package cn.siggy.pojo;
/**
 * 成绩
 * */
public class Score {
	private ScoreId scoreId;
	private double result;//成绩
	
	public ScoreId getScoreId() {
		return scoreId;
	}
	public void setScoreId(ScoreId scoreId) {
		this.scoreId = scoreId;
	}
	public double getResult() {
		return result;
	}
	public void setResult(double result) {
		this.result = result;
	}
}

 

1.2 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="cn.siggy.pojo">
	<class name="Score">
		<!-- 组合主键 -->
		<composite-id name="scoreId" class="ScoreId">
			<key-property name="stuId"></key-property>
			<key-property name="subjectId"></key-property>
		</composite-id>
		<property name="result"/>
	</class>
</hibernate-mapping>

  

1.3 测试类:

 

package cn.siggy.test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

import cn.siggy.pojo.Score;
import cn.siggy.pojo.ScoreId;
import cn.siggy.util.HibernateUtil;

public class HibernateTest {
	@Test
	public void testCreateDB(){
		Configuration cfg = new Configuration().configure();
		SchemaExport se = new SchemaExport(cfg);
		//第一个参数 是否生成ddl脚本  第二个参数  是否执行到数据库中
		se.create(true, true);
	}

 

1.4 运行结果:

 

 

 

 

 

 

  


 

2.数据库方言的理解:<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

  • 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:///hibernate4</property>
    		<property name="connection.username">root</property>
    		<property name="connection.password">root</property>
    		
    		<!--hibernate可选项 -->
    		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    		<property name="show_sql">true</property>
    		<property name="format_sql">true</property>
    		<!-- hbm文件 -->
    		<mapping resource="cn/siggy/pojo/Score.hbm.xml"/>
    	</session-factory>
    </hibernate-configuration>
    

      

  举例来说,我们在MySQL数据库里进行分页查询,只需使用limit关键字就可以了;而标准SQL并不支持limit关键字,例如Oracle则需要使用行内视图的方式来进行分页。同样的应用程序,当我们在不同数据库之间迁移时,底层数据库的访问细节会发生改变,而Hibernate也为这种改变做好了准备,现在我们需要做的是:告诉Hibernate应用程序的底层即将使用哪种数据库——这就是数据库方言。

  一旦我们为Hibernate设置了合适的数据库方言,Hibernate将可以自动应付底层数据库访问所存在的细节差异。

  不同数据库所应使用的方言如表5.1所示:

  

 

 

posted @ 2017-04-06 22:36  chxbar  阅读(296)  评论(0编辑  收藏  举报