如何创建第一个hibernate项目

1.创建项目

2.导包

3.写配置文件

4.创建实体类和数据库

5.创建测试类

以上是需要导入的包

创建一个Customer.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.tz.domain.Customer" table="cst_customer">   <!-- 复制Customer完整的类名到name,把表名填到table --><id name="cust_id" column="cust_id">                        <!-- 把表中的cust_id复制到name,column列名也是这个   -->   <generator class="native"></generator>        <!-- class中填写native先不介绍 --></id><property name="cust_name" column="cust_name"></property>         <!-- 把实体类中的属性复制进来,属性和列名一样 -->     <property name="cust_source" column="cust_source"></property>     <!-- 属性相当于数据库中的字段-->  <property name="cust_industry" column="cust_industry"></property>         <property name="cust_level" column="cust_level"></property>       <property name="cust_linkman" column="cust_linkman"></property>       <property name="cust_phone" column="cust_phone"></property>       <property name="cust_mobile" column="cust_mobile"></property>         </class></hibernate-mapping>

再创建一个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">
<hibernate-configuration><session-factory><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>    <!-- 驱动 --><property name="hibernate.connection.url">jdbc:mysql:///nice</property>         <!-- 地址 三个斜杠可以省略localhost--><property name="hibernate.connection.username">root</property>     <!-- 用户名 --><property name="hibernate.connection.password">nice</property>     <!-- 密码 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    <!-- 数据库方言不同的数据库中,sql语句略有区别,指定方言可以让hibernate框架生成sql语句时,针对数据库的方言生成sql语句都基于sql99标准DDL:定义语言 数据库表的增删改查DCL: 控制语言 事务 权限DML: 操作语言 增删该查注意事项:mysql在选择方言时,请选择最短的方言 hibernate.dialect org.hibernate.dialect.MySQLDialect--><property name="hibernate.show_sql">true</property>        <!-- 把hibernate生成的sql语句显示在控制台上 --><property name="hibernate.format_sql">true</property>    <!-- 把打印的sql语句格式化    不然sql语句都显示在一行上--><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="com/tz/domain/Customer.hbm.xml"/></session-factory></hibernate-configuration>

创建测试类

package com.tz.test.a_hello;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;import com.tz.domain.Customer;//测试Hibernate框架
public class Demo {@Test//保存客户public void fun1(){Configuration conf = new Configuration().configure();SessionFactory sessionFactory = conf.bulidSessionFactory();Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();//-----华丽的分割线-----Customer c = new Customer();c.setCust_name("潭州教育");session.save(c);//执行保存//-----华丽的分割线-----tx.commit();session.close();sessionFactory.close();}
}

创建实体类

package com.tz.domain;public class Customer {/*** CREATE TABLE `cst_customer` (`cust_id` bigint(20) NOT NULL AUTO_INCREMENT,`cust_name` varchar(255) DEFAULT NULL,`cust_source` varchar(255) DEFAULT NULL,`cust_industry` varchar(255) DEFAULT NULL,`cust_level` varchar(255) DEFAULT NULL,`cust_linkman` varchar(255) DEFAULT NULL,`cust_phone` varchar(255) DEFAULT NULL,`cust_moblie` varchar(255) DEFAULT NULL,`cust_mobile` varchar(255) DEFAULT NULL,PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=gbk;*/private Long cust_id;private String cust_name;private String cust_source;private String cust_industry;private String cust_level;private String cust_linkman;private String cust_phone;private String cust_moblie;public Long getCust_id() {return cust_id;}public void setCust_id(Long cust_id) {this.cust_id = cust_id;}public String getCust_name() {return cust_name;}public void setCust_name(String cust_name) {this.cust_name = cust_name;}public String getCust_source() {return cust_source;}public void setCust_source(String cust_source) {this.cust_source = cust_source;}public String getCust_industry() {return cust_industry;}public void setCust_industry(String cust_industry) {this.cust_industry = cust_industry;}public String getCust_level() {return cust_level;}public void setCust_level(String cust_level) {this.cust_level = cust_level;}public String getCust_linkman() {return cust_linkman;}public void setCust_linkman(String cust_linkman) {this.cust_linkman = cust_linkman;}public String getCust_phone() {return cust_phone;}public void setCust_phone(String cust_phone) {this.cust_phone = cust_phone;}public String getCust_moblie() {return cust_moblie;}public void setCust_moblie(String cust_moblie) {this.cust_moblie = cust_moblie;}@Overridepublic String toString() {return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";}}

Hibernate教程01相关推荐

  1. hibernate教程_Hibernate多对多教程

    hibernate教程 介绍: 在本教程中,我们将学习使用Hibernate @ManyToMany注释定义和使用多对多实体关联. 上下文构建: 为了继续学习本教程,我们假设我们有两个实体– 雇员和资 ...

  2. Hibernate教程– ULTIMATE指南(PDF下载)

    编者注:在本文中,我们提供了全面的Hibernate教程. Hibernate ORM(简称Hibernate)是一个对象关系映射框架,它有助于将面向对象的域模型转换为传统的关系数据库. Hibern ...

  3. Hibernate教程

    Hibernate教程 最近我写了很多hibernate教程.Hibernate是当前市场上最好的Java ORM工具之一.所以这篇文章就像是hibernate教程和示例的所有帖子的索引.您可以按顺序 ...

  4. H2O Wave教程---基于浏览器的实时显示工具---教程01

    H2O Wave教程-基于浏览器的实时显示工具-教程01 0 写在前面 1 开始-动手操作起来 2 分类:一个是脚本,一个是app 3 脚本怎么写 0 写在前面 总结一下自己学习H2Owave的学习情 ...

  5. hibernate教程_Hibernate教程

    hibernate教程 Recently I have written a lot of hibernate tutorial. Hibernate is one of the best Java O ...

  6. matlab初学者教程_初学者的Hibernate教程

    matlab初学者教程 Welcome to the Hibernate tutorial for Beginners. Hibernate is one of the most widely use ...

  7. 物联网平台搭建教程01

    物联网平台搭建教程01 1 物联网设备如何接入到网络? 2 网络通信方式 3 物联网设备之间,设备与云平台能够交换数据后,接下来要干怎么呢? 4 如何搭建起一个物联网系统框架呢?它的技术架构又是怎么样 ...

  8. React 全家桶入门教程 01

    React 全家桶入门教程 01 前面是基础课程(难度小,略过),后面是案例 目的 巩固react基础知识,查漏补缺(熟悉的部分快进) 学习相关的库的使用 https://study.163.com/ ...

  9. myeclipse数据库逆向hibernate教程 -

    (导入自己的博客园的文章) myeclipse数据库逆向hibernate教程 .首先我们的准备 1.项目 2.数据库 数据库执行命令.sql 1 /*2 Navicat MySQL Data Tra ...

  10. Python tkinter教程-01:创建窗口

    Python tkinter教程-01:创建窗口 文章目录 0 准备工作 1 创建空白窗体 2 窗口标题 3 窗口大小与位置 4 锁定窗口大小 5 窗口图标 6 隐藏与显示窗口 其它 0 准备工作 要 ...

最新文章

  1. 2022-2028年中国DMF行业市场研究及前瞻分析报告
  2. 百度增长复苏,市值一夜涨160亿;李彦宏:技术价值再度闪耀,宁可创新冒险也不平庸保守...
  3. libreadline.so.6: undefined symbol
  4. mapper中 <include refid=“XXX“></include>标签 <sql id=“XXX“>标签
  5. web前端试题和答案
  6. pymysql(part1)--pymysql初识之检索/增加/更新/删除数据
  7. a/a的4种链接方式
  8. c语言goord函数,park、unpark、ord 函数使用方法(转)
  9. 用shell写的ftp工具
  10. .net2.0中对config文件的操作方法总结
  11. python手册app_Python 中文手册离线版下载_Python3.6中文手册CHM下载 Python3.6中文手册CHM 免费版_当载软件站...
  12. 《区块链开源技术需求调研报告》拍了拍你
  13. 实时视频带宽的计算过程
  14. 获取百度首页的源代码
  15. distribute by 和 partitioned by 区别
  16. 程序员技术面试与HR谈薪资技巧的一些经验心得
  17. 计算机无法进入增值税认证平台,增值税发票综合服务平台热点问题解答 (第三期)...
  18. Microsoft Visio 2010 - 弧线
  19. 两台电脑怎么文件互传,电脑和电脑互传文件怎么传,两台电脑怎么互传文件
  20. 使用nexus搭建maven私服教程详解

热门文章

  1. 小怪掉落碎片及下级符文(灵晶)一览
  2. MATLAB下载+安装教程
  3. python常见运维脚本_Python运维常用脚本
  4. uniapp编译成小程序代码过大的处理方法
  5. 30个Python小游戏,上班摸鱼我能玩一天【内附源码】
  6. python实现设计模式
  7. python实现常见的设计模式
  8. php bin2hex 反向,PHP bin2hex()和pack()函数
  9. ABAQUS6.10 VS2008 Intel fortan11.1
  10. 2:Carmaker+Simulink+Canoe联合仿真,搭建ADAS HIL测试环境