什么是框架?
答:可以完成一部分功能的半成品项目
JavaEE三层架构
Web
Service
Dao
5种框架:
SSH: Spring + Struts2 + Hibernate
SSM: Spring + SpringMVC + MyBatis
hibernate是一款orm框架
orm:object relation mapping. 对象关系映射
orm分四级:dbutils属于1级,mybatis属于2级,hibernate属于4级:完全面向对象操作数据库
hibernate框架的搭建
1.导包
2.创建数据库,准备表写实体类
3.书写orm元数据(对象与表的映射配置文件)
导入约束,映射配置文件
4.书写主配置文件
5.写入代码。
Customer.hbm.xml(ORM元数据)的配置详解
<?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>
<!-- beans包下的实体类名字Customer table是表的名字-->
<class name="beans.Customer" table="cst_customer">
<!-- Customer主键 ,必须有主键,当属性名和字段名一样时可以省略
【type】:类型,字段类型,可以有三种写法
java类型:java.lang.Long
数据库类型:bigint
Hibernate类型:long/string/int
不填也可以自动识别
【length】:设置长度,不填默认最长
-->
<id name="cust_id" column="cust_id">
<!-- generator:主键生成策略 -明天具体详解 -->
<generator class="native"></generator>
</id>
<!-- name是实体类的字段,column是表的属性 -->
<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_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主配置文件 src/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>
<!--
五个必填 连接数据库的 hibernate基本信息
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.2.10/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--
#hibernate.show_sql true
显示生成的sql查询语句
#hibernate.format_sql true
格式化sql语句,可读性强
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!--
## auto schema export
自动导出结构
#hibernate.hbm2ddl.auto create-drop -每次操作都是创建新表,操作完再删除表
#hibernate.hbm2ddl.auto create -每次执行都是创建新表,有原表就删除
以上选项不太用
#hibernate.hbm2ddl.auto update -表存在操作,不存在创建一个表,如果表结构改变也会更新
#hibernate.hbm2ddl.auto validate -只在表存在且结构符合时使用
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 引入映射文件,使用相对路径 元数据
resource:从类路径开始
-->
<mapping resource="beans/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate API详解
增删改查