数据库
CREATE TABLE customer
(cust_id
bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',cust_name
varchar(32) NOT NULL COMMENT '客户名称(公司名称)',cust_source
varchar(32) DEFAULT NULL COMMENT '客户信息来源',cust_industry
varchar(32) DEFAULT NULL COMMENT '客户所属行业',cust_level
varchar(32) DEFAULT NULL COMMENT '客户级别',cust_phone
varchar(64) DEFAULT NULL COMMENT '固定电话',cust_mobile
varchar(16) DEFAULT NULL COMMENT '移动电话',
PRIMARY KEY (cust_id
)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
数据库连接配置
<?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.autocommit">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123123</property>
<!-- 配置Hibernate的方言 后面要写mysql的版本号 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
<!-- 打印SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<!--参数 none 不使用hibernate自动建表 create如果数据库中已经有表,重新创建,如果没有新建,-->
<!--create-drop 如果数据库中已经有表,重新创建,如果没有新建,使用完了删除该表-->
<!--update如果数据库有表使用原表,没有表创建新表 validate效验表和映射结构是否一致,不一致就会报错-->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/myxq/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Customer.java
package com.myxq.domain;
import lombok.Data;
@Data
public class Customer {
private Long cust_id;
private String cust_name;
private String cust_source;
private String cust_industry;
private String cust_level;
private String cust_phone;
private String cust_mobile;
}
数据库映射,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="com.myxq.domain.Customer" table="customer">
<!--哪一个是主键-->
<id name="cust_id" column="cust_id">
<!--主键的生成策略-->
<generator class="native"/>
</id>
<!--建立类中普通属性和数据库字段当中的数据关联-->
<property name="cust_name" column="cust_name" />
<property name="cust_source" column="cust_source"/>
<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>
测试类
package com.myxq.test;
import com.myxq.domain.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateTest {
@Test
public void test1(){
//加载配置文件,如果不传参的话默认是传这个参数
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
//创建seessionFactory jdbc连接池
SessionFactory sessionFactory=configure.buildSessionFactory();
//获取session --连接对象
Session session=sessionFactory.openSession();
Customer customer=new Customer();
customer.setCust_name("myxq");
customer.setCust_level("2");
//保存
session.save(customer);
//释放资源
session.close();
sessionFactory.close();
}
}
访问流程: