參考文章:http://www.cnblogs.com/mrsui/archive/2013/06/20/3146335.html
本文環境:eclipse4.4.0+hibernate4.1+hibernate tools4.0.1+mysql5.7
1 安裝hibernate tools插件:
help -> Eclipse Marketplace
注:eclipse會搜索出適合該版本eclipse安裝的hibernate tools,點擊安裝即可安裝
2 創建hibernate配置文件hibernate.cfg.xml及console配置文件
2.1新建Java或者Java web項目,這裏建立了一個Java web項目hibernate_taotao,建立cn.com.pojo package存放pojo等
2.2選中項目,右鍵-> new -> other -> hibernate -> hibernate configuration file
2.3然後 next,配置console文件,創建了配置文件hibernate.cfg.xml
<?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.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">ccssxx</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/taotao?characterEncoding=utf-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
</session-factory>
</hibernate-configuration>
3 創建hibernate.reveng.xml文件:
3.1選中項目,右鍵 -> new -> other -> hibernate -> hibernate reverse engineering file
3.2下一步,配置要生成POJO對應的表
3.3finish生成hibernate.reveng.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<table-filter match-catalog="taotao" match-name="tb_content"/>
<table-filter match-catalog="taotao" match-name="tb_content_category"/>
<table-filter match-catalog="taotao" match-name="tb_item"/>
<table-filter match-catalog="taotao" match-name="tb_item_cat"/>
<table-filter match-catalog="taotao" match-name="tb_item_desc"/>
<table-filter match-catalog="taotao" match-name="tb_item_param"/>
<table-filter match-catalog="taotao" match-name="tb_item_param_item"/>
<table-filter match-catalog="taotao" match-name="tb_order"/>
<table-filter match-catalog="taotao" match-name="tb_order_item"/>
<table-filter match-catalog="taotao" match-name="tb_order_shipping"/>
<table-filter match-catalog="taotao" match-name="tb_user"/>
</hibernate-reverse-engineering>
4 反向工程創建pojo:
4.1 在java模式下的hibernate code generation configuration菜單中配置
4.2點擊run配置成功,生成pojo和對應的XX.hbm.xml文件
5 一般自動生成的mapping文件(XX.hbm.xml)都需要修改後才能使用: