快速入门

      1. 下载Hibernate框架的开发包

      2. 编写数据库和表结构

Create database hibernate_day01;

Use hibernate_day01;

CREATE TABLE `cst_customer` (

  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',

  `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)',

  `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',

  `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id',

  `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源',

  `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业',

  `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别',

  `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人',

  `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',

  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',

  PRIMARY KEY (`cust_id`)

) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;

  3. 创建WEB的项目,导入了开发的jar包

           * MySQL驱动包、Hibernate开发的必须要有的jar包、日志的jar包

  4. 编写JavaBean,以后不使用基本数据类型,使用包装类

      5. 编写映射的配置文件(核心),先导入开发的约束,里面正常配置标签

      6. 编写hibernate的核心的配置文件,里面的内容是固定的

      7. 编写代码,使用的类和方法   

需要jar包的,留下邮箱!

原始代码分享

 package com.itheima.doman;
/**
* Javabean
* @author Administrator
*
*/
public class Customer { //使用包装类,默认值是null
private Long cust_id;
private String cust_name;
private Long cust_user_id;
private Long cust_create_id; private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_linkman;
private String cust_phone;
private String cust_mobile;
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 Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
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_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
} }
 <?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.itheima.doman.Customer" table="cst_customer">
<!-- 配置id
见到name属性,JavaBean属性
见到column属性,是表结构的字段
-->
<id name="cust_id" column="cust_id">
<!-- 主键的生成策略 -->
<generator class="native"/>
</id>
<!-- 配置其他属性 -->
<property name="cust_name" column="cust_name"/>
<property name="cust_user_id" column="cust_user_id"/>
<property name="cust_create_id" column="cust_create_id"/>
<property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_linkman" column="cust_linkman"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/> </class>
</hibernate-mapping>
<?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>
<!-- 先配置sessionFactory标签,一个数据库对应一个SessionFactory标签 -->
<session-factory> <!-- 必须配置的参数5个,4大参数,数据库方言 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property> <!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 可选配置 --> <!-- 映射配置文件,需要引入的映射配置文件 -->
<mapping resource="com/itheima/doman/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
package com.itheima.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test; import com.itheima.doman.Customer; /**
* 测试hibernate框架
*
* @author Administrator
*
*/ public class Demo1 {
/*
* 测试保存客户
*/
@Test
public void testSave() {
/*
* 1.先加载配置文件
* 2.创建SessionFactory对象,生成Session对象
* 3.创建Session对象
* 4.开启事物
* 5.编写保存代码
* 6.提交事务
* 7.释放资源
*/
//1.加载配置文件
Configuration config=new Configuration();
//默认加载hibernate.cfg.xml配置文件
config.configure();
//创建SessionFactory对象
SessionFactory factory=config.buildSessionFactory();
//创建session对象
Session session=factory.openSession(); //开启事务
Transaction tr=session.beginTransaction(); //编写保存代码
Customer c=new Customer();
c.setCust_name("测试");
c.setCust_phone("110");
c.setCust_level("1"); session.save(c); //提交事务
tr.commit();
//释放资源
session.close();
factory.close();
}
}

 

hibernate入门程序的更多相关文章

  1. Hibernate入门(1)-第一个Hibernate程序

    Hibernate入门(1)-第一个Hibernate程序 Hibernate是最著名的ORM工具之一,本系列文章主要学习Hibernate的用法,不涉及Hibernate的原理.本文介绍第一个Hib ...

  2. Hibernate系列1:入门程序

    1.传统的java数据库连接 在传统的开发中,如果要建立java程序和数据库的连接,通常采用JDBC或者Apache Commons DbUtils开发包来完成.他们分别有以下特点: JDBC: 优点 ...

  3. Hibernate入门案例及增删改查

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  4. Hibernate入门案例 增删改

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  5. Hibernate入门6.Hibernate检索方式

    Hibernate入门6.Hibernate检索方式 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv Hibernate的整体框架已经 ...

  6. Hibernate入门5持久化对象关系和批量处理技术

    Hibernate入门5持久化对象关系和批量处理技术 20131128 代码下载 链接: http://pan.baidu.com/s/1Ccuup 密码: vqlv 前言: 前面学习了Hiberna ...

  7. Hibernate入门3.配置映射文件深入

    Hibernate入门3.配置映射文件深入 2013.11.27 前言: 之前的两节是在Java项目中如何使用hibernate,并且通过一个简单地项目实践,期间有很多的错误,一般都是因为配置包的问题 ...

  8. 简单的Hibernate入门简介

    其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...

  9. 01.Hibernate入门

    前言:本文用一个简单的Hibernate应用程序例子来引领初学者入门,让初学者对Hibernate的使用有一个大致的认识.本文例子使用了MySQL数据库.Maven管理工具.Eclipse开发工具,创 ...

随机推荐

  1. 广度优先遍历(BFS )(转)

    宽度优先搜索(BFS, Breadth First Search)是一个针对图和树的遍历算法.发明于上世纪50年代末60年代初,最初用于解决迷宫最短路径和网络路由等问题. 对于下面的树而言,BFS方法 ...

  2. c# c/s 框架读取的配置文件时是app.exe.config

    c# c/s 框架读取的配置文件时是app.exe.config, 一般在bin中间中俄debug中或者Release中

  3. matplotlib-形状

    需要   import matplotlib.patches as mp import numpy as np import matplotlib.pyplot as plt import matpl ...

  4. ARC071D Infinite Sequence

    传送门 仔细观察可以发现,如果在一个\(> 1\)的数后面放一个\(> 1\)的数,那么后面的序列也就确定了,所以我们考虑dp出特定长度的序列,然后在后面加上能确定序列的数来贡献答案 为了 ...

  5. luogu P3241 [HNOI2015]开店

    传送门 (下面记年龄为\(a_x\))题目要求的是\[\sum_{x=1}^{n} [a_x\in [l,r]]*dis(x,u)=\sum_{x=1}^{n} [a_x\in [l,r]]*de_x ...

  6. IBM X3650 M5服务器RAID阵列设置

    生产环境中的raid配置说明: 一. 开机后,注意引导界面,按F1键进入BIOS进行设置 二. 进入BIOS后,选择system setting--storage ,进入磁盘阵列配置界面,可以看到M5 ...

  7. webpack 优化

    1 优化loader配置 1.1 缩小文件匹配范围(include/exclude)   通过排除node_modules下的文件 从而缩小了loader加载搜索范围 高概率命中文件  module: ...

  8. Spring Cloud学习资料

    博客 1.跟我学Spring Cloud 2.周立|Spring Cloud 3.Spring Cloud基础教程(强烈推荐) 4.Spring Cloud系列文章 5.forezp|史上最简单的 S ...

  9. 2016 alictf Timer writeup

    Timer-smali逆向 参考文档:http://blog.csdn.net/qq_29343201/article/details/51649962 题目链接: https://pan.baidu ...

  10. ARM核心板_迅为imx6工控核心板_核心板中的小新潮

    ARM核心板_迅为imx6工控核心板_核心板中的小新潮核心板参数 尺寸 51mm*61mm CPU Freescale Cortex-A9 四核 i.MX6Q,主频 1.2 GHz 内存 2GB DD ...