包结构如下图所示(按图标进行对齐):

环境搭好后代码分为以下几步:

 /**
* private static final Configuration CONFIGURATION;
* private static final SessionFactory SESSION_FACTORY;
* 1、加载配置文件
* Configuration CONFIGURATION = new Configuration().configure();
* 2、创建 session工厂
* SessionFactory SESSION_FACTORY = CONFIGURATION.buildSessionFactory();
* 3、创建session (类似于connection)
* Session SESSION_FACTORY.getCurrentSession()
* 4、开启事务
* Transaction tx = session.beginTransaction();
* 5、操作
* 6、提交事务
* tx.commit();(如果 session 不是线程绑定需执行session.close();)
* @author Administrator
*
*/

下面进行详细说明:

创建customer   javabean

 package cn.itcast.domain;

 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;
} }

在javaBean所在的包下创建映射关系文件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>
<!-- 建立表和类的映射关系 name类的全路径 table表名 catalog 数据库名(可省略) -->
<class name="cn.itcast.domain.Customer" table="customer" catalog="hibernate">
<!-- id 表示主键 name类属性名 column表中字段名 如果相同可以省略-->
<id name="cust_id" column="cust_id"><generator class="native"/></id>
<!-- property 表示表中的普通属性 -->
<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_moblie"/>
<!-- length 字段长度 type 数据类型-->
</class>
</hibernate-mapping>

在src下创建Hibernate的配置文件    hibernate.cfg.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 必要的配置信息 :连接数据库的基本参数 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernate?characterEncoding=utf8</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否输出sql语句 -->
<property name="show_sql">true</property>
<!-- 是否格式化sql语句 -->
<property name="format_sql">true</property>
<!-- Hibernat的hbm2ddl(数据定义语言:cteate drop alter ...)-->
<!--
hbm2ddl.auto的取值:
*none :不用Hibernate自动生成表
*create : 每次都会创建一个新的表(测试)
*create-drop:每次都会创建一个表,执行程序结束后删除这个表(测试)
*update :如果数据库中有表使用原来的表,创建一个新表,可以更新表结构
*validate :只会使用原来的表对映射关系进行校验
-->
<property name="hbm2ddl.auto">update</property>
<!-- 配置本地jdbc事务配置 ,通过getCurrentSession创建的session会绑定当前线程
thread 对象生命周期会与本地线程绑定
jta 对象生命周期与JTA事务绑定
managed Hibernate委托程序管理对象的生命周期
-->
<property name="current_session_context_class">thread</property> <!-- hibernate内部维护有一个连接池,如果要使用c3p0 等外部连接池 可以添加相应的jar包后进行配置 -->
<!-- 最小连接数 -->
<property name="c3p0.min_size">5</property>
<!-- 最大连接数 -->
<property name="c3p0.max_size">20</property>
<!-- 设定数据库连接的过期时间 以秒为单位 -->
<property name="c3p0.timeout">120</property>
<!-- 没3000秒检查所有数据库的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property> <!-- 加载映射 -->
<property name="myeclipse.connection.profile"></property>
<mapping resource="cn/itcast/domain/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>

封装加载配置类为工具类:

 package cn.util;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 加载配置文件 封装工具类
* @author Administrator
*
*/
public class HibernateUtil {
private static final Configuration CONFIGURATION;
private static final SessionFactory SESSION_FACTORY;
static{
//加载配置文件
CONFIGURATION = new Configuration().configure();
//创建一个SessionFactory
SESSION_FACTORY = CONFIGURATION.buildSessionFactory();
}
/**
* 提供获得Session的方法
*/
/**
* 直接创建新的Session实例 使用完成后需要调用 close方法手动关闭
* @return
*/
public static Session openSession() {
return SESSION_FACTORY.openSession();
}
/**
* 创建的session会被绑定到当前线程中,它在提交或回滚操作时会自动关闭
* @return
*/
public static Session getCurrentSession(){ return SESSION_FACTORY.getCurrentSession();
}
}

