用hibernate往数据库建表存数据

用hibernate往数据库建表存数据编写之前必须在数据库中 建立一个数据库

1、编写model user

import java.util.Date;

public class User {
    private String userId;
    private String userName;
    private int age;
    private int gender;
    private Date birthday;
    private String address;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

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

2、编写映射文件 User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="model">
    <class name="model.User" table="user">
        <id name="userId" column="user_id">
            <generator class="uuid"/>
        </id>
        <property name="userName" column="user_name"/>
        <property name="age" column="age"/>
        <property name="gender" column="gender"/>
        <property name="birthday" column="birthday"/>
        <property name="address" column="address"/>
    </class>
</hibernate-mapping>

3、配置hibernate核心配置文件 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_practise?characterEncoding=UTF-8</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <!--<property name="hibernate.hbm2ddl.auto">create</property>-->
    <!-- C3P0 connection pool (use the built-in) -->
    <!--最小连接数-->
    <property name="c3p0.min_size">10</property>
    <!--最大连接数-->
    <property name="c3p0.max_size">100</property>
    <!--最大等待时间-->
    <property name="c3p0.timeout">3000</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <!--<property name="current_session_context_class">thread</property>-->

    <!-- Disable the second-level cache  -->
    <!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>-->

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <!--<property name="hbm2ddl.auto">update</property>-->

    <mapping resource="model/Team.hbm.xml"/>
    <mapping resource="model/Emp.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

注:配置核心配置文件时,四个基本属性 连接数 方言 必须有

4、编写java运行类

package utils;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class DBExport {
    public static void main(String[] args) {
        //hibernate配置对象
        Configuration cfg = new Configuration();
        //配置核心配置文件使之生效
        cfg.configure("hibernate.cfg.xml");
        //创建表的对象
        SchemaExport se = new SchemaExport(cfg);
        //开始创建表
        se.create(true,true);
    }
}

至此建表环节结束

开始往表中添加数据

建立java测试类 TestHibernate.java

package hibernate.test;

import model.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test;

import java.util.Date;

public class TestHibernateTest {
    @Test
    public void test() {
        //获取hibernate配置对象
        Configuration cfg = new Configuration();
        //注册核心配置文件(必须有)
        cfg.configure("hibernate.cfg.xml");
        //注册属性信息
        ServiceRegistry sr=new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
        //获得sessoion工厂
        SessionFactory sessionFactory = cfg.buildSessionFactory(sr);
        //获得session
        Session session = sessionFactory.openSession();
        //开启事务
        Transaction ts = session.beginTransaction();
        //创建对象
        User user = new User();
        //使用uuid进行设置主键
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        user.setUserId(s);
        user.setUserName("张豆豆");
        user.setAge(22);
        user.setGender(1);
        Date date = new Date(1997-1900,06-1,07);
        user.setBirthday(date);
        user.setAddress("天水");
        //使用session保存对象
        session.save(user);

        //提交事务
        ts.commit();
        //关闭session
        session.close();
    }
}

至此结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值