hibernate在maven中自动生成
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中自动生成的更多相关文章
- hibernate从数据库中自动生成
计应134(实验班) 李佳鸿 DB Brower配置 1.依次选择window-Open Perspective-MyEclipse Explorer
- 在 Linux 中自动生成 Cordova/Phonegap for Android 的 APK 安装程序
在 Linux 中自动生成 Cordova/Phonegap for Android 的 APK 安装程序 本贴首发于: http://xuekaiyuan.com/forum.php?mod=vie ...
- IntelliJ IDEA 中自动生成 serialVersionUID 的方法
as, idea plugin中搜如下关键字,并安装该插件: GenerateSerialVersionUID 如上图所示,创建一个类并实现Serializable接口,然后按alt+Enter键,即 ...
- 在PowerDesigner中自动生成sqlserver字段备注
在PowerDesigner中自动生成sqlserver字段备注 PowerDesigner是数据库设计人员常用的设计工具,但其自生默认生成的代码并不会生成sqlserver数据库的字段备注说明.在生 ...
- 二十四、详述 IntelliJ IDEA 中自动生成 serialVersionUID 的方法
当我们用 IntelliJ IDEA 编写类并实现 Serializable(序列化)接口的时候,可能会遇到这样一个问题,那就是: 无法自动生成serialVersionUID. 而serialVer ...
- eclipse中自动生成注释
eclipse中自动生成注释 包前缀设置的地方 注释模板设置的地方 Eclipse自动生成方法注释 快捷键 自动生成方法的注释格式,例如 /*** @param str* @return* @thro ...
- Eclipse中自动生成get/set时携带注释给get/set
Eclipse中自动生成get/set时携带注释给get/set 编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclips ...
- 从JSON中自动生成对应的对象模型
编程的乐趣和挑战之一,就是将体力活自动化,使效率成十倍百倍的增长. 需求 做一个项目,需要返回一个很大的 JSON 串,有很多很多很多字段,有好几层嵌套.前端同学给了一个 JSON 串,需要从这个 J ...
- maven intall在target文件夹中自动生成的war包部署服务器时缺斤少两
1.问题描述,本地改动特别大或者升级系统操作,打war包部署服务器上程序时候,页面或者后台总是报错,原因就是比本地少东西. 2.问题排查解决:maven clean然后maven intall在tar ...
随机推荐
- SpringBoot+FreeMarker开发word文档下载,预览
背景: 开发一个根据模版,自动填充用户数据并下载word文档的功能 使用freemarker进行定义模版,然后把数据进行填充. maven依赖: <parent> <groupId& ...
- 一、Redis 基础命令---总括
1.redis命令不区分大写和小写.可是KEY区分大写和小写. 2.redis-cli -h 127.0.0.1 -p 6379 依据IP/PORT链接服务端 3.redis-server --por ...
- git的经常使用命令
$ git config --global user.name "姓名" $ git config --global user.email "xxx@qq.com&quo ...
- UVA 4855 Hyper Box
You live in the universe X where all the physical laws and constants are different from ours. For ex ...
- CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)
题意:2个人从1走到n,假设一条路第一次走则是价值di,假设第二次还走这条路则须要价值di+ai,要你输出2个人到达终点的最小价值! 太水了!一条边建2次就OK了.第一次价值为di,第二次为ai+di ...
- oc24--description
// Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; double ...
- 让git for windows记住密码
store 执行这个命令git config --global credential.helper store 检查命令是否成功 $ git config -l | grep credentialcr ...
- Linux - vim的基本使用
通过which指令来查看文件位置! [root@local ~]# which vim /usr/bin/vim [root@local ~]# which vi /usr/bin/vi [root@ ...
- 省市区js三级联动(原创)
看了一些网上的js三级联动,但感觉不是缺这,就是少那,决定亲自操刀写了一个,现记录如下,以备后用! <!DOCTYPE html> <html> <head> &l ...
- Centos 6 搭建邮箱服务器教程
Centos 6 搭建邮箱服务器主要是是包括了Postfix, Dovecot和 MySQL了,下文我们详细的为各位介绍Centos 6 搭建邮箱服务器教程(Postfix, Dovecot和 MyS ...