hibernate入门-基本配置及简单的crud操作

框架来说主要是需要写大量的配置文件,hibernate相比mybatis来说更强大,移植性更好;

1.类和数据库的映射配置:配置文件命名一般——类名.hbm.xml (user.hbm.xml),与实体类放在同一目录下,配置成员变量和数据库字段的映射:

xml version="1.0" encoding="UTF-8"?>
DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="ni.jun.yang.bean.User" table="t_user">
        <id name="userId" column="userId">

            <generator class="native">generator>
        id>        
        <property name="userName" column="userName">property>
        <property name="userPsw" column="userPsw">property>
    class>
hibernate-mapping>

2.主配置文件,放在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>
    
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate1property>
        <property name="hibernate.connection.username">rootproperty>
        <property name="hibernate.connection.password">123property>
                    
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
        
        <property name="hibernate.show_sql">trueproperty>
        
        <property name="connection.isolation">2property>
        <property name="hibernate.format_sql">trueproperty>
        
        <property name="hibernate.hbm2ddl.auto">updateproperty>
        
        
        <property name="hibernate.cache.use_second_level_cache">trueproperty>
        <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactoryproperty>
        
        
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProviderproperty>
        <property name="hibernate.c3p0.max_size">20property>
        <property name="hibernate.c3p0.min_size">5property>
        <property name="hibernate.c3p0.timeout">120property>
        <property name="automaticTestTable">Testproperty>
        <property name="hibernate.c3p0.max_statements">100property>
        <property name="hibernate.c3p0.idle_test_period">120property>
        <property name="hibernate.c3p0.acquire_increment">1property>
        <property name="c3p0.testConnectionOnCheckout">trueproperty>
        <property name="c3p0.idleConnectionTestPeriod">18000property>
        <property name="c3p0.maxIdleTime">25000property>
        <property name="c3p0.idle_test_period">120property>
        
        
        <mapping resource="ni/jun/yang/bean/user.hbm.xml">mapping>
        <mapping resource="ni/jun/yang/bean/Orders.hbm.xml">mapping>
        <mapping resource="ni/jun/yang/bean/Address.hbm.xml">mapping>
        <mapping resource="ni/jun/yang/bean/Customer.hbm.xml">mapping>
        <class-cache    usage="read-write" class="ni.jun.yang.bean.User"/>    
    session-factory>
hibernate-configuration>

 

3.工具类读取配置文件,获取SessionFactory对象:

package ni.jun.yang.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static SessionFactory sessionFactory;

    public HibernateUtil() {
        
    }
    public static SessionFactory getSessionFactory(){
        if(sessionFactory == null){
            Configuration cfg =new Configuration();
            cfg.configure();//读取配置文件
            sessionFactory = cfg.buildSessionFactory();
        }
        return sessionFactory;
    }
    
}

4.session类中提供了一系列的crud操作:

4.1 添加:

public void Add(User user) {
        Session session = sessionFactory.openSession();        
        Transaction ts = session.beginTransaction(); //开启事务
        session.save(user);
        ts.commit();   //提交事务
        session.close();        
    }

4.2 删除:

public void delete(User user) {
        Session session = sessionFactory.openSession();        
        Transaction ts = session.beginTransaction(); //开启事务
        session.delete(user);
        ts.commit(); //提交事务
        session.close();

    }

4.3 修改:

public void update(User user) {
        Session session = sessionFactory.openSession();        
        Transaction ts = session.beginTransaction();//开启事务
        session.update(user);
        ts.commit(); //提交事务
        session.close();

    }

4.4 查询:

public User select(int id) {
        Session session = sessionFactory.openSession();        
        Transaction ts = session.beginTransaction();
        User user = session.get(User.class, id);        
        user.setUserPsw("321");
        ts.commit();
        session.close();
        return user;
    }

4.5 查询所有,结合hql语句

public List selectAll() {
        Session session = sessionFactory.openSession();        
        Transaction ts = session.beginTransaction();
        Query  q=session.createQuery("from User");
//        Query  q=session.createQuery("from User where userName =:userName");//占位符
//        q.setString("userName", "zhangsan"); //占位符赋值
        
        List lists = q.list();
        return lists;
    }

 

你可能感兴趣的:(hibernate入门-基本配置及简单的crud操作)