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

hibernate入门程序

时间:2017-08-26 12:53:39      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:类型   映射   utf8   log   database   主键   分享   ble   其他   

快速入门

      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();
    }
}

 

 

                    

hibernate入门程序

标签:类型   映射   utf8   log   database   主键   分享   ble   其他   

原文地址:http://www.cnblogs.com/ncl-960301-success/p/7434775.html

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