Hibernate基础

ORM和Hibernate简介:










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/test</property>
		<property name="Connection.useUnicode">true</property>
		<property name="connection.characterEncoding">UTF-8</property>
		<property name="connection.username">root</property>
		<property name="connection.password">admin</property>
		
		<!-- 连接池信息 -->
		<property name="hibernate.c3p0.max_size">20</property>
		<property name="hibernate.c3p0.min_size">1</property>
		<property name="hibernate.c3p0.timeout">5000</property>
		<property name="hibernate.c3p0.max_statements">100</property>
		<property name="hibernate.c3p0.idle_test_period">3000</property>
		<property name="hibernate.c3p0.acquire_increment">2</property>
		
		<!-- 指定数据库方言 -->
		<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
		<!-- 根据需要自动创建数据表 -->
		<property name="hbm2ddl.auto">update</property>
		<!-- 显示hibernate持久化操作生成的SQL -->
		<property name="show_sql">true</property>
		<!-- 将SQL脚本格式化后再输出 -->
		<property name="hibernate.format_sql">true</property>
		
		<!-- 映射的持久化对象的类 -->
		<mapping class="com.song.hibernate.service.model.po.UserInfoPO"/>
	</session-factory>
</hibernate-configuration>


HibernateXmlMain.java

package com.song.hibernate.web;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.song.hibernate.service.model.po.UserInfoPO;

@SuppressWarnings("deprecation")
public class HibernateXmlMain {

	public static void main(String[] args) {
		//实例化Configuration,不带参数的configure()方法默认加载hibernate.cfg.xml,若传入参数则加载指定文件
		Configuration conf = new Configuration().configure();
		
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
		//用Configuration实例创建SessionFactory实例
		SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
		//创建Session,PO对象只有在Session的管理下才可完成数据库访问
		Session sess = sf.openSession();
		//开始事务
		Transaction tx = sess.beginTransaction();
		
		
		UserInfoPO userInfoPO = new UserInfoPO();
		userInfoPO.setUsername("刘大");
		userInfoPO.setUserpassword("111111");
		userInfoPO.setLoginaccount("admin");
		userInfoPO.setJobnumber("1111");
		
		//保存消息
		sess.save(userInfoPO);
		//提交事务
		tx.commit();
		//关闭Session
		sess.close();
		sf.close();
	}
}

hibernate.properties

## MySQL

hibernate.dialect org.hibernate.dialect.MySQL5Dialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
hibernate.connection.username root
hibernate.connection.password admin

hibernate.c3p0.max_size 20
hibernate.c3p0.min_size 1
hibernate.c3p0.timeout 5000
hibernate.c3p0.max_statements 100
hibernate.c3p0.idle_test_period 3000
hibernate.c3p0.acquire_increment 2

hibernate.hbm2ddl.auto update
hibernate.show_sql true
hibernate.format_sql true


HibernatePropertiesMain.java

package com.song.hibernate.web;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.song.hibernate.service.model.po.UserInfoPO;

@SuppressWarnings("deprecation")
public class HibernatePropertiesMain {

	public static void main(String[] args) {
		Configuration conf = new Configuration().addAnnotatedClass(UserInfoPO.class);
		
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
		//用Configuration实例创建SessionFactory实例
		SessionFactory sf = conf.buildSessionFactory(serviceRegistry);
		//创建Session,PO对象只有在Session的管理下才可完成数据库访问
		Session sess = sf.openSession();
		//开始事务
		Transaction tx = sess.beginTransaction();
		
		UserInfoPO userInfoPO = new UserInfoPO();
		userInfoPO.setUsername("刘二");
		userInfoPO.setUserpassword("111111");
		userInfoPO.setLoginaccount("admin");
		userInfoPO.setJobnumber("1111");
		
		//保存消息
		sess.save(userInfoPO);
		//提交事务
		tx.commit();
		//关闭Session
		sess.close();
		sf.close();
	}
}





User Libraries:



Hibernate的体系结构:







深入Hibernate配置文件:









深入理解PO对象:



                                                


深入Hibernate映射:

@Entity,@Table, @Column,@Formula, @Id,@GeneratedValue

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值