使用SchemaExport生成数据库表

今天来演示下使用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表已被创建。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值