zoukankan      html  css  js  c++  java
  • Hibernate是怎么工作的——Hibernate的工作流程

    举个简单的样例说明:

    1.Base.java

    package cn.flymouse.hibernate;
    
    import java.util.Date;
    
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import cn.flymouse.hibernate.domain.User;
    
    public class Base {
    
    	public static void main(String[] args) {
    		//读取并解析配置文档hibernate.cfg.xml文件
    		Configuration cfg = new Configuration().configure();
    		//读取并解析映射文件,以Configuration创建SessionFactory
    		SessionFactory sf = cfg.buidSessionFactory();
    		//创建session
    		Session session = sf.openSession();
    		//开启事务
    		Transaction tx = session.beginTransaction();
    		//持久化操作
    		User user = new User();
    		user.setBirthday(new Date());
    		user.setName("name");
    		session.save(user);
    		//提交事务
    		tx.commit();
    		//关闭Session
    		session.close();
    		//关闭SessionFactory
    		sf.close();
    	}
    }
    

    2.User.java

    package cn.flymouse.hibernate.domain;
    
    import java.util.Date;
    
    public class User {
    	private int id;
    	private String name;
    	private Date birthday;
    
    	public int getId() {
    		return id;
    	}
    	pubilc String getName(){
    		return name;
    	}
    	public Date getBirthday() {
    		return birthday;
    	}
    	
    	public void setId(int id) {
    		this.id = id;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public void setBirthday(Date birthday) {
    		this.birthday = birthday;
    	}
    }
    

    3.User的映射文件User.hbm.xml(跟User类放同个文件夹): 

    <?

    xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.flymouse.hibernate.domain"> <class name="User"> <id name="id"> <generator class="native" /> </id> <property name="name" unique="false"/> <property name="birthday" /> </class> </hibernate-mapping>


    4.hibernate的配置文件hibernate.cfg.xml(放在类路径下,即src文件夹下): 

    <!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:///test</property>
    		<property name="connection.username">root</property>
    		<property name="connection.password">123456</property>
    		<!-- 方言,用于生成SQL语句 -->
    		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    		<!-- 自己主动建表 -->
    		<property name="hbm2ddl.auto">create</property>
    		<property name="show_sql">true</property>
    	
    		<mapping resource="cn/flymouse/hibernate/domain/User.hbm.xml"/>
    	</session-factory>
    </hibernate-configuration>


  • 相关阅读:
    运算符重载
    vmware 下 ubuntu 不能全屏显示 的解决方法
    最优化
    常见算法(logistic回归,随机森林,GBDT和xgboost)
    转:CRF++词性标注
    条件随机场(CRF)理论及应用
    转:RNN(Recurrent Neural Networks)
    RNN(Recurrent Neural Networks)公式推导和实现
    ML、DL相关资源
    机器学习(周志华西瓜书) 参考答案 总目录
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6916503.html
Copyright © 2011-2022 走看看