hibernate入门程序

快速入门

      1. 下载Hibernate框架的开发包

      2. 编写数据库和表结构

Create database hibernate_day01;

Use hibernate_day01;

CREATE TABLE `cst_customer` (

  `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘,

  `cust_name` varchar(32) NOT NULL COMMENT ‘客户名称(公司名称)‘,

  `cust_user_id` bigint(32) DEFAULT NULL COMMENT ‘负责人id‘,

  `cust_create_id` bigint(32) DEFAULT NULL COMMENT ‘创建人id‘,

  `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=94 DEFAULT CHARSET=utf8;

  3. 创建WEB的项目,导入了开发的jar包

           * MySQL驱动包、Hibernate开发的必须要有的jar包、日志的jar包

  4. 编写JavaBean,以后不使用基本数据类型,使用包装类

      5. 编写映射的配置文件(核心),先导入开发的约束,里面正常配置标签

      6. 编写hibernate的核心的配置文件,里面的内容是固定的

      7. 编写代码,使用的类和方法   

需要jar包的,留下邮箱!

原始代码分享

 1 package com.itheima.doman;
 2 /**
 3  * Javabean
 4  * @author Administrator
 5  *
 6  */
 7 public class Customer {
 8
 9     //使用包装类,默认值是null
10     private Long cust_id;
11     private String cust_name;
12     private Long  cust_user_id;
13     private Long  cust_create_id;
14
15     private String  cust_source;
16     private String  cust_industry;
17     private String  cust_level;
18     private String  cust_linkman;
19     private String  cust_phone;
20     private String  cust_mobile;
21     public Long getCust_id() {
22         return cust_id;
23     }
24     public void setCust_id(Long cust_id) {
25         this.cust_id = cust_id;
26     }
27     public String getCust_name() {
28         return cust_name;
29     }
30     public void setCust_name(String cust_name) {
31         this.cust_name = cust_name;
32     }
33     public Long getCust_user_id() {
34         return cust_user_id;
35     }
36     public void setCust_user_id(Long cust_user_id) {
37         this.cust_user_id = cust_user_id;
38     }
39     public Long getCust_create_id() {
40         return cust_create_id;
41     }
42     public void setCust_create_id(Long cust_create_id) {
43         this.cust_create_id = cust_create_id;
44     }
45     public String getCust_source() {
46         return cust_source;
47     }
48     public void setCust_source(String cust_source) {
49         this.cust_source = cust_source;
50     }
51     public String getCust_industry() {
52         return cust_industry;
53     }
54     public void setCust_industry(String cust_industry) {
55         this.cust_industry = cust_industry;
56     }
57     public String getCust_level() {
58         return cust_level;
59     }
60     public void setCust_level(String cust_level) {
61         this.cust_level = cust_level;
62     }
63     public String getCust_linkman() {
64         return cust_linkman;
65     }
66     public void setCust_linkman(String cust_linkman) {
67         this.cust_linkman = cust_linkman;
68     }
69     public String getCust_phone() {
70         return cust_phone;
71     }
72     public void setCust_phone(String cust_phone) {
73         this.cust_phone = cust_phone;
74     }
75     public String getCust_mobile() {
76         return cust_mobile;
77     }
78     public void setCust_mobile(String cust_mobile) {
79         this.cust_mobile = cust_mobile;
80     }
81
82
83 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5
 6 <hibernate-mapping>
 7
 8 <!--配置类和表结构的映射-->
 9     <class name="com.itheima.doman.Customer" table="cst_customer">
10         <!-- 配置id
11               见到name属性,JavaBean属性
12               见到column属性,是表结构的字段
13          -->
14          <id name="cust_id" column="cust_id">
15              <!-- 主键的生成策略 -->
16              <generator class="native"/>
17          </id>
18     <!-- 配置其他属性 -->
19         <property name="cust_name" column="cust_name"/>
20         <property name="cust_user_id" column="cust_user_id"/>
21         <property name="cust_create_id" column="cust_create_id"/>
22         <property name="cust_source" column="cust_source"/>
23         <property name="cust_industry" column="cust_industry"/>
24         <property name="cust_level" column="cust_level"/>
25         <property name="cust_linkman" column="cust_linkman"/>
26         <property name="cust_phone" column="cust_phone"/>
27         <property name="cust_mobile" column="cust_mobile"/>
28
29
30 </class>
31 </hibernate-mapping>
<?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>
    <!-- 先配置sessionFactory标签,一个数据库对应一个SessionFactory标签 -->
    <session-factory>

    <!-- 必须配置的参数5个,4大参数,数据库方言 -->
    <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.username">root</property>
    <property name="hibernate.connection.password">root</property>

    <!-- 数据库方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- 可选配置 -->

    <!-- 映射配置文件,需要引入的映射配置文件 -->
    <mapping resource="com/itheima/doman/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
package com.itheima.test;

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

import com.itheima.doman.Customer;

/**
 * 测试hibernate框架
 *
 * @author Administrator
 *
 */

public class Demo1 {
    /*
     * 测试保存客户
     */
    @Test
    public void testSave() {
        /*
         * 1.先加载配置文件
         * 2.创建SessionFactory对象,生成Session对象
         * 3.创建Session对象
         * 4.开启事物
         * 5.编写保存代码
         * 6.提交事务
         * 7.释放资源
         */
        //1.加载配置文件
        Configuration config=new Configuration();
        //默认加载hibernate.cfg.xml配置文件
        config.configure();
        //创建SessionFactory对象
        SessionFactory factory=config.buildSessionFactory();
        //创建session对象
        Session session=factory.openSession();

        //开启事务
        Transaction tr=session.beginTransaction();

        //编写保存代码
        Customer c=new Customer();
        c.setCust_name("测试");
        c.setCust_phone("110");
        c.setCust_level("1");

        session.save(c);

        //提交事务
        tr.commit();
        //释放资源
        session.close();
        factory.close();
    }
}

 

时间: 2024-08-11 01:33:46

hibernate入门程序的相关文章

学习hibernate hibernate入门程序

In this tutorial you will see how to persist the java objects using the Hibernate Object/Relational Mapping (ORM) framework. Hibernate automates ORM and considerably reduces the number of lines of code needed to persist the object in the database. Th

Hibernate入门(一)

一 Hibernate介绍 Hibernate 是一个开源.轻量级的ORM(对象关系映射)工具,该工具简化了数据创建.数据处理和数据访问,它是一种将对象映射到数据库中表的编程技术.ORM工具内部使用JDBC API与数据库进行交互. Hibernate 的优点有:开源.轻量级.快速性能.数据库独立查询.自动创建表.简化复杂查询.提供查询统计和数据库状态. 二 入门程序 准备JAR包 hibernate包和mysql驱动连接包: 创建项目,导入JAR包 3.在src路径下新建hibernate.c

史上最简单的Hibernate入门简单介绍

事实上Hibernate本身是个独立的框架,它不须要不论什么web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了非常多非Hibernate的东西,比方: Tomcat, Eclipse, Log4J,Struts, XDoclet, 甚至JBoss.这easy让人产生Hibernate复杂难懂的误解,特别是打击了刚開始学习的人的积极性. 在这篇文章将不涉及Eclipse, log4j, Struts, Tomcat, XDoclet,

MyBatis 介绍、简单入门程序

JDBC 编程中的问题 1. 将 SQL 语句硬编码到 Java 代码.不利于系统维护. 设想怎样解决:将SQL单独抽取出来,在配置文件(xml方式.properties文件)进行配置. 2. 数据库连接不能反复利用,对数据库资源是一中浪费. 设想怎样解决:使用数据库连接池管理数据库连接. 3. 向 Statement 设置參数时,对于參数的位置通过硬编码指定,不利于系统维护. 设想怎样解决:是否可以自己主动将 Java 对象的值设置到 Statement. 4. 遍历结果集.resultSet

【转】Hibernate入门实例

1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate ORM,可以下载最新的hibernate,还有配套的document教程 http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/ .下载到的hibernate文件夹中有document文档(hibernate\documenta

Hibernate入门篇——第一个Hibernate应用

Hibernate入门 第一步: 挑选jar包 在工程下新建lib文件夹,然后将Hibernate基本包复制进去.可以采用Maven来获取这些包: <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.7.Final</version> </dependency> 也可

第四章 Hibernate入门

第四章 Hibernate入门4.1 框架技术    4.1.1 框架的概念        框架是一个提供了可重用的公共结构的半成品.    4.1.2 主流框架        4.1.2.1 Struts框架        4.1.2.2 Struts2框架        4.1.2.3 Hibernate框架            一个优秀的持久化框架,负责简化对象数据保存到数据库中,或从数据库中读取数据并封装到对象的工作.        4.1.2.4 Spring框架4.2 Hibern

Hibernate入门案例及增删改查

一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private Integer age; private String name; public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public Integer getAge() {

Hibernate入门_增删改查

一.Hibernate入门案例剖析:  ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private Integer age; private String name; public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public Integer getAge()