java hibernate +mysql demo
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 }
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>
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>
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 }
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;
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 |
..
posted on 2015-09-23 21:41 Henry_Wang 阅读(240) 评论(0) 编辑 收藏 举报