20151215 Hibernate学习笔记------基本的配置和使用方法
来源:互联网 发布:手机淘宝怎么买火车票 编辑:程序博客网 时间:2024/06/06 03:00
20151215 Hibernate学习笔记——基本的配置和使用方法
Hibernate 基本配置
1. 下载 Hibernate
2. 导入jar包
将hibernate根目录\lib\required文件夹下的包全部导入,同时还需要导入数据库的驱动包.要注意的是因为要使用JPA标准的标签(需要import javax.persistence),所以还要导入jpa.jar包(百度一下下载即可).(看一些介绍说不用导此包也行,但我不导却报错了,待研究)
3. 配置xml文档
在中文html文档中有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> <!-- Database connection settings 数据库的设置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hj_legend</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- JDBC connection pool 连接池 (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect 数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache 缓存 --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout 输出sql语句 --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup 更新数据库(无则创建,有则不创建) --> <property name="hbm2ddl.auto">update</property> <!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> --> <!-- 配置持久化类的类名 --> <mapping class="com.hj.hibernate.bean.User"/> </session-factory></hibernate-configuration>
4. 用注解配置javaBean
package com.hj.hibernate.bean;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity//说明此类是一个持久化类@Table(name = "tb_user")//说明对应的数据库表名public class User{ @Id//说明此字段是id @GeneratedValue(strategy=GenerationType.IDENTITY)//id自动增长 private int userId;//以下三个字段为了简便命名与数据库中的命名相同,这样就不用单独配置了 private String userName; private String password; public User() { super(); } public User(String userName, String password) { super(); this.userName = userName; this.password = password; } public int getId() { return userId; } public void setId(int id) { this.userId = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}
用Hibernate操作数据库
基本步骤:
- 获取Configuraction
- 获取SessionFactory
- 获取session并开始事务
- 用面向对象的方法操作数据库
- 关闭资源
java代码如下(向tb_user表中添加一条记录):
package com.hj.hibernate.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.hj.hibernate.bean.User;/** * 12/15/2015 11:06:15 AM * @author HangJian * */public class Main{ /** * @param args */ public static void main(String[] args) { //获得配置 Configuration cfg = new Configuration().configure(); //获取sessionFactory SessionFactory sf = cfg.buildSessionFactory(); //创建session Session sess = sf.openSession(); //开始事务 Transaction ts = sess.beginTransaction(); //创建实体类的对象 User user = new User("hjhjhjhjhjhj", "123"); //保存 sess.save(user); //提交事务 ts.commit(); //关闭session sess.close(); sf.close();/**/ }}
1 0
- 20151215 Hibernate学习笔记------基本的配置和使用方法
- hibernate学习笔记-基本配置和执行流程
- Hibernate学习笔记(二)基本配置
- ibatIS学习笔记---Ibatis的基本使用方法
- log4j的基本配置和使用方法
- Log4j的基本配置和使用方法
- Log4j的基本配置和使用方法
- Hibernate学习笔记(二)——Hibernate的一些基本配置
- {传智播客} (学习笔记)--hibernate的使用和配置
- Hibernate学习03---Hibernate基本配置以及Annotation和Juni
- 如何配置struts+hibernate,基本使用方法
- Hibernate学习笔记(4):基本映射以及相关属性配置
- Hibernate学习笔记(二)【标识符及基本级联配置】
- Hibernate学习笔记(二)【标识符及基本级联配置】
- web学习笔记25-Hibernate基本配置使用
- Git 学习笔记 --- 安装和基本配置
- VisualSVN Server和TotoiseSVN的配置和基本使用方法
- hibernate的基本配置
- JPA总结
- linux notes
- linux6.5 redhat 安装centos 免注册的yum 源
- Delphi属性Property学习应用总结
- jQuery设置和获取HTML、文本和值
- 20151215 Hibernate学习笔记------基本的配置和使用方法
- 运行Delphi 2007 IDE提示无法打开"EditorLineEnds.ttr"文件
- 学习中的蜕变
- 【JEECG技术博文】JEECG图表配置说明
- 为什么要学习elua?
- Kinect for Windows SDK开发入门(十三):语音识别 下
- PHP 虚拟主机配置
- JDBC连接MySQL程序
- android学习中知识点集合(未完)