zoukankan      html  css  js  c++  java
  • Hibernate基本演示

    保存一个对象到数据库中

    目录结构

    hibernate.cfg.xml

    <!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/Lesson7_Hibernate3_Demo1</property>
            <property name="connection.username">root</property>
            <property name="connection.password">123456</property>
            <!--方言-->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    
            <!--参数信息-->
            <!--显示sql-->
            <property name="show_sql">true</property>
            <!--如果没有表 自动创建-->
            <property name="hbm2ddl.auto">create</property>
           <!-- <property name="c3p0.max_size">100</property>
            <property name="c3p0.min_size">10</property>
            <property name="cache.use_second_level_cache">true</property>
            <property name="cache.use_query_cache">true</property>
            <property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
    
            <property name="generate_statistics">true</property>-->
    
            <!--  
            <class-cache class="cn.itcast.hibernate.domain.User" usage="read-only"/>
            -->
    
            <!--映射文件-->
            <mapping resource="org/zln/hibernate/domain/hbm/User.hbm.xml"/>
    
        </session-factory>
    </hibernate-configuration>

    HibernateUtils.java

    package org.zln.hibernate.utils;
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    /**
     * Created by coolkid on 2015/6/14 0014.
     */
    public class HibernateUtils {
        private static Configuration configuration = new Configuration();
        private static SessionFactory sessionFactory;
        private static final String PATH = "hibernate.cfg.xml";
        static {
            configuration.configure(PATH);
        }
        public static Session getSession(){
            sessionFactory = configuration.buildSessionFactory();
            return sessionFactory.openSession();
        }
    }

    User.hbm.xml

    <?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="org.zln.hibernate.domain">
    
        <class name="User">
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="name"/>
            <property name="birthday"/>
        </class>
    
    </hibernate-mapping>

    UserDao.java

    package org.zln.hibernate.dao;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.zln.hibernate.domain.User;
    import org.zln.hibernate.utils.HibernateUtils;
    
    /**
     * Created by coolkid on 2015/6/14 0014.
     */
    public class UserDao {
    
        public void saveUser(User user){
            Session session = HibernateUtils.getSession();
            Transaction transaction = session.beginTransaction();
            try {
                session.save(user);
                session.flush();
                transaction.commit();
            }finally {
                session.close();
            }
        }
    }
  • 相关阅读:
    常用CSS代码大全(工作必备)
    微信开发新增拖动组件--movableview介绍
    CSS---解决文本溢出,换行
    SublimeText 自带格式化代码功能
    后台界面也可以很酷!31个高大上的后台管理系统模版
    漏洞:阿里云盾phpMyAdmin <=4.8.1 后台checkPageValidity函数缺陷可导致GETSHELL
    Linux系统定时备份网站文件到七牛云存储脚本
    php重定向的三种方法分享
    CentOS Gnome字体不清晰
    centos 6.5 安装mplayer
  • 原文地址:https://www.cnblogs.com/sherrykid/p/4575368.html
Copyright © 2011-2022 走看看