origin article:http://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide

required jar files: 
hibernate: http://www.javatpoint.com/src/hb/hibernatejar.zip
mysql connector Jar:  https://files.cnblogs.com/files/netact/mysql-connector-java-5.1.36.zip

1,create a java project in eclipse.

2, add package:com.javatpoint.mypackage and Employee class:

 1 package com.javatpoint.mypackage;
2
3 public class Employee {
4
5 private int id;
6 private String firstName, lastName;
7
8 public int getId() {
9 return id;
10 }
11
12 public void setId(int id) {
13 this.id = id;
14 }
15
16 public String getFirstName() {
17 return firstName;
18 }
19
20 public void setFirstName(String firstName) {
21 this.firstName = firstName;
22 }
23
24 public String getLastName() {
25 return lastName;
26 }
27
28 public void setLastName(String lastName) {
29 this.lastName = lastName;
30 }
31
32 }

Employee.java

3,add hibernate.cfg.xml  , please change the parameters in this file:

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6 <hibernate-configuration>
7 <session-factory>
8 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
9 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
10 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
11 <property name="hibernate.connection.username">hp.wang</property>
12 <property name="hibernate.connection.password">123a</property>
13 <mapping resource="Employee.hbm.xml" />
14 </session-factory>
15 </hibernate-configuration>

hibernate.cfg.xml

4, add employee.hbm.xml file,

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5
6 <hibernate-mapping>
7 <class name="com.javatpoint.mypackage.Employee" table="employee">
8 <id name="id">
9 <generator class="assigned"></generator>
10 </id>
11
12 <property name="firstName"></property>
13 <property name="lastName"></property>
14
15 </class>
16
17 </hibernate-mapping>

employee.hbm.xml

5, add the class with main():

 1 package com.javatpoint.mypackage;
2
3 import java.util.Random;
4
5 import org.hibernate.Session;
6 import org.hibernate.SessionFactory;
7 import org.hibernate.Transaction;
8 import org.hibernate.cfg.Configuration;
9
10 public class StoreData {
11 public static void main(String[] args) {
12
13 // creating configuration object
14 Configuration cfg = new Configuration();
15 cfg.configure("hibernate.cfg.xml");// populates the data of the
16 // configuration file
17
18 // creating seession factory object
19 SessionFactory factory = cfg.buildSessionFactory();
20
21 // creating session object
22 Session session = factory.openSession();
23
24 // creating transaction object
25 Transaction t = session.beginTransaction();
26
27 Employee e1 = new Employee();
28 Random rd = new Random();
29 int id = rd.nextInt(50000) + 1;
30 e1.setId(id);
31 e1.setFirstName("firstname"+id);
32 e1.setLastName("lastname"+id);
33
34 session.persist(e1);// persisting the object
35
36 t.commit();// transaction is committed
37 session.close();
38
39 System.out.println("successfully saved");
40
41 }
42 }

StoreData.java

6, create db and table in mysql:

1 CREATE TABLE `employee` (
2 `id` int(11) NOT NULL,
3 `firstName` varchar(50) DEFAULT NULL,
4 `lastName` varchar(50) DEFAULT NULL,
5 PRIMARY KEY (`id`)
6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

create table

7, run the application.

SQL Dialects in Hibernate

For connecting any hibernate application with the database, you must specify the SQL dialects. There are many Dialects classes defined for RDBMS in the org.hibernate.dialect package. They are as follows:

RDBMS Dialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle9i org.hibernate.dialect.Oracle9iDialect
Oracle10g org.hibernate.dialect.Oracle10gDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Ingres org.hibernate.dialect.IngresDialect
Progress org.hibernate.dialect.ProgressDialect
Mckoi SQL org.hibernate.dialect.MckoiDialect
Interbase org.hibernate.dialect.InterbaseDialect
Pointbase org.hibernate.dialect.PointbaseDialect
FrontBase org.hibernate.dialect.FrontbaseDialect
Firebird org.hibernate.dialect.FirebirdDialect

..

最新文章

  1. 【LeetCode OJ】Maximum Depth of Binary Tree
  2. 一、Owin Identity的使用
  3. 连连看beta发布
  4. Android SDK版本和ADT版本
  5. My First Django Project - &lt;Django + MySQL + Ajax&gt; (1)
  6. Java关键字介绍之this与super
  7. HDU 4121 Xiangqi (算是模拟吧)
  8. 三种LVS负载均衡技术的优缺点----负载均衡调度算法
  9. hdu4542 &amp;&amp; ZOJ2562(反素数)
  10. mysql varcahr转int类型
  11. Java 技术笔记
  12. Excel下拉选项二级联动
  13. 参加公司工作总结会要准备的内容 IT 技术部
  14. Jenkins权限分配
  15. LVS简介与使用
  16. selenium:chromedriver与chrome版本对应关系
  17. 算法相关——Java排序算法之冒泡排序(二)
  18. 使用Jenkins构建、部署spring boot项目
  19. mui封装做好的手机版网站为apk
  20. 怎么在移动端模拟pc端进行web开发调试日志

热门文章

  1. Stanford NLP 在Python环境中安装、介绍及使用
  2. Mysql 原生语句
  3. xmind某一天突然打不开了
  4. 注意注意!!!!关于keil的问题,调试时候的bug
  5. 用keil调试程序的时候,一点击调试就弹出STARTUP.A51那个窗口,解决办法
  6. Spring基本概述
  7. Java中静态方法和实例方法
  8. Zabbix3.4 安装配置
  9. CCIE DC Multicast Part 2.
  10. RN 报错信息总结