Hibernate框架_搭建第一个Hibernate框架
一、eclipse搭建
A.创建动态web项目
New-->Dynamic web project(web project)
B.导入jar包
1.数据库驱动包
2.hibernate开发必须jar包(去官网下载hibernate,解压后里面lib-->required,里面jar包是hibernate必须jar包)
3.日志记录包
C.创建表(也可以不创建)
D.创建实体类(数据库中如一个客户表就是客户实体,用Java语言描述出来)
- public class Customer {
- private Long cust_id;
- private String cust_name;
- private String cust_source;
- private String cust_industry;
- private String cust_level;
- 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 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_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;
- }
- @Override
- public String toString() {
- return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
- + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
- + ", cust_mobile=" + cust_mobile + "]";
- }
- }
E.根据实体创建映射文件(统一一下文件命名规范:类名.hbm.xml。文件位置一般和实体类放在一起)
- 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.xxx.pojo.Customer" table="cst_customer">
- <id name="cust_id" column="cust_id">
<!-- 主键生成策略 -->- <generator class="native"></generator>
- </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_phone" column="cust_phone"></property>
- <property name="cust_mobile" column="cust_mobile"></property>
- </class>
- </hibernate-mapping>
标签及其属性所代表的含义:
- class标签用来建立类和表的关系映射
- *name 表示该类的全路径(带包路径)
- *table 与该类映射的表名,如果表名和类名一致,表名可以省略不写
- *catalog:数据库名称,可以省略
- id标签用来建立类中属性域表中主键字段的对应
- *name 类中的属性名
- *column 表中的字段名,如果类中的属性名和表中的字段名一致,可以省略column
- *length:字段的长度,用于hibernate帮你建表时候指定表中字段的长度,默认为sql类型中长度最大值
- *type:类型,有三种写法:
- java数据类型:如java.lang.String
- hibernate类型: string (type属性默认值)
- sql类型: 如varchar
对于type扩展:
- property标签:用来建立表中类的普通属性域表中普通字段的对应
- 这里面也同样有四个属性,name,column,length,type,和上面id标签用法一样,只是多了not-null,unique,可以设置属性非空或者唯一
F.创建hibernate配置文件。(该文件命名为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://localhost:3306/hibernate_crm?useUnicode=true&characterEncoding=UTF8</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
- <!-- 配置hibernate方言 -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <!-- hibernate显示发送sql语句 -->
- <property name="hibernate.show_sql">true</property>
- <!-- hibernate格式化sql语句 -->
- <property name="hibernate.format_sql">true</property>
- <!-- 数据定义语言,主要是对表的操作 -->
- <property name="hibernate.hbm2ddl.auto">update</property>
- <!-- hibernate映射文件加载 -->
- <mapping resource="com/xxx/pojo/customer.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
对于hibernate.hbm2dd.autol属性,有四个值
- none:不用hibernate自动生成表
- create:每次运行hibernate都会创建一个新的表
- create-drop:每次创建都会创建一个新的表,执行程序结束后删除这个表
- update:如果数据库中有该表,使用数据库中原有的表,如果没有,hibernate会帮你创建一个新表,可以更新表的结构
- validate:只会使用原有的表。对映射关系进行校验
G.编写测试类
- /**
- * 插入数据
- * */
- @Test
- public void demo1() {
- // 加载配置文件
- Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
- // 根据配置文件获取session工厂对象
- SessionFactory sessionFactory = cfg.buildSessionFactory();
- // 从session工厂里面获取session对象
- Session session = sessionFactory.openSession();
- // 开启事务
- Transaction tx = session.beginTransaction();
- // 保存更新数据
- Customer cust = new Customer();
- cust.setCust_name("张三");
- cust.setCust_source("介绍");
- cust.setCust_phone("13085469875");
- Serializable uid = session.save(cust);
- System.out.println(uid);
- // 提交事务
- tx.commit();
- // 关闭资源
- session.close();
- }
至此,eclipse手动搭建的hibernate框架就创建好了。
二.MyEclipse自动创建hibernate框架
A.创建web项目
B.增加hibernate框架
右键项目名称 选择Configure facet--->install hibernate facet(安装hibernate方面)
接下来直接finish。
c.生成hibernate映射文件(其实主要就是开启myeclipse数据库,利用数据库反转生成pojo)
https://blog.csdn.net/tuke_tuke/article/details/49803293
myeclipse创建hibernate就大致这样
Hibernate框架_搭建第一个Hibernate框架的更多相关文章
- 2015年3月26日 - Javascript MVC 框架DerbyJS DerbyJS 是一个 MVC 框架,帮助编写实时,交互的应用。
2015年3月26日 - Javascript MVC 框架DerbyJS DerbyJS 是一个 MVC 框架,帮助编写实时,交互的应用.
- 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序
1.直接通过JDBC API持久化实体域对象: A.java.sql常用接口和类: DriverManager:驱动程序管理器,负责创建数据库连接. Connection:代表数据库连接. State ...
- 分享知识-快乐自己:搭建第一个 Hibernate (Demo)
使用 Hibernate 完成持久化操作 七大 步骤: 1.读取并解析配置文件及映射文件: Configuration configuration=new Configuration().config ...
- Spring框架学习-搭建第一个Spring项目
步骤一:下载Spring开发包. 官网:https://spring.io/ 下载地址:https://repo.spring.io/libs-release-local/org/ ...
- Android Afinal框架学习(二) FinalActivity 一个IOC框架
框架地址:https://github.com/yangfuhai/afinal 相应的源代码: net.tsz.afinal.annotation.view.* FinalActivity Fina ...
- 【Hibernate】hibernate框架的搭建
1, Hibernate 是什么 Hibernate是java应用程序与数据库交互的开发的框架. Hibernate是一个开源,轻量级的ORM(对象关系映射)工具. 2,Hibernate框架的优点 ...
- Selenium自动化测试框架的搭建
说 起自动化测试,我想大家都会有个疑问,要不要做自动化测试? 自动化测试给我们带来的收益是否会超出在建设时所投入的成本,这个嘛别说是我,即便是高手也很难回答,自动化测试的初衷是美好的,而测 ...
- 一个小框架,基于rx_retrofit2_mvp
离职在即,也没什么事情做,就鼓捣了一下.任意搭建了一个小框架,看看以后能不能搞出自己的一个model,好了.不说别的,上代码 1,先上依赖库 compile 'io.reactivex:rxandro ...
- python webdriver 从无到有搭建混合驱动自动化测试框架的过程和总结
一步一步实现混合驱动自动化测试框架的搭建 混合驱动自动化测试框架,是一个非常高级的框架,非常好用,但也很难,不好掌握,需要多练习,就像搭建数据驱动框架一样,需要自己去一点一点的写,一边搭建一边做思路整 ...
随机推荐
- 5G+边缘计算,着眼可见的未来
在 2019 年 2 月巴塞罗那举办的 MWC(世界移动通讯大会)上,华为手机带来了一款超薄的 5G 折叠屏手机 Mate X.这款手机将折叠屏和 5G 结合在一起,引起了不少人的关注与舆论,而昂贵的 ...
- "元素隐式具有 “any” 类型,因为类型“Shared”没有索引签名"问题解决思路
最近在构建一个typescript项目时如下代码在项目框架里vscode报错元素隐式具有 "any" 类型,因为类型“Shared”没有索引签名;很有意思的是当我们单独的把这段代码 ...
- Java集合详解8:Java集合类细节精讲
今天我们来探索一下Java集合类中的一些技术细节.主要是对一些比较容易被遗漏和误解的知识点做一些讲解和补充.可能不全面,还请谅解. 本文参考:http://cmsblogs.com/?cat=5 具体 ...
- 解决SpannableString在Android组件间传递时显示失效的问题
问题:在A activity中传递一个SpannableString到B activity中,并最终传递到B activity中的TextView中,但是没有展示出Span效果. 解决:阅读TextV ...
- 邀您参加 | BigData & Alluxio 交流会-成都站
4月27日,在天府之国,与你共享大数据与Alluxio的技术魅力. 活动介绍 本期技术沙龙将会聚焦在大数据.存储.数据库以及Alluxio应用实践等领域,邀请腾讯技术专家和业界技术专家现场分享关于Al ...
- C#处理json实战
昨天看到技术群中发了一个查询天气的api,http://www.sojson.com/open/api/weather/json.shtml?city=南昌 点进去看,发现服务器传回来一个天气信息的j ...
- 【开发记录】如何在B/S项目中使用中国天气的实时天气功能
好久没有更新我的博客了,正好手头有一个比较合适的项目经验可以分享出来,就是这个如何使用中国天气的天气预报功能,也正好做个项目经验记录. 功能需求 这个功能需求比较简单,就是想在网页端显示实时天气数据. ...
- Flutter 异常处理之图片篇
背景 说到异常处理,你可能直接会认为不就是 try-catch 的事情,至于写一篇文章单独来说明吗? 如果你是这么想的,那么本篇说不定会给你惊喜哦~ 而且本篇聚焦在图片的异常处理. 场景 学以致用,有 ...
- gittalk报错Error
最近通过github和jekyll搭了一个博客,申请使用了gittalk的评论. 但是博客的页面一直报Error:Not found,如下 发现是gittalk中的信息填写错了,name随便写:Hom ...
- 数据库【mongodb】之pymongo
一个Python操作mongodb的模块 # coding=utf-8 from pymongo import MongoClient #实例化client,建立连接 client = MongoCl ...