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. APP漏洞自动化扫描专业评测报告(下篇)

    上篇.中篇回顾:通过收费情况.样本测试后的扫描时间.漏洞项对比以及扫描能力这几个方面对阿里聚安全[1].360App漏洞扫描[2].腾讯金刚审计系统[3].百度移动云测试中心[4]以及AppRisk ...

  2. org.openqa.selenium.NoSuchElementException:

    http://www.blogjava.net/qileilove/archive/2014/12/11/421309.html selenium webdriver定位不到元素的五种原因及解决办法 ...

  3. poj1179 区间dp(记忆化搜索写法)有巨坑!

    http://poj.org/problem?id=1179 Description Polygon is a game for one player that starts on a polygon ...

  4. xcode Could not launch "" ; has denied the launch request

    xcode Could not launch "" ;  “”“ has denied the launch request  (注意,这种方式不能调试) 1.编辑scheme 2 ...

  5. Battle City

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7208   Accepted: 2427 Descr ...

  6. 10.TCPIP监听器

    给它做代理之后你这个端口是什么? 做一个代理. Local monitoring port:本地的监听端口.你要给谁做代理,那么给它做代理之后,你这个代理的端口是什么? 现在要给百度做一个代理. 能看 ...

  7. php百度翻译类

    <?php // +---------------------------------------------------------------------- // | PHP MVC Fra ...

  8. IDEA中Lombok插件的安装与使用

    背景   我们在开发过程中,通常都会定义大量的JavaBean,然后通过IDE去生成其属性的构造器.getter.setter.equals.hashcode.toString方法,当要对某个属性进行 ...

  9. Vue组件的三种调用方式

    最近在写fj-service-system的时候,遇到了一些问题.那就是我有些组件,比如Dialog.Message这样的组件,是引入三方组件库,比如element-ui这样的,还是自己实现一个?虽然 ...

  10. SourceInsight3.5 Space 替换Tab

    # SourceInsight3.5 Space 替换Tab 公司要求所有的缩进都要使用空格,而不是Tab.至于使用Tab,还是Space来进行缩进,这在网上有各种各样的讨论,毕竟使用Tab可以节省很 ...