一、新建工程Demo(如果选的时候勾选了hibernate,IDEA会自动下载Hibernate包,不需要手动导入)



二、导入相关包 Mysql && Hibernate



三、添加Hibernate



四、自动生成配置文件

1.连接数据库(View-Tool Windows-datebase)

如果test connection是灰色的,仔细看Driver这个位置,需要导入mysql的包,IDEA可以直接下载。或者手动导入



2.选择Persistence

五、主函数



六、运行结果

相关代码:

UserEntity.hbm.xml(自动生成)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="com.war.hibernate.UserEntity" table="user" schema="demo">
<id name="id" column="id"/>
<property name="username" column="username"/>
<property name="password" column="password"/>
<property name="address" column="address"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml(需要配置数据库用户和密码)

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory> <!--配置数据库信息-->
<property name="connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<mapping class="com.war.hibernate.UserEntity"/>
<!--配置文件连接到核心文件-->
<mapping resource="com/war/hibernate/UserEntity.hbm.xml"/>
<!-- DB schema will be updated if needed -->
<!-- 设置数据库创建表的策略 -->
<!--<property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>

TestMain.java

package com.war.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; /**
* Created by Administrator on 2017/6/22.
*/
public class TestMian {
public static void main(String[] args) {
//1.加载HIbernate核心配置文件
Configuration cfg = new Configuration();
cfg.configure();
//2.使用sessionFactory
SessionFactory sessionFactory = cfg.buildSessionFactory();
//3使用sessionfactory 实例化session对象
Session session = sessionFactory.openSession();
//4.开始事务
Transaction transaction = session.beginTransaction();
UserEntity userEntity = new UserEntity();
userEntity.setUsername("Susan1");
userEntity.setPassword("12345");
userEntity.setAddress("TaiBei");
//5.调用 session 保存 数据。
session.save(userEntity);
//6.commit transaction
transaction.commit();
//7.close Database resources
session.close();
sessionFactory.close(); } }

UserEntity.java(自动生成)

package com.war.hibernate;

import javax.persistence.*;

/**
* Created by Administrator on 2017/6/22.
*/
@Entity
@Table(name = "user", schema = "demo", catalog = "")
public class UserEntity {
private int id;
private String username;
private String password;
private String address; @Id
@Column(name = "id")
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} @Basic
@Column(name = "username")
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} @Basic
@Column(name = "password")
public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Basic
@Column(name = "address")
public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; UserEntity that = (UserEntity) o; if (id != that.id) return false;
if (username != null ? !username.equals(that.username) : that.username != null) return false;
if (password != null ? !password.equals(that.password) : that.password != null) return false;
if (address != null ? !address.equals(that.address) : that.address != null) return false; return true;
} @Override
public int hashCode() {
int result = id;
result = 31 * result + (username != null ? username.hashCode() : 0);
result = 31 * result + (password != null ? password.hashCode() : 0);
result = 31 * result + (address != null ? address.hashCode() : 0);
return result;
}
}

原文:http://www.jianshu.com/p/c920962872d3

