hibernate_day01_notes

   第1章 Hibernate_day01笔记

1.1 Hibernate框架的学习路线

 第一天:Hibernate的入门(Hibernate的环境搭建、Hibernate的API、Hibernate的CRUD)
 第二天:Hibernate的一级缓存、其他的API
 第三天:Hibernate的一对多配置、Hibernate的多对多的配置
 第四天:Hibernate的查询方式、抓取策略

1.2 CRM的案例
  1.2.1 CRM的概述(了解)
       1.2.1.1 什么是CRM

  CRM(Customer Relationship Management),即客户关系管理。 (#####就是利用互联网等一些技术协调企业和用户之间的交互[交互不陌生把…第一次跟bibi学做项目时候老师经常提到.]向客户提供新型的服务,实现对客户活动的全面管理######)

       1.2.1.2 CRM有哪些模块

在这里插入图片描述

1.3 Hibernate的框架的概述
   1.3.1 框架的概述
        1.3.1.1 什么是框架

       框架:指的是软件的半成品,已经完成了部分功能。

  1.3.2 EE的三层架构
        1.3.2.1 EE的经典三层结构

在这里插入图片描述

  1.3.3 Hibernate的概述
       1.3.3.1 什么是Hibernate

在这里插入图片描述

       1.3.3.2 什么是ORM

       ORM:Object Relational Mapping(对象关系映射)。指的是将一个Java中的对象与关系型数据库中的表建立一种映射关系,从而操作对象就可以操作数据库中的表。
封装一个类和数据库中表建立映射

       1.3.3.3 为什么要学习Hibernate

在这里插入图片描述

1.4 Hibernate的入门
   1.4.1 Hibernate的入门
        1.4.1.1 下载Hibernate的开发环境

      Hibernate3.x Hibernate4.x Hibernate5.x(4算是过度版本把,很少用4,5就完事了)
  https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/

        1.4.1.2 解压Hibernate

在这里插入图片描述
 documentation :Hibernate开发的文档
 lib :Hibernate开发包
 required :Hibernate开发的必须的依赖包
 optional :Hibernate开发的可选的jar包
 project :Hibernate提供的项目

        1.4.1.3 创建一个项目,引入jar包

      数据库驱动包
      Hibernate开发的必须的jar包
      Hibernate引入日志记录包
在这里插入图片描述

        1.4.1.4 创建表
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;

        1.4.1.5 创建实体类
package com.itheima.hibernate.demo1;

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;
	}
	@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 + "]";
	}
	
}

        1.4.1.6 创建映射(*****)

      映射需要通过XML的配置文件来完成,这个配置文件可以任意命名。尽量统一命名规范(类名.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.itheima.hibernate.demo1.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id" >
			<generator class="native"/>
		</id>
		
		<!-- 建立类中的普通的属性和表的字段的对应 -->
		<property name="cust_name" column="cust_name" length="32" />
		<property name="cust_source" column="cust_source" length="32"/>
		<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>
        1.4.1.7 创建一个Hibernate的核心配置文件(*****)

       Hibernate的核心配置文件的名称: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.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<!-- 配置Hibernate的方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 可选配置================ -->
		<!-- 打印SQL -->
		<property name="hibernate.show_sql">true</property>
		<!-- 格式化SQL -->
		<property name="hibernate.format_sql">true</property>
		<!-- 自动创建表 -->
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- 配置C3P0连接池 -->
		<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<!--在连接池中可用的数据库连接的最少数目 -->
		<property name="c3p0.min_size">5</property>
		<!--在连接池中所有数据库连接的最大数目  -->
		<property name="c3p0.max_size">20</property>
		<!--设定数据库连接的过期时间,以秒为单位,
		如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
		<property name="c3p0.timeout">120</property>
		 <!--3000秒检查所有连接池中的空闲连接 以秒为单位-->
		<property name="c3p0.idle_test_period">3000</property>
		
		<!-- 设置事务隔离级别 -->
		<property name="hibernate.connection.isolation">4</property>
		<!-- 配置当前线程绑定的Session -->
		<property name="hibernate.current_session_context_class">thread</property>
		
		<mapping resource="com/itheima/hibernate/demo1/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
        1.4.1.8 编写测试代码(*****)
package com.itheima.hibernate.demo1;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * Hibernate的入门案例
 * @author jt
 *
 */
public class HibernateDemo1 {

	@Test
	// 保存客户的案例
	public void demo1(){
		// 1.加载Hibernate的核心配置文件
		Configuration configuration = new Configuration().configure();
		// 2.创建一个SessionFactory对象:类似于JDBC中连接池
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.通过SessionFactory获取到Session对象:类似于JDBC中Connection
		Session session = sessionFactory.openSession();
		// 4.手动开启事务:
		Transaction transaction = session.beginTransaction();
		// 5.编写代码
		
		Customer customer = new Customer();
		customer.setCust_name("王西");
		
		session.save(customer);
		
		// 6.事务提交
		transaction.commit();
		// 7.资源释放
		session.close();
		sessionFactory.close();
	}
}
        1.5 关于日志问题解决方法

在这里插入图片描述
解决方法在项目src下边创建    log4j.properties  文件
在这里插入图片描述

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###
# error warn info debug trace
log4j.rootLogger= info, stdout

写这博客目的一是为了自己有学习笔记,回顾加深记忆.其次希望能帮助到准备学习hibernate或遇到相关问题的朋友.最后感谢白菜大佬让我有了学习的方向.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值