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

Posted bluedarkni

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>
        <!-- 显示sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!-- 表自动生成,没有此表自动建表 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 设置数据库方言 -->        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 映射类配置文件 -->
        <mapping resource="ni/jun/yang/bean/user.hbm.xml"></mapping>    
    </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<User> selectAll() {
        Session session = sessionFactory.openSession();        
        Transaction ts = session.beginTransaction();
        Query<User>  q=session.createQuery("from User");
//        Query<User>  q=session.createQuery("from User where userName =:userName");//占位符
//        q.setString("userName", "zhangsan"); //占位符赋值
        
        List<User> lists = q.list();
        return lists;
    }

 

以上是关于hibernate入门-基本配置及简单的crud操作的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate 入门------简单的CRUD

hibernate多对多 一对多 及简单入门

hibernate的入门crud

[原创]java WEB学习笔记82:Hibernate学习之路---映射 一对多关联关系,配置,CRUD方法测试及注意点

Hibernate入门

java之hibernate之crud