用SchemaExport生成數據庫
首先導包:到Windows/Preferences/java/Build Path/User Libreries裏面添加 mysql.jar包,hibernate-4.jar包(required),struts2.jar包(解壓blank.war文件即可得到),junit.jar包。再到項目右擊properties到裏面添加build path
1.建立entity
1.學生類
package entity;
import java.util.Date;
public class Student {
private String sid;
private String sname;
private String gender;
private Date birthday;
private String address;
public Student(){
}
public Student(String sid, String sname, String gender, Date birthday,
String address) {
//super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
@Override
public String toString() {
return "Student [address=" + address + ", birthday=" + birthday
+ ", gender=" + gender + ", sid=" + sid + ", sname=" + sname
+ "]";
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.用戶類
package entity;
public class User {
private int uid;
private String username;
private String password;
public User(){
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public User(int id, String username, String password) {
//super();
this.uid = id;
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.創建對象關係映射文件
1.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="STUDENTS">
<id name="sid" type="java.lang.String" length="19">
<generator class="assigned"/>
</id>
<property name="sname" type="java.lang.String"/>
<property name="gender" type="java.lang.String"/>
<property name="birthday" type="date"/>
<property name="address" type="date"/>
</class>
</hibernate-mapping>
2.User.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.User"
table="USERS">
<id name="uid" type="int">
<generator class="native"/>
</id>
<property name="username" type="java.lang.String"/>
<property name="password" type="java.lang.String"/>
</class>
</hibernate-mapping>
3.建立hibernate.cfg.xm
<?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.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/testmooc
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="entity/Student.hbm.xml"/>
<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.建立TestStuend類
package entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class TestStudent {
@Test
public void testSchemaExport(){
//創建配置對象
Configuration config=new Configuration().configure();
//創建服務註冊對象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//創建sessionFactory
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//創建session對象
Session session = sessionFactory.getCurrentSession();
//創建SchemaExport對象
SchemaExport export= new SchemaExport(config);
//第一個true輸出表結構,第二個true輸出sql語句
export.create(true, true);
}
}
5.生成數據庫
打開數據庫建立相應的數據庫……單元測試即可.