用SchemaExport生成数据库
来源:互联网 发布:信号屏蔽软件 编辑:程序博客网 时间:2024/06/15 16:25
用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.生成数据库
打开数据库建立相应的数据库……单元测试即可.
1 0
- 用SchemaExport生成数据库
- 使用SchemaExport生成数据库表
- 利用ant,hibernate的schemaexport自动生成数据库
- Hibernate整合Spring后,如何使用SchemaExport生成数据库表
- 【Hibernate】——SchemaExport自动生成数据库表
- hibernate使用SchemaExport生成对应的数据库表
- 使用SchemaExport生成数据表
- SchemaExport
- SchemaExport
- 利用hibernate中的SchemaExport生成数据表
- 利用hibernate中的SchemaExport生成数据表
- 使用schemaExport自动生成表结构
- Hibernate之SchemaExport+配置文件生成表结构
- hibernate使用SchemaExport自动生成数据表
- Hibernate5.x 使用SchemaExport生成数据表
- SchemaExport 类使用(自动生成数据表)
- Hibernate 使用SchemaExport自动生成表
- 使用hibernate的SchemaExport来创建数据库
- RxJava练习(2)--时间间隔输出字符串
- Sicily 1818. 成绩转换
- Swift print 小技巧
- 安卓M-SettingsProvider新功能特性总结
- 类 对象 方法 属性
- 用SchemaExport生成数据库
- 如何科学的创建索引
- HttpGet登陆
- 2. AllowOverride以及Options使用详解
- synchronized的原子性以及DCL为什么出错的
- Java并发同步器--CyclicBarrier
- Hackerearth.com编程问题解题思路系列:Chandan and Balanced Strings
- Mysql配置远程连接
- 设计模式篇一(介绍)