【 hibernate 】基本配置

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:///hibernate_day01</property>
        <property name="hibernate.connection.useUnicode">true</property>
        <property name="hibernate.connection.characterEncoding">UTF-8</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root3306</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>
        <!-- 配置数据库方言
            在mysql里面实现分页 关键字 limit,只能使用mysql里面
            在oracle数据库,实现分页rownum
            让hibernate框架识别不同数据库的自己特有的语句
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <!-- 第三部分: 把映射文件放到核心配置文件中 必须的-->
        <mapping resource="k/entity/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

HibernateUtils.java

package k.utils;

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

public class HibernateUtils {

    static Configuration cfg = null;
    static SessionFactory sessionFactory = null;
    //静态代码块实现
    static {
        //加载核心配置文件
        cfg = new Configuration();
        cfg.configure();
        sessionFactory = cfg.buildSessionFactory();
    }
    
    //提供方法返回sessionFactory
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void main(String[] args) {
        
    }
}

HibernateDemo.java

package k.hibernatetest;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
//import org.hibernate.cfg.Configuration;
import org.junit.Test;

import k.entity.User;
import k.utils.HibernateUtils;

public class HibernateDemo {

    @Test
    public void testAdd() {
//        第一步 加载hibernate核心配置文件
        // 到src下面找到名称是hibernate.cfg.xml
        //在hibernate里面封装对象
//        Configuration cfg = new Configuration();
//        cfg.configure();
        
//        第二步 创建SessionFactory对象
        //读取hibernate核心配置文件内容,创建sessionFactory
        //在过程中,根据映射关系,在配置数据库里面把表创建
//        SessionFactory sessionFactory = cfg.buildSessionFactory();
        
        SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
        
//        第三步 使用SessionFactory创建session对象
        // 类似于连接
        Session session = sessionFactory.openSession();
        
//        第四步 开启事务
        Transaction tx = session.beginTransaction();

//        第五步 写具体逻辑 crud操作
        //添加功能
        User user = new User();
        user.setUsername("小马");
        user.setPassword("1314520");
        user.setAddress("美国");
        //调用session的方法实现添加
        session.save(user);
        
//        第六步 提交事务
        tx.commit();

//        第七步 关闭资源
        session.close();
        sessionFactory.close();
    }
}

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>
    <!-- 1 配置类和表对应 
        class标签
        name属性:实体类全路径
        table属性:数据库表名称
    -->
    <class name="k.entity.User" table="t_user">
        <!-- 2 配置实体类id和表id对应 
            hibernate要求实体类有一个属性唯一值
            hibernate要求表有字段作为唯一值
        -->
        <!-- id标签
            name属性:实体类里面id属性名称
            column属性:生成的表字段名称
         -->
        <id name="uid" column="uid">
            <!-- 设置数据库表id增长策略  native:生成表id值就是主键自动增长  uuid -->
            <generator class="native"></generator>
        </id>
        <!-- 配置其他属性和表字段对应  name属性:实体类属性名称 column属性:生成表字段名称
        -->
        <property name="username" column="username"></property>
        <property name="password" column="password"></property>
        <property name="address" column="address"></property>
    </class>
</hibernate-mapping>

User.java

package k.entity;

public class User {

    /* hibernate要求实体类有一个属性唯一的 */
    private int uid;
    public int getUid() {
        return uid;
    }

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

    private String username;
    private String password;

    
    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;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    private String address;
}
View Code

 

posted @ 2020-02-05 20:30  一只桔子2233  阅读(123)  评论(0编辑  收藏  举报