1.Hibernate的基本使用方式
1.Hibernate是一个对象关系映射框架
2.Hibernate的命名规范(尽量使用 类名.hbm.xml的命名规范)
3.建立实体类与表的映射
Customer.java
package com.wuhaitao.hibernate.demo1;
/**
* CREATE TABLE `cst_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;
* @author wuhaitao
*
*/
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;
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 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_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;
}
}
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.wuhaitao.hibernate.demo1.Customer" table="cst_customer">
<!-- id属性是建立于表的主键映射 -->
<id name="cust_id" column="cust_id">
<generator class="native"></generator>
</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>
4.创建核心配置文件hibernate.cfg.xml
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.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--为了方便调试是否在运行hibernate时在日志中输出sql语句 -->
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<!-- 数据库连接url ,由于用的Mysql8 必须配置时区和SSl,并且驱动也改了-->
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01?useSSL=false&serverTimezone=UTC</property>
<!-- 数据库用户名和密码 -->
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- Hibernate 方言 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否对日志中输出的sql语句进行格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 引入映射文件 -->
<mapping resource="com/wuhaitao/hibernate/demo1/customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
5.在核心配置文件中引入映射文件
- 案例测试
HibernateDemo1.java
package com.wuhaitao.hibernate.demo1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class HibernateDemo1 {
@Test
public void test() {
//1.加载核心配置文件
Configuration configuration = new Configuration().configure();
//2.创建SessionFactory对象
SessionFactory sessionFactory = configuration.buildSessionFactory();
//3.得到session对象
Session session = sessionFactory.openSession();
//4.手动开启事务
Transaction beginTransaction = session.beginTransaction();
//5.编写代码
Customer customer = new Customer();
customer.setCust_name("马云");
session.save(customer);
//6.提交事务
beginTransaction.commit();
//7.释放资源
session.close();
}
}
结果
结果
网友评论