首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

调整SSH 数据库表能否根据映射文件自动生成

2012-10-19 
整合SSH 数据库表能否根据映射文件自动生成数据库为mysql个人觉得如果可以 应该需要在下面这段代码里添加

整合SSH 数据库表能否根据映射文件自动生成
数据库为mysql 


个人觉得如果可以 应该需要在下面这段代码里添加点什么吧。。。希望懂得给回答下 。。。谢谢

  <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://127.0.0.1/myTest"/>
  <property name="username" value="root"/>
  <property name="password" value="12345"/>
  <property name="maxActive" value="100"/>
  <property name="maxIdle" value="30"/>
  <property name="maxWait" value="500"/>
  <property name="defaultAutoCommit" value="true"/>
  </bean>


映射文件

<?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.test.bean.User" table="t_user">
<id name="id">
<generator class="native"/> 
</id>
<property name="firstname"/>
  <property name="lastname"/>
  <property name="age"/>
</class>
</hibernate-mapping>

[解决办法]
写下面一个类,运行后看你数据库 ,直接从对象模型导入关系模型。。。
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class HBM2DDL {

/**
* @param args
*/
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}

}
[解决办法]
1.创建Hibernian配置文件hibernate.cfg.xml

2. 定义实体类User

3.定义User对象的映射文件User.hbm.xml

4.将映射文件User.hbm.xml加入到hibernate.cfg.xml中

5.创建数据库,用hibernate工具类将实体映射导入到数据库中
public class ExportDB {
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
se.create(true, true);
}
}

热点排行