(01)hibernate框架环境搭建及测试

---恢复内容开始---

1.创建javaweb项目

2.导包

hibernate包 hibernatelib equired*.jar

数据库驱动包 mysql-connector-java-5.1.7-bin.jar

标签库包 jstl-1.2.jar standard.jar

BeanUtils包 commons-beanutils-1.8.3.jar  commons-logging-1.1.1.jar

3.引入静态页面

4.搭建hibernate框架

一.书写orm元数据(对象与表的映射配置文件)

1)导入约束 hibernate-configuration-3.0.dtd  hibernate-mapping-3.0.dtd

  1、window--->Preferences--->XML--->XML Catalog--->User Specified Entries窗口,点击Add按钮

  2.在Add XML Catalog Entry 对话框中选择或输入以下内容: 

        Location: 路径
        Key Type: URI 
        KEY: http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd    http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd



2)实体

1     private Long cust_id;
2     
3     private String cust_name;
4     private String cust_source;
5     private String cust_industry;
6     private String cust_level;
7     private String cust_linkman;
8     private String cust_phone;
9     private String cust_mobile;
Customer

3)orm元数据

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5    <!-- 配置表与实体对象的关系 -->
 6    <!-- package属性:填写一个包名.在元素内部凡是需要书写完整类名的属性,可以直接写简答类名了. -->
 7 <hibernate-mapping package="cn.itheima.domain" >
 8     <!-- 
 9         class元素: 配置实体与表的对应关系的
10             name: 完整类名
11             table:数据库表名
12      -->
13     <class name="Customer" table="cst_customer" >
14         <!-- id元素:配置主键映射的属性
15                 name: 填写主键对应属性名
16                 column(可选): 填写表中的主键列名.默认值:列名会默认使用属性名
17                 type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
18                         每个类型有三种填法: java类型|hibernate类型|数据库类型
19                 not-null(可选):配置该属性(列)是否不能为空. 默认值:false
20                 length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
21          -->
22         <id name="cust_id"  >
23             <!-- generator:主键生成策略(明天讲) -->
24             <generator class="native"></generator>
25         </id>
26         <!-- property元素:除id之外的普通属性映射
27                 name: 填写属性名
28                 column(可选): 填写列名
29                 type(可选):填写列(属性)的类型.hibernate会自动检测实体的属性类型.
30                         每个类型有三种填法: java类型|hibernate类型|数据库类型
31                 not-null(可选):配置该属性(列)是否不能为空. 默认值:false
32                 length(可选):配置数据库中列的长度. 默认值:使用数据库类型的最大长度
33          -->
34         <property name="cust_name" column="cust_name" >
35             <!--  <column name="cust_name" sql-type="varchar" ></column> -->
36         </property>
37         <property name="cust_source" column="cust_source" ></property>
38         <property name="cust_industry" column="cust_industry" ></property>
39         <property name="cust_level" column="cust_level" ></property>
40         <property name="cust_linkman" column="cust_linkman" ></property>
41         <property name="cust_phone" column="cust_phone" ></property>
42         <property name="cust_mobile" column="cust_mobile" ></property>
43     </class>
44 </hibernate-mapping>
Customer.hbm.xml

二.书写主配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7     
 8         <!-- 
 9         #hibernate.dialect org.hibernate.dialect.MySQLDialect
10         #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
11         #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
12         #hibernate.connection.driver_class com.mysql.jdbc.Driver
13         #hibernate.connection.url jdbc:mysql:///test
14         #hibernate.connection.username gavin
15         #hibernate.connection.password
16          -->
17          <!-- 数据库驱动 -->
18         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
19          <!-- 数据库url -->
20         <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
21          <!-- 数据库连接用户名 -->
22         <property name="hibernate.connection.username">root</property>
23          <!-- 数据库连接密码 -->
24         <property name="hibernate.connection.password">root</property>
25         <!-- 数据库方言
26             不同的数据库中,sql语法略有区别. 指定方言可以让hibernate框架在生成sql语句时.针对数据库的方言生成.
27             sql99标准: DDL 定义语言  库表的增删改查
28                       DCL 控制语言  事务 权限
29                       DML 操纵语言  增删改查
30             注意: MYSQL在选择方言时,请选择最短的方言.
31          -->
32         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
33         
34         
35         <!-- #hibernate.show_sql true 
36              #hibernate.format_sql true
37         -->
38         <!-- 将hibernate生成的sql语句打印到控制台 -->
39         <property name="hibernate.show_sql">true</property>
40         <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
41         <property name="hibernate.format_sql">true</property>
42         <!-- 
43         ## auto schema export  自动导出表结构. 自动建表
44         #hibernate.hbm2ddl.auto create        自动建表.每次框架运行都会创建新的表.以前表将会被覆盖,表数据会丢失.(开发环境中测试使用)
45         #hibernate.hbm2ddl.auto create-drop 自动建表.每次框架运行结束都会将所有表删除.(开发环境中测试使用)
46         #hibernate.hbm2ddl.auto update(推荐使用) 自动生成表.如果已经存在不会再生成.如果表有变动.自动更新表(不会删除任何数据).
47         #hibernate.hbm2ddl.auto validate    校验.不自动生成表.每次启动会校验数据库中表是否正确.校验失败.
48          -->
49         <property name="hibernate.hbm2ddl.auto">update</property>
50         <!-- 引入orm元数据
51             路径书写: 填写src下的路径
52          -->
53         <mapping resource="cn/itheima/domain/Customer.hbm.xml" />
54         
55     </session-factory>
56 </hibernate-configuration>
hibernate.cfg.xml

三.书写代码测试

 1 //测试Hibernate框架
 2 public class Demo {
 3 
 4     @Test
 5     //保存客户
 6     public void fun1(){
 7         Configuration conf = new Configuration().configure();
 8         
 9         SessionFactory sessionFactory = conf.buildSessionFactory();
10         
11         Session session = sessionFactory.openSession();
12     
13         Transaction tx = session.beginTransaction();
14         //----------------------------------------------
15         Customer c = new Customer();
16         c.setCust_name("google公司");
17         
18         session.save(c);//执行保存
19         
20         //----------------------------------------------
21         tx.commit();
22         session.close();
23         sessionFactory.close();
24     }
25 }
Test
-------------------------------------------------------------------------
## 极客时间全网最便宜最优惠购买方式,优惠券返现 百度网盘 微信关注公众号“选门好课”
扫描下方二维码关注我的公众号"选门好课",与我一起交流知识
原文地址:https://www.cnblogs.com/singworld/p/8886977.html