Hibernate1之例子
来源:互联网 发布:特效ae软件下载 编辑:程序博客网 时间:2024/05/29 13:28
Hibernate
1、描述
orm(object relation mapping框架,处于持久层
对jdbc的轻量级封装
2、优点
跨数据库,不需要重写业务层
入门案例 第二种方式
/**SessionFactory是重量级,做成单例一个数据库对应一个*/
表内容
1、建表
create table employee(id number primary key,name nvarchar2(64) not null,email nvarchar2(64) not null,hiredate date not null);
2、序列
create sequence emp_seqstart with 1increment by 1minvalue 1nomaxvaluenocyclenocache;
结构图
创建pojo
package net.hibernate.pojo;import java.util.Date;//对应表首字母大写 序列化,为唯一public class Employee {private Integer id;private String name;private String email;private Date hiredate;public Integer getId() { return id;}public void setId(Integer id) { this.id = id;}public String getName() { return name;}public void setName(String name) { this.name = name;}public String getEmail() { return email;}public void setEmail(String email) { this.email = email;}public Date getHiredate() { return hiredate;}public void setHiredate(Date hiredate) { this.hiredate = hiredate;}}
Employee.hbm.xml文件
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <!-- 操作的是哪个包 --><hibernate-mapping package="net.hibernate.pojo"> <class name="Employee" table="employee"> <!-- id用与指定主键属性 --> <id name="id" length="64" column="id"> <!-- generator用来指定主键生成策略hilo、uuid、increment、sequence、native --> <!-- 如果不希望用序列,class="assigned" --> <generator class="sequence"> <param name="sequence">emp_seq</param> </generator> </id> <property name="name" type="string"> <column name="name" not-null="false" /> </property> <property name="email" not-null="false" type="java.lang.String" /> <property name="hiredate" not-null="false" type="java.util.Date"/> </class></hibernate-mapping>
hibernate.cfg.xml配置文件
<!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.dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="hibernate.connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <property name="hibernate.connection.username">scott</property> <property name="hibernate.connection.password">tiger</property> <property name="hibernate.connection.url"> jdbc:oracle:thin:@localhost:1521:ORCL </property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 控制台显示sql语句 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 映射文件 --> <mapping resource="net/hibernate/pojo/Employee.hbm.xml" /></session-factory></hibernate-configuration>
工具类,获得session
package net.hibernate.view.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {private static SessionFactory sessionFactory;static{ Configuration config=new Configuration().configure(); sessionFactory=config.buildSessionFactory();} public static Session getSession(){ return sessionFactory.openSession(); }}
测试代码
package net.hibernate.view;import java.util.Date;import org.hibernate.Session;import org.hibernate.Transaction;import net.hibernate.pojo.Employee;import net.hibernate.view.util.HibernateUtil;public class TestMain { public static void main(String[] args) { //完成crud操作 insertEmp(); } public static void insertEmp(){ Employee emp=new Employee(); emp.setName("苏东坡"); emp.setEmail("2812394@qq.com"); emp.setHiredate(new Date()); Session session=HibernateUtil.getSession(); //事务 Transaction tr=session.beginTransaction(); try { session.save(emp); tr.commit(); } catch (Exception e) { e.printStackTrace(); tr.rollback(); }finally{ session.close(); } }}
0 0
- Hibernate1之例子
- hibernate1
- Hibernate1
- hibernate1
- Hibernate1
- Hibernate1
- Hibernate1
- hibernate1
- Hibernate1
- Hibernate1 Xdoclet
- Hibernate1--简介
- hibernate1.1
- hibernate1.2
- Hibernate1(入门)
- Hibernate1基本知识点
- automake之hello例子
- automake之hello例子
- 委托例子之二
- linux下的端口扫描工具
- [Java并发包学习五]CountDownLatch和CyclicBarrier介绍
- 最值得看的十大机器学习公开课
- 用virtualenv建立多个Python独立开发环境
- Properties中用到的类型
- Hibernate1之例子
- SecureCRT和FX 的下载激活方法
- 项目管理的五个过程和九大知识领域
- 分针网—IT教育: JS 实现随机验证码功能
- 用virtualenv建立多个Python独立开发环境
- execve函数的介绍与使用
- Retrofit2.0初探
- 前后台实现验证当前操作记录是否存在
- 关于 UIBezierPath