作者:叁念
本hibernate学习笔记教程所有资源百度云下载:链接:https://pan.baidu.com/s/1emXOxGbEzusjzPqJjLjMSg 密码:ulk1
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_linkman` varchar(64) 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;
注意:持久化(实体)类中必须有一个默认的构造器,即空构造器
package com.sannian.bean;
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_linkman;
private String cust_phone;
private String cust_mobile;
public Customer() {
super();
}
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_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
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内容如下【编写过程中如果出现不能自动提示的情况(即alt +/不能提示),请按照这篇文章进行操作:关于hibernate书写xml文件时不能提示的解决方案】:
<hibernate-mapping package="">
<class name="com.sannian.bean.Customer" table="cst_customer">
<id name="cust_id">
<generator class="native">generator>
id>
<property name="cust_name">property>
<property name="cust_source">property>
<property name="cust_industry">property>
<property name="cust_level">property>
<property name="cust_linkman">property>
<property name="cust_phone">property>
<property name="cust_mobile">property>
class>
hibernate-mapping>
Customer.hbm.xml详解:
<hibernate-mapping package="">
<class name="" table="">
<id name="" column="">
<generator class="native">generator>
id>
<property name="cust_name">property>
class>
hibernate-mapping>
hibernate.cfg.xml内容如下(如出现不能提示请参考上文):
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///Hibernateproperty>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">6666property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<mapping resource="com/sannian/bean/Customer.hbm.xml"/>
session-factory>
hibernate-configuration>
hibernate.cfg.xml详解
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
<property name="hibernate.connection.url">jdbc:mysql:///Hibernateproperty>
<property name="hibernate.connection.username">rootproperty>
<property name="hibernate.connection.password">183686property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
<property name="hibernate.show_sql">trueproperty>
<property name="hibernate.format_sql">trueproperty>
<property name="hibernate.hbm2ddl.auto">updateproperty>
<mapping resource="com/sannian/bean/Customer.hbm.xml"/>
session-factory>
hibernate-configuration>
Hibernate的执行流程,代码如下( 注意:在hibernate中事务不是自动提交的):
Test 内容如下:
package com.sannian.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.sannian.bean.Customer;
public class Test {
public static void main(String[] args) {
// 保存一个数据到数据库
Configuration conf = new Configuration().configure();
SessionFactory sessionFactory = conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// -------------------------------------------
Customer customer = new Customer();
customer.setCust_name("sannian");
session.save(customer);
// -------------------------------------------
transaction.commit();
session.close();
sessionFactory.close();
}
}
控制台信息如下:
数据库信息如下:
至此,你已经学会了如何初步使用hibernate
Configuration conf = new Configuration().configure();
SessionFactory sessionFactory = conf.buildSessionFactory();
Configuration conf = new Configuration().configure();
//根据文件hibernate.cfg.xml生成sessionFactory对象
SessionFactory sessionFactory = conf.buildSessionFactory();
//打开一个新的session
Session openSession = sessionFactory.openSession();
//获得一个与线程绑定的session[了解]
Session currentSession = sessionFactory.getCurrentSession();
public class SessionTransactionDemo {
/**
* session对象 功能: 表达hibernate与数据库之间的一次会话 类似于JDBC中的Connection
* session是hibernate中的核心对象
*/
public static void main(String[] args) {
// 1.创建配置对象
Configuration conf = new Configuration().configure();
// 2.session工厂
SessionFactory sessionFactory = conf.buildSessionFactory();
// 3.获得session
Session session = sessionFactory.openSession();
// 4.获得事务
// session.getTransaction();//获得一个事务对象
Transaction transaction = session.beginTransaction();// 获得并启动一个事务(推荐)
// --------------------------
//1.增
Customer customer = new Customer();
customer.setCust_name("yujie");
session.save(customer);
//2.查
Customer customer1 = session.load(Customer.class, 2l);
System.out.println(customer1.getCust_name());
//3.改
Customer customer2 = session.load(Customer.class, 2l);
customer2.setCust_name("temp");
session.update(customer2);
//4.删
Customer customer3 = session.load(Customer.class, 2l);
session.delete(customer3);
transaction.commit(); // 事务提交
// transaction.rollback(); // 事务回滚
session.close();
sessionFactory.close();
}
}