Radcat

导航

简单hibernate框架测试

-jar包

 

-日志配置文件:

 

-实体类:

package cn.itcast.domain;

public class Customer {
    private Long cust_id;          //客户编号
    private String cust_name;      //客户名称(公司名称)
    private String cust_source;    //客户信息来源
    private String cust_industry;  //客户所属行业
    private String cust_level;     //客户级别
    private String cust_phone;     //固定电话
    private String cust_mobile;    //移动电话
    public Long getCust_id() {
        return cust_id;
    }
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_source() {
        return cust_source;
    }
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
    public String getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
    public String getCust_level() {
        return cust_level;
    }
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    public String getCust_mobile() {
        return cust_mobile;
    }
    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
}

-映射文件:

<?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="cn.itcast.domain" >
    <class name="Customer" table="cst_customer" >
        <id name="cust_id"  >
            <generator class="native"></generator>
        </id>
        <property name="cust_name" column="cust_name" >
        </property>
        <property name="cust_source" column="cust_source" ></property>
        <property name="cust_industry" column="cust_industry" ></property>
        <property name="cust_level" column="cust_level" ></property>
        <property name="cust_phone" column="cust_phone" ></property>
        <property name="cust_mobile" column="cust_mobile" ></property>
    </class>
</hibernate-mapping>

-hibernate配置文件:

<?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">oracle.jdbc.driver.OracleDriver</property>
         <!-- 数据库url -->
        <property name="hibernate.connection.url">jdbc:oracle:thin://localhost:1521/orcl</property>
         <!-- 数据库连接用户名 -->
        <property name="hibernate.connection.username">lpf</property>
         <!-- 数据库连接密码 -->
        <property name="hibernate.connection.password">123456</property>
        <!-- 数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        
     
        <!-- 将hibernate生成的sql语句打印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
        <property name="hibernate.format_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 引入orm元数据
            路径书写: 填写src下的路径
         -->
        <mapping resource="cn/itcast/domain/Customer.hbm.xml" />
        
    </session-factory>
</hibernate-configuration>

-测试类:

package cn.itcast.test;

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

import cn.itcast.domain.Customer;


public class Hibernatedemo1 {

    @Test
    //保存客户
    public void fun1(){
        Configuration conf = new Configuration().configure();
        
        SessionFactory sessionFactory = conf.buildSessionFactory();
        
        Session session = sessionFactory.openSession();
    
        Transaction tx = session.beginTransaction();
        //----------------------------------------------
        Customer c = new Customer();
        c.setCust_name("1234");
        
        session.save(c);//执行保存
        
        //----------------------------------------------
        tx.commit();
        session.close();
        sessionFactory.close();
    }

}

-SQL语句:

Hibernate:
select
hibernate_sequence.nextval
from
dual
Hibernate:
insert
into
cst_customer
(cust_name, cust_source, cust_industry, cust_level, cust_phone, cust_mobile, cust_id)
values
(?, ?, ?, ?, ?, ?, ?)

posted on 2018-10-25 11:26  Radcat  阅读(245)  评论(0编辑  收藏  举报