1.构建数据库连接

2.新建maven项目,利用工具生成hibernate相应的类和xml文件

新建pojo包

右击项目 点击Configure Facets 选择hibernate

选择包

选择驱动

选择jar包

3.数据库反向生成类和xml文件

pom.xml添加mysql的驱动包

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

4.使用junit创建测试类

package com.hwua.test;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test; import com.hwua.pojo.Emps; public class Test1 {
@Test
public void save(){
//1.读取配置文件
//1.1 configure()方法,默认加载src目录下的hibernate.cfg.xml
Configuration cfg=new Configuration().configure(); //1.2如果配置文件不符合加载规则,可以使用以下两种方式加载
//Configuration cfg=new Configuration().configure(file);
//Configuration cfg=new Configuration().configure(path); //1.3可以使用Configuration对象加载映射文件(不推荐)
//cfg.addClass(Emps.class);
//推荐hibernate.cfg.xml 使用mapping属性引入配置文件(自动生成类的时候已经引入) //2.创建SessionFactroy对象
//SessionFactory 创建session的工厂
SessionFactory factory=cfg.buildSessionFactory(); //3.获取session对象
//3.1 获得一个全新的session;
Session session=factory.openSession();
//3.2 获得当前的session,本地线程绑定的session,没有session的时候创建一个新的
//Session currentSession = factory.getCurrentSession(); //4.操作数据库
Emps emp=new Emps();
emp.setEname("Tom");
emp.setAge(20);
emp.setSex("男");
emp.setDeptno(30);
emp.setSalary(10000);
session.save(emp); //5.关闭资源
session.close();
factory.close();
}
}

5.相关属性介绍

5.1主键生成策略:

http://www.cnblogs.com/flyoung2008/articles/2165759.html

5.2 hibernate.cfg.xml配置

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory> <property name="myeclipse.connection.profile">mysql_test</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">oracle</property>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property> <!-- 自动生成表结构 -->
<property name="hbm2ddl.auto">update</property> <!-- 将session与线程绑定, 只有配置了该配置,才能使用currentSession-->
<property name="hibernate.connection.autocommit">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property> <mapping resource="com/hwua/pojo/Locs.hbm.xml" />
<mapping resource="com/hwua/pojo/Depts.hbm.xml" />
<mapping resource="com/hwua/pojo/Emps.hbm.xml" /> </session-factory> </hibernate-configuration>

hibernate在maven中自动生成的更多相关文章

  1. hibernate从数据库中自动生成

    计应134(实验班) 李佳鸿 DB Brower配置 1.依次选择window-Open Perspective-MyEclipse Explorer

  2. 在 Linux 中自动生成 Cordova/Phonegap for Android 的 APK 安装程序

    在 Linux 中自动生成 Cordova/Phonegap for Android 的 APK 安装程序 本贴首发于: http://xuekaiyuan.com/forum.php?mod=vie ...

  3. IntelliJ IDEA 中自动生成 serialVersionUID 的方法

    as, idea plugin中搜如下关键字,并安装该插件: GenerateSerialVersionUID 如上图所示,创建一个类并实现Serializable接口,然后按alt+Enter键,即 ...

  4. 在PowerDesigner中自动生成sqlserver字段备注

    在PowerDesigner中自动生成sqlserver字段备注 PowerDesigner是数据库设计人员常用的设计工具,但其自生默认生成的代码并不会生成sqlserver数据库的字段备注说明.在生 ...

  5. 二十四、详述 IntelliJ IDEA 中自动生成 serialVersionUID 的方法

    当我们用 IntelliJ IDEA 编写类并实现 Serializable(序列化)接口的时候,可能会遇到这样一个问题,那就是: 无法自动生成serialVersionUID. 而serialVer ...

  6. eclipse中自动生成注释

    eclipse中自动生成注释 包前缀设置的地方 注释模板设置的地方 Eclipse自动生成方法注释 快捷键 自动生成方法的注释格式,例如 /*** @param str* @return* @thro ...

  7. Eclipse中自动生成get/set时携带注释给get/set

    Eclipse中自动生成get/set时携带注释给get/set   编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclips ...

  8. 从JSON中自动生成对应的对象模型

    编程的乐趣和挑战之一,就是将体力活自动化,使效率成十倍百倍的增长. 需求 做一个项目,需要返回一个很大的 JSON 串,有很多很多很多字段,有好几层嵌套.前端同学给了一个 JSON 串,需要从这个 J ...

  9. maven intall在target文件夹中自动生成的war包部署服务器时缺斤少两

    1.问题描述,本地改动特别大或者升级系统操作,打war包部署服务器上程序时候,页面或者后台总是报错,原因就是比本地少东西. 2.问题排查解决:maven clean然后maven intall在tar ...

随机推荐

  1. 断路器-Hystrix的深入了解

    前言 高可用相关的技术以及架构,对于大型复杂的分布式系统,是非常重要的.而高可用架构中,非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的各种各样的 ...

  2. eclipse重置页面恢复到最初布局状态

    eclipse重置页面恢复到最初布局状态 window->perspective->reset perspective

  3. UVA 10039 Railroads

    这道题好吧,一开始便是拓扑排序的想法,搞了好久,试了多组测试数据,没错啊,可是没过...作孽啊,竟然忘了拓扑不能处理环,白浪费了一晚上... 只好用动态规划了.. DP[time][city]表示在t ...

  4. currentThread()方法返回代码段正在被哪个线程调用

    currentThread()方法返回代码段正在被哪个线程调用 package com.stono.thread2.page16; public class MyThread extends Thre ...

  5. [Javascript Crocks] Recover from a Nothing with the `alt` method

    Once we’re using Maybes throughout our code, it stands to reason that at some point we’ll get a Mayb ...

  6. POJ 2167 Irrelevant Elements 质因数分解

    Irrelevant Elements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2231   Accepted: 55 ...

  7. 在java中,怎样跳出当前的多重循环?

    </pre>直接用break ;详细举比例如以下:<p></p><p></p><p></p><pre name ...

  8. HDOJ GCD 2588【欧拉函数】

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. C C++每个头文件的功能说

    C/C++每个头文件的功能说明 传统 C++ #include <assert.h> //设定插入点 #include <ctype.h> //字符处理 #include &l ...

  10. bootstrap模态框通过传值解决重复提交问题

    自己通过模态框确认是否提交的功能时,总是重复提价上次的请求. 原因:重复的原因是jquery通过id绑定了确定按钮的onclick事件,所以每次提交都会增加 一次绑定(没有清除上次的绑定),才造成了重 ...