日期:2014-05-17  浏览次数:20624 次

整合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);
}
}