zoukankan      html  css  js  c++  java
  • Hibernate—01

    Hibernate_01

    一、要导的包:

    1、核心包

    2、Required里面的全部

    3、Bytecode>cglib....

    4、Slf4jnop1.5.8.jar

    二、copy配置文件:hibernate.cfg.xml放入src

    project>etc>hibernate.cfg.xml

    并将其改成如下:

    <hibernate-configuration>

    <session-factory>

    <property name="current_session_context_class">thread</property>//改成sf.getcurrentSession()后应该加的属性

    //自动生成的mysql驱动

    自动生成如下代码:

    <property name="myeclipse.connection.profile">mysql</property>

    <property name="hibernate.connection.url">

    jdbc:mysql:///test

    </property>

    <property name="hibernate.connection.username">root</property>

    <property name="hibernate.connection.password">root</property>

    <property name="hibernate.connection.driver_class">

    com.mysql.jdbc.Driver

    </property>

    <property name="hibernate.dialect">

    org.hibernate.dialect.MySQLDialect//此处mysql后加5速度会更快

    </property>

     

    自动生成如下代码:

    <property name="show_sql">true</property>

    <property name="format_sql">true</property>

    <property name="hbm2ddl.auto">update</property>//自动生成表

     

    自动生成代码如下:

    <mapping resource="com/ecjtu/po/User.hbm.xml" />//copyUser.hbm.xml在mapping中配置

     

    </session-factory>

    </hibernate-configuration>

    三、写po,与pojo的写法一样

    四、Copy 文件Item.hbm.xml放入po所在的包下

    Project>testing>..........>test>cache

    之后将其名字改成和相应的po相关的名字,如:User.hbm.xml

    五、Class

    package com.ecjtu.hibernate;

    import org.hibernate.cfg.Configuration;

    import org.hibernate.tool.hbm2ddl.SchemaExport;

    public class Test {

    public static void main(String[] args) {

    Configuration cfg=new Configuration().configure();

    SchemaExport ex=new SchemaExport(cfg);

    ex.create(true, true);

    }

    }

    六、测试类,自己写,不用继承。

    package com.ecjtu.hibernateTest;

    import java.util.List;

    import org.hibernate.HibernateException;

    import org.hibernate.Query;

    import org.hibernate.Session;

    import org.hibernate.SessionFactory;

    import org.hibernate.Transaction;

    import org.hibernate.cfg.Configuration;

    import org.junit.Test;

     

    import com.ecjtu.po.User;

    import com.ecjtu.util.HibernateUtil;

    public class HibernateTest {

      @Test

      public void hibernate(){

    Configuration cfg=new Configuration().configure();

    SessionFactory sf=cfg.buildSessionFactory();

    Session session=HibernateUtil.getSession(sf);

    Transaction ts=session.getTransaction();

    try {

    ts.begin();

    User user=new User();

    user.setUsername("jack");

    user.setPassword("123456");

    user.setAge(20);//瞬时状态

    session.save(user);//持久化状态

    //User user=(User)session.load(User.class,1);

    //User user=(User)session.get(User.class,1);

    //getload的区别:

    Get马上发出SQL语句,返回的是真实的对象,对象中的属性都有值,load不会马上发出SQL语句,返回的是代理对象,当正真使用这个对象是才发出SQL语句,load是使用的技术叫lazy(懒加载、延迟加载)

    ts.commit();

    //离线状态

    } catch (HibernateException e) {

    ts.rollback();

    e.printStackTrace();

    }

      }

    }

     

  • 相关阅读:
    蓝桥杯_基础_杨辉三角
    蓝桥杯_基础_数组特征
    蓝桥杯_基础_美丽的图形
    脉象
    词根汇总
    蓝桥杯 入门训练 Fibonacci数列 解析
    制作tomcat重启.bat文件
    day23(023-递归练习)
    day27(027-反射&JDK新特性)
    day25(025-多线程(下)&GUI)
  • 原文地址:https://www.cnblogs.com/stting/p/2751952.html
Copyright © 2011-2022 走看看