Hibernate 搭建

1.什么是hibernate框架
hibernate框架是当今主流的Java持久层框架之一,由于他具有简单易学,灵活性强,扩展性强等特点,能够大大地简化程序的代码量,提高工资效率。因此受到广大开发人员的喜爱。
hibernate是一个开放源代码的ORM框架,他对JDBC进行了轻量级的对象封装,使得Java开发人员可以使用面向对象的编程思想来操作数据库
2.下载hibernate5
http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7Final/
解压看到以下文件夹
documentation:存放hibernate相关的文档,包括参考文档的API文档
lib:存放hibernate编译和运行所依赖的JAR包,其中REQUIRED子目录下包含了运行hibernate5项目所必需的JAR包
project:存放hibernate各种相关的源代码
3.创建数据库和表
create database hibernate_day01;
create table 'cst_customer';
4.引入JAR包
数据库驱动包mysql-connector-java-5.1.7-bin.jar
hibernate/lib/required/*.jar

5.创建实体

public class Customer {
	private Long cust_id;
	private String cust_name;
	private String cust_source;
	private String cust_industry;
	private String cust_level;
	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;
	}
	private String cust_phone;
	private String cust_mobile;
	
}
6.创建映射文件
在实体类所在的包中,创建一个名称为*.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="cn.itcast.domain.Customer" table="cst_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>
7.hibernate主配置
在项目的src下创建一个名称为*.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>
<!-- 数据库url -->
		<property 
name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
<!-- 数据库链接用户名 -->
		<property 
name="hibernate.connection.username">root</property>
<!-- 数据库链接密码 -->
		<property name="hibernate.connection.password"></property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.hbm2dd1.auto">update</property>	
		<mapping resource="cn/itcast/domain/Customer.hbm.xml"/>	
	</session-factory>
</hibernate-configuration>
8.编写测试代码
public class HibernateDemo1 {
	@Test
	public void demo1(){
		//加载配置文件
		Configuration  cfg=new Configuration().configure();
		//创建一个sessionfactory
		SessionFactory sessionFactory=cfg.buildSessionFactory();
		//创建session对象
		Session session=sessionFactory.openSession();
		//开启事务
		Transaction tx=session.beginTransaction();
		//执行相关操作
		Customer customer=new Customer();
		customer.setCust_name("小王");
		customer.setCust_source("网络推广");
		session.save(customer);
		//事务提交
		tx.commit();
		//释放资源
		session.close();
	}
}
9.工具类

public class HibernateUtils {
	private static SessionFactory sf;
	//静态代码块调用方法只执行一次类加载器调用
	static{
		//创建,调用空参构造
		Configuration conf=new Configuration().configure();
		//根据配置信息,创建sessionFactory对象
		sf=conf.buildSessionFactory();
	}
	//获得session=>获得全新session
	public static Session openSession(){
		//获得session
		Session session=sf.openSession();
		return session;
	}
	//获得session=》获得与线程绑定的session
	public static Session getCurrentSession(){
		Session session=sf.getCurrentSession();
		return session;
	}
	public static void main(String[] args){
		System.out.println(HibernateUtils.openSession());
	}
}
10.
CRM:客户关系管理系统customer ralation manager
创建web项目
导包(hibernate包,数据库驱动包,标签库包,BeanUtils包)
引入静态页面
搭建hibernate框架
思路分析
开发
测试 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值