- hibernate版本:5.0.7
- 要使用hibernate,首先下载hibernate,然后导入../lib/required下面的jar包,这个文件夹里的jar包都是必须要的jar包,如果要使用其他的,比如c3p0,就导入optional中有的c3p0jar包即可。
- 配置好两个配置文件,建立一个类Customer,数据库有张表cst_customer,类中的属性与表中字段对应(ORM),再引入日志文
件(log4j.properties),里面可以配置相关信息。
映射文件(类名.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>
<!-- 建立类与表的映射,如果类名与表名一样,可以省略table,catalog对应数据库名,可以不配 -->
<class name="hibernate_demo01.Customer" table="cst_customer" catalog="hibernate_day01">
<!-- 建立类中的属性与表中的主键的对应,name是类中属性名,column是表中字段名 -->
<id name="cust_id" column="cust_id">
<!-- 主键生成策略-->
<generator class="native"/>
</id>
<!-- 建立类中的属性与表中的字段的对应,
name是类中属性名,column是表中字段名
type是类型,比如字符串类型可以写:type="java.lang.String",
或hibernate中类型:type="string",
或数据库中的类型:去掉column,在property中写一个conlumn子标签,
里面写name="",sql-type="varchar"
-->
<property name="cust_name" column="cust_name" length="32" />
<property name="cust_source" column="cust_source" length="32"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/>
<property name="cust_phone" column="cust_phone"/>
<property name="cust_mobile" column="cust_mobile"/>
</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://localhost:3306/hibernate_day01?useSSL=true&useUnicode=true&characterEncoding=UTF-8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 三可选 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 自动建表
none:不使用hibernate的自动建表
create:如果已经有表,删除原有表,重新创建,如果没有,就创建一个
create-drop:如果已经有表,删除原有表,重新创建,如果没有,就创建一个,用完后在删除
update:如果有表,使用原有表,如果没有,创建表
validate:如果没有表,不会创建表,会验证折据库中原有表(验证映射和表结构)
-->
<property name="hibernate.hbm2ddl.auto"></property>
<!-- 引入映射文件 -->
<mapping resource="hibernate_demo01/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3.第一个小案例
package hibernate_demo01;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* hibernate入门案例
* @author LSD
*
*/
public class HibernateDemo01 {
public static void main(String[] args) {
//1.加载核心配置文件
Configuration configuration = new Configuration().configure();
//如果将hibernate.cfg.xml替换为hibernate.properties (hibernate.properties中无法配置映射文件)
//那么第一步就是:Configuration configuration = new Configuration();
//configuration.addResource("hibernate_demo01/Customer.hbm.xml");
//2.创建一个SessionFactory,类似JDBC中连接池
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.通过SessionFactory获取Session对象,类似JDBC中Connection
Session session = sessionFactory.openSession();
//4.手动开启事务
Transaction transaction = session.beginTransaction();
//5.编写代码
Customer customer = new Customer();
customer.setCust_name("某东");
session.save(customer);
//6.事务提交
transaction.commit();
}
}