Getting started with hibernate

Source: Internet
Author: User

Getting started with hibernate
Quick Start

1. Download The Hibernate framework development kit

2. Write the database and Table Structure

Create database hibernate_day01;

Use hibernate_day01;

Create table 'cst _ customer '(

'Cust _ id' bigint (32) not null AUTO_INCREMENT COMMENT 'customer id (primary key )',

'Cust _ name' varchar (32) not null comment 'customer name (company name )',

'Cust _ user_id 'bigint (32) default null comment 'owner id ',

'Cust _ create_id 'bigint (32) default null comment 'creator id ',

'Cust _ source' varchar (32) default null comment 'customer information source ',

'Cust _ industry 'varchar (32) default null comment 'customer industry ',

'Cust _ level' varchar (32) default null comment 'customer level ',

'Cust _ linkman 'varchar (64) default null comment 'contacts ',

'Cust _ phone' varchar (64) default null comment 'landline phone ',

'Cust _ mobile' varchar (16) default null comment 'mobile phone ',

Primary key ('cust _ id ')

) ENGINE = InnoDB AUTO_INCREMENT = 94 default charset = utf8;

 

3. Create a WEB project and import the developed jar package

* The MySQL driver package, the required jar package for Hibernate development, and the log jar package 

 

4. Write a JavaBean file. In the future, the basic data type will not be used, and the packaging class will be used. 

5. Write the ing configuration file (CORE) and import the development constraints first. The labels are configured normally.

6. Compile the core configuration file of hibernate. The content in the file is fixed.

7. write code and use classes and Methods

If you need a jar package, leave your mailbox!

Original code sharing

1 package com. itheima. doman; 2/** 3 * Javabean 4 * @ author Administrator 5*6 */7 public class Customer {8 9 // use the packaging class. The default value is null10 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 
<? 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> <! -- First configure the sessionFactory tag. A database corresponds to a SessionFactory tag --> <session-factory> <! -- Five parameters must be configured, four parameters, database Dialect --> <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> <! -- Database dialect --> <property name = "hibernate. dialect"> org. hibernate. dialect. MySQLDialect </property> <! -- Optional configuration --> <! -- Ing configuration file: The ing configuration file to be introduced --> <mapping resource = "com/itheima/doman/Customer. hbm. xml "/> </session-factory> 
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;/*** Test the hibernate framework ** @ author Administrator **/public class Demo1 {/** Test and save Customer */@ Test public void testSave () {/** 1. load the configuration file first * 2. create the SessionFactory object and generate the Session object * 3. create a Session object * 4. enable things * 5. write and save the Code * 6. commit a transaction * 7. release resources * // 1. load the Configuration file Configuration config = new Configuration (); // hibernate is loaded by default. cfg. xml configuration file config. configure (); // create the SessionFactory object SessionFactory factory = config. buildSessionFactory (); // creates the session object Session session = factory. openSession (); // enable Transaction tr = session. beginTransaction (); // write and save the code Customer c = new Customer (); c. setCust_name ("test"); c. setCust_phone ("110"); c. setCust_level ("1"); session. save (c); // submit the transaction tr. commit (); // release the resource session. close (); factory. close ();}}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.