zoukankan      html  css  js  c++  java
  • Hibernate入门

    okay,了解了struts2后开始搭建hibernate环境

    eclipse 4.3.2

    jdk 1.7.0_45

    hibernate 4.3.5

    mysql 5.1.67

    准备好上面的环境,安装hibernate(拷贝hibernate所需lib-->requered-->jar包和mysql的java驱动jar包拷到lib里面)

    在数据库新建一个数据库hibernate,创建usertable表字段有name,password

    在eclipse中新建一个java project Hiernate

    src目录下新建一个hibernate.cfg.xml文件(可以在文档里面找到基本的配置,然后根据自己的情况修改一下)

    <?xml version='1.0' encoding='utf-8'?>
    <!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>
    
            <!-- Database connection settings -->
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</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.internal.NoCacheProvider</property>
    
            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>
    
            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>
    
            <mapping resource="com/gxf/beans/user.hbm.xml"/>
    
        </session-factory>
    
    </hibernate-configuration>

    在src目录下新建一个bean,User.java

    package com.gxf.beans;
    
    public class User {
        private String name;
        private String password;
        
        public User(String name, String password) {
            super();
            
            this.name = name;
            this.password = password;
        }
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
    }

    在与bean同目录下新建一个user.hbm.xml(这里可以在文档里面找到,根据自己的bean和数据库表进行修改)

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3 "-//Hibernate/Hibernate Mapping DTD//EN"
     4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
     5 
     6 <hibernate-mapping package="com.gxf.beans">
     7 
     8     <class name="User" table="usertable">
     9         <id name="name"> 
    10         </id>
    11         <property name="password"/>
    12     </class>
    13 
    14 </hibernate-mapping>

    在src目录下新建一个测试类,Test.java

     1 package com.gxf.test;
     2 
     3 import org.hibernate.HibernateException;
     4 import org.hibernate.Session;
     5 import org.hibernate.SessionFactory;
     6 import org.hibernate.Transaction;
     7 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
     8 import org.hibernate.cfg.Configuration;
     9 import org.hibernate.service.ServiceRegistry;
    10 
    11 import com.gxf.beans.User;
    12 
    13 public class Test {
    14 
    15     public static void main(String[] args) {
    16         try {
    17             Configuration configuration = new Configuration().configure();
    18             ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
    19             SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    20             Session session = sessionFactory.openSession();
    21             
    22             
    23             Transaction tx = session.beginTransaction();
    24             User userToSave = new User("lisi","luckyzhangsan");
    25             session.save(userToSave);
    26             tx.commit();
    27             session.close();
    28            } catch (HibernateException e) {
    29             e.printStackTrace();
    30            }
    31         }
    32 
    33     }

    最后运行Test.java,控制台没有报错,病输出对应的sql语句。到数据库中执行查找命令,插入成功

    这里只是为了测试hibernate环境,熟悉hibernate的工作流程,比较粗糙

  • 相关阅读:
    css3
    css3
    npm 安装包无法继续下载? 卡住
    tcp/ip协议中的SYN, ACK的数值变化
    【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)
    ES6 中 Symbol.split的用法
    Why does Typescript use the keyword “export” to make classes and interfaces public?
    es6中的import,export浏览器已经支持
    Understanding the JavaScript Engine—— two phase
    【转】js-ES6学习笔记-Symbol
  • 原文地址:https://www.cnblogs.com/luckygxf/p/3740392.html
Copyright © 2011-2022 走看看