hibernate框架:
框架:
1. 框架是用来提高开发效率的。
2. 封装了好了一些功能。我们需要使用这些功能时,调用即可.不需要再手动实现。
3. 框架可以理解成是一个半成品的项目。只要懂得如何驾驭这些功能即可。
hibernate框架:
hibernate是一款orm框架:
orm:object relationg mapping. 对象关系映射
orm分4级:
hibernate属于4级:完全面向对象操作数据库
mybatis属于2级
dbutils属于1级
用途:
操作数据库的时候,可以以面向对象的方式来完成.不需要书写SQL语句
hibernate框架的搭建:
1.导包
2.创建数据库,准备表,实体:
3.书写orm元数据(对象与表的映射配置文件):
导入约束:
在Myeclipse中,window-->prefences-->files and editors-->xml-->xml files-->xml catalog中,选择add。
其中,对于Key type,Public ID和URI任选一个。下图中所示内容在dtd文件中开始的地方。
实体:
//Customer.java
package domain; 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, cust_industry, cust_level, cust_linkman, cust_phone, 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; } }
orm元数据:
4.书写主配置文件:
配置Customer.hbm.xml和hibernate.cfg.xml(hibernate.cfg.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="domain.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_user_id" column="cust_user_id"></property> <property name="cust_create_id" column="cust_create_id"></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_linkman" column="cust_linkman"></property> <property name="cust_phone" column="cust_phone"></property> <property name="cust_mobile" column="cust_mobile"></property> </class> </hibernate-mapping>
<!--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:///crm</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 非必选项 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="domain/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
hibernate.cfg.xml中的标签可以在名为hibernate.properties的文件中找到,有很多配置信息。
5.书写代码测试(待续)
新建一个java web项目,文件结构如下图。