• Hibernate基本构建


    ===========================属性文件的配置================================
    <?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">
    <!-- Generated by MyEclipse Hibernate Tools.                   -->
    <!-- 重点:学会hibernate的属性的配置(连接数据库) -->
    <hibernate-configuration>

     <session-factory>
      <!-- 数据库方言 -->
      <property name="dialect">
       org.hibernate.dialect.MySQLDialect
      </property>
      <!-- 路径 -->
      <property name="connection.url">
       jdbc:mysql://127.0.0.1:3309/hibernatetest
      </property>
      <!-- 用户名 -->
      <property name="connection.username">root</property>
      <!-- 密码 -->
      <property name="connection.password">root</property>
      <!-- 驱动 -->
      <property name="connection.driver_class">
       com.mysql.jdbc.Driver
      </property>
      <!-- myeclipse中的数据库连接名 -->
      <property name="myeclipse.connection.profile">fengke</property>
      <!-- 显示数据查询语句 -->
      <property name="show_sql">true</property>
      <!-- 匹配的持久化类路径 -->
      <mapping resource="fengke/vo/Login.hbm.xml" />
     </session-factory>

    </hibernate-configuration>
    =====================================================================
    我是用的是myeclipse,因此反向工程自动生成实体类
    ============================JUnit4 测试==========================================
    package test;

    import static org.junit.Assert.*;
    import java.sql.Time;
    import java.util.List;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import fengke.util.HibernateSessionFactory;
    import fengke.vo.Login;
    /**
     * Hibernate测试
     * @author 锋客
     * 注意:
     *   1、对于事务的理解:
     *       在本例中如果不开启事务,做以一下循环
     *      int i = 0;
     *  while (i < 3) {
     *   Login login = new Login("fengke", "123456");
     *   session.save(login);
     *   i++;
     *  }
     *  String hql = "from Login";
     *  Query query = session.createQuery(hql);
     *  List<Login> list = query.list();
     *  for (Login q : list) {
     *   System.out.println(q.getId());
     *  }
     *   结果:数据库中不会有数据存入,但循环输出可以实现。
     *   结论:事务就像是先将数据临时存入到数据库中,可以进行任何操作,但是当你不提交事务时,
     *       程序运行结束,会清空操作的数据,还原到原本的数据库,但是数据库会记录这些操作,因为
     *       id主键的变化,并没有从1开始,而是从上次事务操作数据库最后一条数据的id为标准。
     *      
     *
     */
    public class HibernateTest {
     static Session session = null;
     static Transaction transaction = null;

     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
      
     }

     @AfterClass
     public static void tearDownAfterClass() throws Exception {
      
     }

     @Before
     public void setUp() throws Exception {
      // 配置hibernate环境
        System.out.println("开始配置Hibernate的环境");
        try {
         // Configuration config = new Configuration().configure();
         // SessionFactory sessionFactory = config.buildSessionFactory();
         session = HibernateSessionFactory.getSession();
         //使用beginTransaction()开启事务
         transaction = session.beginTransaction();
        } catch (Exception e) {
         e.printStackTrace();
        }

     }

     @After
     public void tearDown() throws Exception {
      // 关闭session
        System.out.println("关闭session");
        try {
         transaction.commit();
         session.close();
        } catch (Exception e) {
         e.printStackTrace();
        }
     }
       
     
     /*
      * 测试是否Hibernate配置成功
      */
     @Test
     public void test() {
      int i = 0;
      while (i < 3) {
       Login login = new Login("fengke", "123456");
       session.save(login);
       i++;
      }
      String hql = "from Login";
      Query query = session.createQuery(hql);
      List<Login> list = query.list();
      for (Login q : list) {
       System.out.println(q.getId());
      }

     }
     
     
    }
    =================================更换数据库(更换另一个属性文件)==================================
    package fengke.dao;

    import java.util.List;

    import org.hibernate.Hibernate;
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;

    import fengke.util.HibernateSessionFactory;
    import fengke.vo.Login;
    import fengke.vo1.User;

    public class Dao {
     
     public static void main(String[] args) {
      Session session = HibernateSessionFactory.getSession();
      Transaction tran=session.beginTransaction();
      int i=0;
      while (i<3) {
       Login login=new Login("fengke", "123456");
       session.save(login);
       i++;
      }
      String hql="from Login";
       Query query = session.createQuery(hql);
      List<Login> list = query.list();
      for(Login q:list){
       System.out.println("aaa");
      }
      tran.commit();//可以测试事务的特性
      session.close();
      System.out.println("******************更换数据库********************");
      exchangeDB();
        
     }
       //更换数据库
     private static void exchangeDB() {
      //设置配置文件(链接数据库的文件)
      //注意:最好实体类的名字不要重复,否则
      /*1:比较简单的办法,更改其中一个类名。
      * 2:第二种解决办法,将类的映射文件,
      * <hibernate-mapping>结点后加上auto-import="false",默认为true,
      * 即变成<hibernate-mapping auto-import="false">,但是这样改了以后,千万要注意,
      * 在写HQL语句时候,比如"from cn.com.test01.TUser4",一定要加上完整包名,
      * 因为你设置了auto-import="false".
      */
      HibernateSessionFactory.setConfigFile("/hibernatefeng.cfg.xml");
      Session se = HibernateSessionFactory.getSession();
      Transaction tran=se.beginTransaction();
      int i=0;
      while (i<3) {
       User login=new User("root", "root");
       se.save(login);
       i++;
      }
      tran.commit();
      se.close();
     }

    }

  • 相关阅读:
    MPLS TE 配置与各大属性调整
    Net学习日记_基础提高_9
    Net学习日记_基础提高_8
    Net学习日记_基础提高_7
    Net学习日记_基础提高_6
    Net学习日记_基础提高_5
    Net学习日记_基础提高_4
    Net学习日记_基础提高_3
    Net学习日记_基础提高_2
    Net学习日记_基础提高_1
  • 原文地址:https://www.cnblogs.com/fengke/p/4916083.html
Copyright © 2020-2023  润新知