第一步:创建普通的Java工程用Main方法测试工程创建完成后在Mysql中新建一张名称叫XuXuDan的表。代码如下:
C:\Documents and Settings\Administrator>mysql -uroot -proot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.15 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'datab
ase' at line 1
2、查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hibernate |
| hibernates |
| mysql |
| performance_schema |
| test |
| xqwy |
+--------------------+
7 rows in set (0.02 sec)
3、使用已经存在的数据库
mysql> use hibernate;
Database changed
mysql> show tables;
+---------------------+
| Tables_in_hibernate |
+---------------------+
| students |
| teacher |
+---------------------+
2 rows in set (0.05 sec)
4、创建名称为XuXuDan的表
mysql> create table XuXuDan(id int primary key auto_increment,name varchar(20),s
ex varchar(20),age int);
第二步、编写Modle类实现序列化接口类的成员变量和数据库的字段和数据类型一一对应代码如下
package com.ygc.hibernate.modle;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="XuXuDan") //注解说明表名
public class XuXuDan implements Serializable {
private int id;
private String name;
private String sex;
private int age;
//主键id
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
第三步编写测试类代码如下
package com.ygc.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ygc.hibernate.modle.XuXuDan;
import com.ygc.hibernate.util.HibernateUtils;
public class XuXuDanTest {
/**
* @param args
*/
public static void main(String[] args) {
XuXuDan xuDan = new XuXuDan();
xuDan.setName("徐旭丹");
xuDan.setSex("女");
xuDan.setAge(24);
SessionFactory sessionFactory = HibernateUtils.getHibernate().getSessionFactory();
Session session= sessionFactory.openSession();
session.beginTransaction();
session.save(xuDan);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
第四步编写获取SessionFactory的工具类
package com.ygc.hibernate.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
static HibernateUtils instance;
public static HibernateUtils getHibernate(){
if(instance==null){
instance = new HibernateUtils();
}
return instance;
}
public SessionFactory getSessionFactory(){
Configuration configuration = new AnnotationConfiguration().configure();
return configuration.buildSessionFactory();
}
}
第五步:添加hibernate.cfg.xml配置文件代码如下
<?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>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<!--<property name="connection.pool_size">1</property>-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!--<property name="current_session_context_class">thread</property>-->
<!-- Disable the second-level cache -->
<!--<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- <property name="hbm2ddl.auto">create-drop</property>-->
<!-- Drop and re-create the database schema on startup -->
<!--<property name="hbm2ddl.auto">update</property>-->
<!--<mapping resource="com/ygc/hibernate/modle/Students.hbm.xml"/>-->
<mapping class="com.ygc.hibernate.modle.XuXuDan"/>
</session-factory>
</hibernate-configuration>
第六步:运行main方法查看测试结果
结果显示为:
mysql> select * from xuxudan;
+----+--------+------+------+
| id | name | sex | age |
+----+--------+------+------+
| 1 | 徐旭丹 | 女 | 24 |
+----+--------+------+------+
1 row in set (0.00 sec)
源代码下载:Demo下载