第一步:下载Hibernate5的运行环境

  https://sourceforge.net/projects/hibernate/files/hibernate-orm/

第二步:在数据库创建表

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;

第三步:搭建Hibernate的开发环境

  创建WEB工程并且引入Hibernate所需要的包

  • MySQL的驱动jar包

  • Hibernate开发需要的jar包(/lib/required/所有jar包)
  • 日志jar包(资料/jar包/log4j/所有jar包)

第四步:编写JavaBean实体类

    public class Customer {
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;
// 省略get和set方法
}

第五步:创建类与表结构的映射

    <?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.domain.Customer" table="cst_customer">
<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>

第六步:编写Hibernate核心的配置文件

    <?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:///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/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

第七步:编写Hibernate入门代码

    /**
* 测试保存客户
*/
@Test
public void testSave(){
// 先加载配置文件
Configuration config = new Configuration();
// 默认加载src目录下的配置文件
config.configure();
// 创建SessionFactory对象
SessionFactory factory = config.buildSessionFactory();
// 创建session对象
Session session = factory.openSession();
// 开启事务
Transaction tr = session.beginTransaction();
// 编写保存代码
Customer c = new Customer();
// c.setCust_id(cust_id); 已经自动递增
c.setCust_name("测试名称");
c.setCust_mobile("110");
// 保存客户
session.save(c);
// 提交事务
tr.commit();
// 释放资源
session.close();
factory.close();
}

最新文章

  1. unsafe
  2. EL表达式的算术运算
  3. Android Studio 2.2.2导入Eclipse中创建的项目
  4. js 防止button频繁点击
  5. PgSQL &#183; 特性分析 &#183; 谈谈checkpoint的调度
  6. 如何进行js动态生成option?如何实现二级连动?
  7. 博客不再更新,已转移到自己的小站iwenku.net
  8. Linux下的绘图(流程图、UML、mindmap)工具
  9. Android开发中Handler的经典总结--转载至网络
  10. OpenCV原则解读HAAR+Adaboost
  11. Python 3 使用venv创建虚拟环境
  12. 【SRM-07 D】天才麻将少女KPM
  13. 系统环境变量(就是不需要切换目录,敲击“python”就可以进入编码器)
  14. Bytom合约预编译
  15. BASH if/while/until loop
  16. linux&amp;php:ubuntu安装php-7.2
  17. array_map 巧替 foreach
  18. Python数据分析工具库-Numpy 数组支持库(二)
  19. tomcat的server.xml配置及context配置直接引用工程
  20. Ruby(3):基本语法中

热门文章

  1. ES5数组扩展
  2. 关于Cadence OrCad 16.6的破解
  3. 【JZOJ1667】【BZOJ1801】【luoguP2051】中国象棋
  4. 阿里云在云栖大会发布SaaS加速器3.0版最新成果,让天下没有难做的SaaS
  5. 2019云栖大会开幕,5G边缘计算成首日焦点
  6. thinkphp DEFINED标签
  7. Django项目:堡垒机(Linux服务器主机管理系统)--03--03堡垒机在Linux系统里记录会话日志02/02
  8. golang中time包的使用
  9. 阿里P8架构师谈:数据库分库分表、读写分离的原理实现,使用场景
  10. POJ 2187 /// 凸包入门 旋转卡壳