美文网首页
Hibernate 入门案例

Hibernate 入门案例

作者: 年少懵懂丶流年梦 | 来源:发表于2017-02-24 16:17 被阅读19次

1、导入jar包

  • hibernate 核心jar包。

2、创建hibernate.cfg.xml配置文件

  • 在核心配置文件中引入dtd约束
<?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/student
        </property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">111111</property>
        <!-- 配置数据库的方言 比如在mysql里面有关键字 limit ,只能使用在mysql里面 
            让hibernate识别到不同数据库自己特有的语句 -->
        <property name="hibernate.dialect">
            org.hibernate.dialect.MySQLDialect
        </property>

        <!-- 配置hibernate的相关信息 -->
        <!-- 是否显示底层的sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 是否格式化sql语句 -->
        <property name="hibernate.format_sql">true</property>
        <!-- hibernate会帮自己创建数据库表,默认不会创建,需要配置。 
            值 update: 如果数据库里面不存在表,创建;如果已经存在,更新 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <!-- 配置事务自动提交 -->
        <!-- <property name="hibernate.connection.autocommit">true</property> -->

        <!-- 引入映射配置文件 -->
        <mapping resource="com/meng/entity/Users.hbm.xml" />
    </session-factory>
</hibernate-configuration>

3、编写实体类Users

public class Users {

    private int uid;
    private String username;
    private String password;

    public Users() {

    }
    
    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Users [uid=" + uid + ", username=" + username + ", password=" + password + "]";
    }
    
}

4、创建实体映射配置文件Users.hbm.xml

  • 映入dtd约束
<?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>
    <!-- 配置实体类名称和表名称对应 name:实体类全路径 table:表名称 -->
    <class name="com.meng.entity.Users" table="users">
        <!-- 在hibernate中要求每个表有一个唯一标识 -->
        <id name="uid" column="uid">
            <generator class="identity"></generator>
        </id>
        <!-- 配置其他的属性和其他字段映射 name: 实体类属性名称 column: 表里面字段名称 细节问题: 
            1 propery里面有 type属性,指定字段类型,但是可以省略 
            2 column属性,实体类属性名称和字段名称一样,column可以省略 -->
        <property name="username" column="username"></property>
        <property name="password" column="password"></property>
    </class>
</hibernate-mapping>   

5、在hibernate.cfg.xml中引入映射配置文件

 <!-- 引入映射配置文件 -->
<mapping resource="com/meng/entity/Users.hbm.xml" />

6、创建Test测试文件

  • 使用hibernate代码实现crud操作,步骤是固定
    第一步 加载核心配置文件
    第二步 创建sessionFactory
    第三步 使用sessionFactory创建session
    第四步 开启事务
    第五步 写具体逻辑(crud)
    第六步 提交事务
    第七步 关闭资源
public class TestDemo1 {
    // 实现添加操作
    @Test
    public void add() {
        // 创建配置对象(加载核心配置文件)
        Configuration config = new Configuration().configure();
        // 创建服务注册对象
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
                    applySettings(config.getProperties()).build();
        // 创建sessionFactory
        SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
        // 打开session
        Session session = sessionFactory.openSession();
        // 开启事务
        Transaction tx = session.beginTransaction();
        
        // 写添加逻辑
        // 项数据库设置值
        Users user = new Users();
        user.setUsername("zhangsan");
        user.setPassword("123456");
        // 调用session里面的方法 save方法,保存对象进入数据库
        session.save(user);
        
        // 提交事务
        tx.commit();
        // 关闭连接
        session.close();
        sessionFactory.close();
    }
    
}

相关文章

网友评论

      本文标题:Hibernate 入门案例

      本文链接:https://www.haomeiwen.com/subject/htkhwttx.html