码迷,mamicode.com
首页 > Web开发 > 详细

hibernate框架的搭建

时间:2018-03-01 00:40:35      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:ali   nfa   weight   inf   ddl   png   type   mat   移动电话   

1 导入所需的jar包

1 导入hibernate必须的jar包

技术分享图片

2 导入驱动包

技术分享图片

2 创建数据库,准备表,实体

1 创建hibernate数据库

CREATE DATABASE hibernate;

2 创建cst_customer表

CREATE TABLE `cst_customer` (
  `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT 客户编号(主键),
  `cust_name` VARCHAR(32) NOT NULL COMMENT 客户名称(公司名称),
  `cust_source` VARCHAR(32) DEFAULT NULL COMMENT 客户信息来源,
  `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT 客户所属行业,
  `cust_level` VARCHAR(32) DEFAULT NULL COMMENT 客户级别,
  `cust_linkman` VARCHAR(64) DEFAULT NULL COMMENT 联系人,
  `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT 固定电话,
  `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT 移动电话,
  PRIMARY KEY (`cust_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

3 Customer实体

package www.test.domain;

public class Customer {
    
    /*
     * CREATE TABLE `cst_customer` (
      `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘,
      `cust_name` VARCHAR(32) NOT NULL COMMENT ‘客户名称(公司名称)‘,
      `cust_source` VARCHAR(32) DEFAULT NULL COMMENT ‘客户信息来源‘,
      `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT ‘客户所属行业‘,
      `cust_level` VARCHAR(32) DEFAULT NULL COMMENT ‘客户级别‘,
      `cust_linkman` VARCHAR(64) DEFAULT NULL COMMENT ‘联系人‘,
      `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT ‘固定电话‘,
      `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT ‘移动电话‘,
      PRIMARY KEY (`cust_id`)
    ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
     */
    private Long cust_id;
    
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_linkman;
    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_linkman() {
        return cust_linkman;
    }
    public void setCust_linkman(String cust_linkman) {
        this.cust_linkman = cust_linkman;
    }
    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;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
    }
}

3 书写orm元数据(对象与表的映射配置文件)

技术分享图片

技术分享图片
<?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>
    <class name="www.test.domain.Customer" table="cst_customer">
        <id name="cust_id" column="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_linkman" column="cust_linkman"></property>
        <property name="cust_phone" column="cust_phone"></property>
        <property name="cust_mobile" column="cust_mobile"></property>
    </class>
</hibernate-mapping>
View Code

1 导入约束

技术分享图片

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

2 实体

package www.test.domain;

public class Customer {
    
    /*
     * CREATE TABLE `cst_customer` (
      `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘,
      `cust_name` VARCHAR(32) NOT NULL COMMENT ‘客户名称(公司名称)‘,
      `cust_source` VARCHAR(32) DEFAULT NULL COMMENT ‘客户信息来源‘,
      `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT ‘客户所属行业‘,
      `cust_level` VARCHAR(32) DEFAULT NULL COMMENT ‘客户级别‘,
      `cust_linkman` VARCHAR(64) DEFAULT NULL COMMENT ‘联系人‘,
      `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT ‘固定电话‘,
      `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT ‘移动电话‘,
      PRIMARY KEY (`cust_id`)
    ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
     */
    private Long cust_id;
    
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_linkman;
    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_linkman() {
        return cust_linkman;
    }
    public void setCust_linkman(String cust_linkman) {
        this.cust_linkman = cust_linkman;
    }
    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;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
    }
    
}

3 orm元数据

<!-- 配置表与实体的关系 -->
<hibernate-mapping>
    <class name="www.test.domain.Customer" table="cst_customer">
        <id name="cust_id" column="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_linkman" column="cust_linkman"></property>
        <property name="cust_phone" column="cust_phone"></property>
        <property name="cust_mobile" column="cust_mobile"></property>
    </class>
</hibernate-mapping>

4.书写主配置文件

技术分享图片

技术分享图片
<?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.dialect org.hibernate.dialect.MySQLDialect
        #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
        #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
        #hibernate.connection.driver_class com.mysql.jdbc.Driver
        #hibernate.connection.url jdbc:mysql:///test
        #hibernate.connection.username gavin
        #hibernate.connection.password 
        -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- /// 表示连接本机 -->
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
        
        <!-- 
        #hibernate.show_sql true  //打印生成的sql
        #hibernate.format_sql true  //格式化sql 不格式都会在一行
         -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        
        <!-- 
        ## auto schema export

        #hibernate.hbm2ddl.auto create-drop
        #hibernate.hbm2ddl.auto create
        #hibernate.hbm2ddl.auto update
        #hibernate.hbm2ddl.auto validate
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <mapping resource="www/test/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
View Code

1 第一部分

<!-- 
        #hibernate.dialect org.hibernate.dialect.MySQLDialect
        #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
        #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
        #hibernate.connection.driver_class com.mysql.jdbc.Driver
        #hibernate.connection.url jdbc:mysql:///test
        #hibernate.connection.username gavin
        #hibernate.connection.password 
        -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- /// 表示连接本机 -->
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>

2 第二部分

<!-- 
        #hibernate.show_sql true  //打印生成的sql
        #hibernate.format_sql true  //格式化sql 不格式都会在一行
         -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        
        <!-- 
        ## auto schema export

        #hibernate.hbm2ddl.auto create-drop
        #hibernate.hbm2ddl.auto create
        #hibernate.hbm2ddl.auto update
        #hibernate.hbm2ddl.auto validate
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>

3 第三部分

<mapping resource="www/test/domain/Customer.hbm.xml"/>

5.书写代码测试

package www.test.a_hello;

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

import www.test.domain.Customer;

// 测试hibernate 框架
public class Demo {

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

    }
}

 

hibernate框架的搭建

标签:ali   nfa   weight   inf   ddl   png   type   mat   移动电话   

原文地址:https://www.cnblogs.com/jepson6669/p/8486056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!