Hibernate框架介绍以及入门 【一】Hibernate 快速入门 框架的概述 什么是 Hibernate Hibernate 持久层的ORM框架 下载Hibernate 官网 编写测试代码

5、创建实体类Customer

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;

}

public Customer() {

// TODO Auto-generated constructor stub

}

public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,

String cust_phone, String cust_mobile) {

super();

this.cust_id = cust_id;

this.cust_name = cust_name;

this.cust_source = cust_source;

this.cust_industry = cust_industry;

this.cust_level = cust_level;

this.cust_phone = cust_phone;

this.cust_mobile = cust_mobile;

}

@Override

public String toString() {

return “Customer [cust_id=” + cust_id + “, cust_name=” + cust_name + “, cust_source=” + cust_source

  • “, cust_industry=” + cust_industry + “, cust_level=” + cust_level + “, cust_phone=” + cust_phone

  • “, cust_mobile=” + cust_mobile + “]”;

}

}

6、创建映射

映射需要通过XML配置文件来完成,这个配置文件可以任意命名。尽量统一命名规范(类名.hbm.xml)

复制该头,引入jar当中的配置

在这里插入图片描述

在这里插入图片描述

7、创建Customer.hbm.xml文件*
<?xml version="1.0" encoding="UTF-8"?>

<class name=“com.itzheng.hibernate.demo01.Customer”

table=“cst_customer”>

8、创建hibernate的核心配置文件*

hibernate核心配置文件的名称:hibernate.cfg.xml

在这里插入图片描述

在这里插入图片描述

创建hibernate.cfg.xml文件

<?xml version="1.0" encoding="UTF-8"?>

com.mysql.jdbc.Driver

jdbc:mysql:///hibernate_day01

root

root

org.hibernate.dialect.MySQLDialect

true

true

配置文件与hibernate提供资料的联系

在这里插入图片描述

9、编写测试代码

在这里可能会遇到jdk新版本和hibernate不兼容的问题

手动加入这些依赖Jar包

要解决这个问题,我导入了下面这四个Jar包修复成功。

javax.activation-1.2.0.jar

jaxb-api-2.3.0.jar

jaxb-core-2.3.0.jar

jaxb-impl-2.3.0.jar

public class HibernateDemo01 {

@Test

// 保存客户的案例

public void demo01() {

// 1、加载Hibernate核心配置文件

Configuration configuration = new Configuration().configure();// 该方法加载了Customer.hbm.xml

// 2、创建SessionFactory对象:类似于JDBC中的链接池

SessionFactory sessionFactory = configuration.buildSessionFactory();// sessionFactory工厂

// 3、通过SessionFactory获取到Session对象:类似于我们JDBC中的Connection

Session session = sessionFactory.openSession();// Hibernate以及和mysql数据库建立起链接

// 4、手动开启事务:

Transaction transaction = session.beginTransaction();

// 5、编写代码

Customer customer = new Customer();

customer.setCust_name(“张三”);

session.save(customer);

// 6、事务提交

transaction.commit();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值