用JUnit4自动生成Mysql的表,分别用xml与annotation方式

 

首先:在hibernate.cfg.xml中:

<mapping resource="com/mapping/msb/Student.hbm.xml"/>

         <mapping class="com.mapping.msb.Teacher"/>

第二:做映射

xml映射:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.mapping.msb.Student" table="student">

<id name="id">

<generator class="native"></generator>

</id>

<property name="name"></property>

</class>

</hibernate-mapping>

annotation映射:

@Entity

public class Teacher {

private int id;

private String name;

@Id

public int getId() {

return id;

}

……

第三:写Junit4文件

public class TestStudent {

public void testSchemaExport(){

//new SchemaExport(new Configuration().configure()).create(true, true);

new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);

}

}

 

public class TestTeacher {

@Test

public void testSchemaExport(){

//new SchemaExport(new Configuration().configure()).create(true, true);

new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);

}

}

测试成功

------------------------------------------------------------------Over---------------------------------------------------------------------------

注意以下几点:

1.如果忘了导入mysql的jar包,是不会报错的,但也不能建表。

2.当xml方式和ann方式都存在时,只能用AnnotationConfiguration()

3. 只要hibernate.cfg.xml中有,就会生成表

4.有时利用Myeclipse生成的java文件前面没有"+"号,也就是在JUnit4中不能测试单个@Test.

   解决办法,调出OutLine窗口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值