一、ORM技术
ORM:Obejict Relation Mapping,对象关系映射。ORM技术可以建立对象与数据库之间的关系。它可以通过对象来访问数据库。目前市场有:Hibernate、MyBateis、TopLink、EJB等等
二、Hibernate框架
Hibernate是目前市场上最流行的ORM框架之一。
它的优点
1)Hibernate使用面向对象的方式来访问数据库;
2)Hibernate的查询性能更好;
3)Hibernate可以在不同的数据库之间进行切换,因此可移植性更强;
在实际使用中,Hibernate是属于Dao层技术。
2.1Hibernate的下载和安装
Hibernate的官网:http://www.sourceforge.net
下载完成后直接压缩。它的目录结果:
documention:保存Hibernate的API文档;
lib:保存Hibernate相关的jar包
project:保存Hibernate项目的源文件
2.2第一个Hibernate例子
第一步:导入jar包
把lib/required目录下所有jar包复制到工程中。除此以外,还要把数据库的驱动包复制到工程中。
第二步:创建表
1 CREATE TABLE `cst_customer` ( 2 ??`cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘, 3 ??`cust_name` VARCHAR(32) NOT NULL COMMENT ‘客户名称(公司名称)‘, 4 ??`cust_source` VARCHAR(32) DEFAULT NULL COMMENT ‘客户信息来源‘, 5 ??`cust_industry` VARCHAR(32) DEFAULT NULL COMMENT ‘客户所属行业‘, 6 ??`cust_level` VARCHAR(32) DEFAULT NULL COMMENT ‘客户级别‘, 7 ??`cust_address` VARCHAR(128) DEFAULT NULL COMMENT ‘客户联系地址‘, 8 ??`cust_phone` VARCHAR(64) DEFAULT NULL COMMENT ‘客户联系电话‘, 9 ??PRIMARY KEY (`cust_id`)10 ) ENGINE=INNODB DEFAULT CHARSET=utf8;
第三步:创建实体类
实体类就是符合JavaBeen的规范的类
编写JavaBeen的规范
1)把成员属性私有化;
2)为每个成员属性提供setter和getter方法
3)提供无参的构造函数;
4)建议实现Serializable接口;
public class Customer implements Serializable { ???????private Integer custId;//客户编号 ???private String custName;//客户名称 ???private String source;//客户信息的来源 ???private String industry;//客户所在的行业; ???private String level;//客户的等级 ???private String address;//联系地址 ???private String phone;//联系电话 ???public Integer getCustId() { ???????return custId; ???} ???public void setCustId(Integer custId) { ???????this.custId = custId; ???} ???public String getCustName() { ???????return custName; ???} ???public void setCustName(String custName) { ???????this.custName = custName; ???} ???public String getSource() { ???????return source; ???} ???public void setSource(String source) { ???????this.source = source; ???} ???public String getIndustry() { ???????return industry; ???} ???public void setIndustry(String industry) { ???????this.industry = industry; ???} ???public String getLevel() { ???????return level; ???} ???public void setLevel(String level) { ???????this.level = level; ???} ???public String getAddress() { ???????return address; ???} ???public void setAddress(String address) { ???????this.address = address; ???} ???public String getPhone() { ???????return phone; ???} ???public void setPhone(String phone) { ???????this.phone = phone; ???}}
第四步:配置实体类与数据库表的映射关系
在实体类所在包下新建一个Customer.hbm.xml的映射文件
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 <!-- package属性:指定实体类的包名 --> 6 <hibernate-mapping package="com.enter.dome"> 7 ??<!-- class用来配置实体类与数据库表的映射关系 ?8 ?????name:类名 9 ?????table:实体类所对应的表10 ??-->11 ??<class name="Customer" table="cst_customer">12 ????<!-- 配置实体类的ID与主键的映射关系13 ????????name:实体类的属性名14 ????????column:表的子段名15 ?????-->16 ?????<id name="custId" column="cust_id">17 ???????<!-- 主键的自增方式 -->18 ???????<generator class="identity"/>19 ?????</id>20 ?????<!-- 配置实体类成员属性与字段的关系 -->21 ?????<property name="custName" column="cust_name"/>22 ?????<property name="source" column="cust_source"/>23 ?????<property name="industry" column="cust_industry"/>24 ?????<property name="level" column="cust_level"/>25 ?????<property name="address" column="cust_address"/>26 ?????<property name="phone" column="cust_phone"/>27 ??</class>28 29 </hibernate-mapping>
第五步:创建Hibernate核心配置文件
把hibernate/project/etc目录下的hibernate.cfg.xml拷贝项目的src目录下。
然后修改文件,设置hibernate的配置信息。详细的配置信息可以参考hibernate.properties文件中的配置。
1 <!-- 2 ??~ Hibernate, Relational Persistence for Idiomatic Java 3 ??~ 4 ??~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 5 ??~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 6 ??--> 7 <!DOCTYPE hibernate-configuration PUBLIC 8 ????"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 9 ????"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">10 11 <hibernate-configuration>12 ????<session-factory>13 ?????????<!-- 配置数据库的方言 -->14 ?????????<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>15 ?????????<!-- 配置数据库的驱动 -->16 ?????????<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>17 ?????????<!-- 数据库驱动 -->18 ?????????<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jun1</property>19 ?????????<!-- 用户名 -->20 ?????????<property name="hibernate.connection.username">root</property>21 ?????????<!-- 密码 -->22 ?????????<property name="hibernate.connection.password">entor</property>23 ?????????<!-- 是否显示SQL语句 -->24 ????????<property name="show_sql">true</property>25 ????????<!-- 指定映射文件的位置 26 ????????<mapping resource="com/entor/doction/Customer.hbm.xml"/>-->27 ????????<mapping class="com.enter.dome.Customer"/>28 ????</session-factory>29 </hibernate-configuration>
第六步:使用Hibernate提供的API访问数据库
Session:提供了一些访问数据库的方法。例如:save()、update()、delete() 、get()等等。每次访问数据库都应该创建一个Session。
SessionFactory:Session的工厂类,它负责产生Session对象。当服务器启动的时候创建该对象即可。也就是说,该只需要创建一次。
Configuration:代表Hibernate的配置。它的作用:1)加载配置文件;2)创建SessionFactory对象
1 public class HelloWorld { 2 ????public static void main(String[] args) { 3 ????????Configuration configuration=new Configuration(); 4 ????????configuration.configure(); 5 ????????SessionFactory factory=configuration.buildSessionFactory(); 6 ????????Session session=factory.openSession(); 7 ????????Customer customer=new Customer(); 8 ????????//Transaction tx=session.beginTransaction(); 9 ????????10 ????????customer.setCustName("陈骏金111");11 ????????session.save(customer);12 ????????//tx.commit();13 ????????session.close();14 ????????/*Transaction tx=session.beginTransaction();15 ????????customer.setCustId(1);16 ????????customer.setCustName("陈骏金11");17 ????????session.update(customer);18 ????????tx.commit();19 ????????session.close();*/20 ????}21 22 }
Hibernate框架的讲解
原文地址:https://www.cnblogs.com/chenjunjin/p/8536024.html