IDEA 自动生成Hibernate实体类和Mapping文件的更多相关文章

  1. Eclipse从数据库逆向生成Hibernate实体类和映射文件(Eclipse插件系列之HibernateTools)

    ♣下载安装Eclipse插件(HibernateTools) ♣Eclipse连接数据库(Mysql5.7) ♣新建hibernate.properties和hibernate.cfg.xml文件 ♣ ...

  2. MyEclipse自动生成hibernate实体类和配置文件攻略

    步骤1:找到导航栏里面的window--showView然后输入db brower,打开数据库浏览窗口步骤2:在数据库浏览窗口里只有一个Myeclipse自带的数据库,该数据没有用,我们在空白的地方右 ...

  3. 反向生成hibernate实体类和映射文件

    工欲善其事,必先利其器.我们可以使用IDE来根据数据库中的表反向生成实体类和映射文件,虽然这些东西手写也并不是难度很大,但是如果存在大量的简单工作需要我们做,也会显得很麻烦. 写在前面 我们反向生成的 ...

  4. (转)MyEclipse自动生成Hibernate实体类, oracle篇

    转自http://blog.csdn.net/hejinwei_1987/article/details/9465529 1.打开 windows -> Open Perspective -&g ...

  5. (转) Eclipse通过HibernateTools实现逆向生成Hibernate实体类

    背景:工作中使用Hibernate进行持久化的开发工作,所以有必要详细了解这方面的知识. ps:这里有个问题就是刷新表的时候速度太慢了.还不如自己手动去创建.如果表太多倒是可以采取批量生成的策略. 在 ...

  6. eclipse从数据库逆向生成Hibernate实体类

    做项目必然要先进行数据库表设计,然后根据数据库设计建立实体类(VO),这是理所当然的,但是到公司里做项目后,让我认识到,没有说既进行完数据库设计后还要再“自己”建立一变VO.意思是,在项目设计时,要么 ...

  7. [转]eclipse借助hibernate tool从数据库逆向生成Hibernate实体类

    如何从数据库逆向生成Hibernate实体类呢??? 1. 首先,要在eclipse中采用自带的数据库管理器(Data Management),连通你的数据库: 然后选择数据库,这里用的oracle, ...

  8. Mysql逆向工程效率神器之使用IDE自动生成Java实体类

    Mysql逆向工程效率神器之使用IDE自动生成Java实体类 简介:实战使用IDE根据Mysql自动生成java pojo实体类 1.IDEA连接数据库 菜单View→Tool Windows→Dat ...

  9. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-6.Mysql逆向工程效率神器之使用IDE自动生成Java实体类

    笔记 6.Mysql逆向工程效率神器之使用IDE自动生成Java实体类     简介:实战使用IDE根据Mysql自动生成java pojo实体类                  1.IDEA连接数 ...

随机推荐

  1. 服务定位器(Service Locator)

    服务定位器(Service Locator) 跟DI容器类似,引入Service Locator目的也在于解耦.有许多成熟的设计模式也可用于解耦,但在Web应用上, Service Locator绝对 ...

  2. A锚点实现,滚动页面内容改变tab选项

    Css: ul{margin:0;padding:0;list-style:none;} a{ text-decoration: none; outline:none; -webkit-tap-hig ...

  3. 微信和QQ内置浏览器为什么老是弹停止访问该网页,微信域名被屏蔽的解决办法

    近来很多商家开始重视域名防封的技术了,为什么呢,因为实在是封怕了.三天两头就得去换域名,换域名是小事,用户流失就是大事了,直接跟利益挂钩的.那么域名防封技术究竟有多重要呢?又该如何实现域名防封呢?下面 ...

  4. php上传文件,接口是java,go。

    $uri = ‘https://www.xxx.com/api/xxxxx’; $ch = curl_init(); //加@符号curl就会把它当成是文件上传处理 $tmpName = $_FILE ...

  5. ldap 导出、导入ldif数据

    ldap 导出.导入ldif数据有如下方式: 1.dsadm(速度快,需要停止ldap实例) 2.dsconf(速度慢,需要保持ldap实例开启) windows导出.导入需要加上参数--unsecu ...

  6. SharePoint CU、Hotfix和SP版本的区别

    1.Hotfix:通常是对一个特殊问题的修复包 2.CU(Cumulative Update):Hotfix的集合,包含从上一个SP(Service Pack)版本以来所有的Hotfix 3.SP(S ...

  7. [Err] 1093 - You can't specify target table 'master_data' for update in FROM clause

    delete from master_data where category_id not in (select category_id from master_data a, bc_category ...

  8. rebar3自动编译

    功能:修改完代码可以自动编译加载到VM中 必须安装的软件: Linux: inotify  链接https://github.com/rvoicilas/inotify-tools/wiki 配置: ...

  9. GIT 初始化 中文编码、自动换行

    解决中文编码: git config --global core.quotepath false git config --global gui.encoding utf-8 解决git log 中文 ...

  10. bzoj2369

    题解: 显然把每一个环求出来 然后做一个lcm即可 代码: #include<cstdio> using namespace std; ],f[],n; int gcd(int x,int ...