eclipse使用hibernate tools插件生成mysql数据表的pojo

来源:互联网 发布:无间道风云知乎 编辑:程序博客网 时间:2024/05/29 02:03

参考文章: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)都需要修改后才能使用:

阅读全文
0 0