创建测试类进行测试:

 public class Demo1 {

 public static void main(String[] args) {
////获取session
//Session session = HibernateUtil.getCurrentSession();
////开启事务
//Transaction tx = session.beginTransaction();
//Customer customer= new Customer();
//customer.setCust_name("小王");
//customer.setCust_source("网络推广");
//session.save(customer);
////事务提交
//tx.commit();
////如果采用 openSession 获取实例 还需要 执行session.close();释放资源
////session 为轻量级 Configuration SessionFactory为重量级 之间重建所消耗的资源不同 //Session session = HibernateUtil.getCurrentSession();
//Transaction tx = session.beginTransaction();
//Customer customer = session.get(Customer.class,Long.valueOf("1"));
//session.delete(customer);
//tx.commit(); Session session =HibernateUtil.getCurrentSession();
Transaction tx = session.beginTransaction();
Customer customer = session.get(Customer.class,Long.valueOf("2"));
customer.setCust_mobile("124575615121");
session.update(customer);
tx.commit();
}
}

Hibernate连接数据库的更多相关文章

  1. JDBC、mybatis、hibernate连接数据库

    JDBC连接数据库五步骤: 一.加载驱动 Class.forName(“com.mysql.jdbc.Driver”); 二.建立连接 Connection conn = DriverManager. ...

  2. MySQL数据库的使用流程,代码解释+Hibernate连接数据库

    数据库的使用流程: 1.注册驱动: 2.用DriverManager.getConnection方法获得连接对象con: A方法:  3.用连接对象的createStatement()方法,获得可以执 ...

  3. hibernate连接数据库和反向工程

    一.JSP界面连接数据库: 导包:将11个包倒进web-inf的lib目录下: 二.建立hibernate.cfg.xml的配置文件:!注意:是放到项目SRC目录下: 三.将视图切换到java下,在左 ...

  4. hibernate连接数据库和使用

    hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibe ...

  5. Hibernate连接数据库超时设置autoReconnect=true

    如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat. 不用hibernate的话, connection url加参数: autoReconnect ...

  6. hibernate连接数据库的步骤

    三个准备 一.导包   mysql二.在默认src下创建hibernate.cfg.xml   1.创建xml文件,命名为hibernate.cfg.xml 2.添加约束   (在org.hibern ...

  7. hibernate 连接数据库时报错

         错误信息 : com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allo ...

  8. *IntelliJ IDEA使用Hibernate连接数据库

    在IntelliJ IDEA中配置MySQL Database.

  9. hibernate连接数据库中文乱码

    4.做完这两步还是不行,需要修改hibernate的配置文件hibernate.cfg.xml,在配置文件配置hibernate.connection.url属性.示例: <property n ...

随机推荐

  1. Android实战简易教程-第四十九枪(两种方式实现网络图片异步加载)

    加载图片属于比较耗时的工作,我们需要异步进行加载,异步加载有两种方式:1.通过AsyncTask类进行:2.通过Handler来实现,下面我们就来看一下如何通过这两种方式实现网络图片的异步加载. 一. ...

  2. javaScript中计算字符串MD5

    进行HTTP网络通信的时候,调用API向服务器请求数据,有时为了防止API调用过程中被黑客恶意篡改,所请求参数需要进行MD5算法计算,得到摘要签名.服务端会根据请求参数,对签名进行验证,签名不合法的请 ...

  3. 题解 P1967 货车运输

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车在不超过车辆限重的情况下,最多能 ...

  4. JS 实现全屏预览 F11功能

    老是不通过,没办法,只能是重新发布了,反正我就是杠上了,大大小小写过很多前端特效,当然也经常在网上copy或者修改人家的代码,我觉得也挺好的,为什么?!因为我想这样,你能怎么办,打我?少废话,直接上代 ...

  5. 基于flask的网页聊天室(一)

    基于flask的网页聊天室(一) 基本目标 基于flask实现的web聊天室,具有基本的登录注册,多人发送消息,接受消息 扩展目标 除基本目标外添加当前在线人数,消息回复,markdown支持,历史消 ...

  6. day21 05 员工信息表

    day21 05 员工信息表 假设有一个员工信息表,里面有每个员工的名字,id,年龄,电话,还有他们所作的工作,而有时候我们并不需要所有的信息,而想根据某些条件,寻找符合条件即可,即筛选, 比如想要筛 ...

  7. 51中xdata,idata,data,pdata的区别

    51系列中data,idata,xdata,pdata的区别 data: 固定指前面0x00-0x7f的128个RAM,可以用acc直接读写的,速度最快,生成的代码也最小. idata: 固定指前面0 ...

  8. 关于Filter中ServletRequest和ServletResponse强转HttpServletRequest和HttpServletResponse安全问题(向下转型一定不安全吗?)

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...

  9. linux-NMON监控

  10. 九度oj 题目1203:IP地址

    题目1203:IP地址 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3636 解决:1800 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1< ...