Hibernate入门

1。导包:
这里写图片描述
2。实体类:Person.java文件:

package com.itheima.domain;
import java.io.Serializable;
/*
create database day22;
use day22;
create table PERSONS(
    ID int primary key,
    NAME varchar(255)
);
 */
public class Person implements Serializable {
    private Integer id;
    private String name;
    public Person() {
        super();
    }
    public Person(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

3。映射文件:Person.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 package="com.itheima.domain">
    <class name="Person" table="PERSONS">
        <id name="id" column="ID">
            <generator class="native"></generator>
        </id>
        <property name="name" column="NAME"></property>
    </class>
</hibernate-mapping>

4。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>
        <!-- 配置Hibernate:属性配置参考  Hibernate发型包\project\etc\hibernate.properties -->
        <!-- JDBC的基本链接 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/day22</property>
        <!-- 配置数据库方言 -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- 根据映射产生表结构的类型:
            create-drop:木有表结构创建,下次启动时删除重新创建。适合学习阶段
            create:只做创建
            update:探测表结构够的变化,对于数据库没有的,进行更新操作。适合学习阶段
            validate:对比与数据库的结构
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 自动开启事务 -->
<!--        <property name="hibernate.connection.autocommit">true</property> -->
        <!-- 显示sql语句及格式:开发调试阶段非常有用 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 告知映射文件 -->
        <mapping resource="com/itheima/domain/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

5。获取session对象的工具类,HibernateUtil.java文件:

package com.itheima.util;

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

public class HibernateUtil {
    private static SessionFactory sessionFactory;
    static {
        Configuration cfg = new Configuration().configure();
        sessionFactory = cfg.buildSessionFactory();
    }

    public static Session getSession(){
        return sessionFactory.openSession();
    }
}

6。测试类,CRUDDemo.java文件:

package com.itheima.test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.itheima.domain.Person;
import com.itheima.util.HibernateUtil;

public class CRUDDemo {
    @Test
    public void testAdd(){
        Person p = new Person();
        p.setId(1);
        p.setName("杨洋");

        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.save(p);
        tx.commit();
        s.close();
    }
    @Test
    public void testFind(){
        Session s = HibernateUtil.getSession();
        Person p = (Person) s.get(Person.class, 1);
        System.out.println(p);
        s.close();
    }
    @Test
    public void testUpdate(){
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        Person p = (Person) s.get(Person.class, 1);
        p.setName("范青霞");
        s.update(p);
        tx.commit();
        s.close();
    }
    @Test
    public void testDelete(){
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        Person p = (Person) s.get(Person.class, 1);
        s.delete(p);
        tx.commit();
        s.close();
    }
}

7。测试结果:

log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: insert into PERSONS (NAME) values (?)
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select person0_.ID as ID0_0_, person0_.NAME as NAME0_0_ from PERSONS person0_ where person0_.ID=?
com.itheima.domain.Person@16c9b8c
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select person0_.ID as ID0_0_, person0_.NAME as NAME0_0_ from PERSONS person0_ where person0_.ID=?
Hibernate: update PERSONS set NAME=? where ID=?
log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select person0_.ID as ID0_0_, person0_.NAME as NAME0_0_ from PERSONS person0_ where person0_.ID=?
Hibernate: delete from PERSONS where ID=?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值