这是一个简单的例子
1. 在mySQL中建立一张USER表。
CREATE TABLE USER (
user_id CHAR(32) NOT NULL PRIMARY KEY,
name VARCHAR(16) NOT NULL,
sex CHAR(1),
age INT
);
2. 建立一个pojo
package com.xy;
public class User {
private String id;
private String name;
private char sex;
private int age;
public int getAge() {
return age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public char getSex() {
return sex;
}
public void setAge(int i) {
age = i;
}
public void setId(String string) {
id = string;
}
public void setName(String string) {
name = string;
}
public void setSex(char c) {
sex = c;
}
}
3. hibernate.cfg.xml文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 显示实际操作数据库时的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,这里设定的是MySQL -->
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驱动程式 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/jiejie</property>
<!-- 用户名 -->
<property name="connection.username">root</property>
<!-- 密码-->
<property name="connection.password"></property>
<!-- 映射文件 -->
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4. User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.xy.User" table="USER">
<id name="id" type="string" unsaved-value="null">
<column name="user_id" sql-type="char(32)" />
<generator class="uuid.hex"/>
</id>
<property name="name" type="string" not-null="true">
<column name="name" length="16" not-null="true"/>
</property>
<property name="sex" type="char"/>
<property name="age" type="int"/>
</class>
</hibernate-mapping>
5. 测试文件
package com.xy;
import java.util.List;
import java.util.ListIterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateTest {
private static Log log = LogFactory.getLog(HibernateTest.class);
public void find() {
SessionFactory sessionFactory;
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
Session session = sessionFactory.openSession();
List users = session.find("from User");
session.close();
sessionFactory.close();
for (ListIterator iterator = users.listIterator(); iterator
.hasNext();) {
User user = (User) iterator.next();
System.out.println(user.getName() + "\n\tAge: " + user.getAge()
+ "\n\tSex: " + user.getSex());
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void insert() {
SessionFactory sessionFactory;
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
User user = new User();
user.setName("caterpillar");
user.setSex('M');
user.setAge(23);
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
sessionFactory.close();
System.out.println("插入数据OK!请在MySQL查看结果!");
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws HibernateException {
new HibernateTest().find();
new HibernateTest().insert();
}
}
6. 文件结构和所用到的jar包
7. 操作结果
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select user0_.user_id as user_id, user0_.name as name, user0_.sex as sex, user0_.age as age from USER user0_
caterpillar
Age: 28
Sex: M
caterpillar
Age: 28
Sex: M
caterpillar
Age: 28
Sex: M
caterpillar
Age: 23
Sex: M
qqqqq
Age: 20
Sex: M
www
Age: 21
Sex: M
yyy
Age: 20
Sex: M
yyy
Age: 20
Sex: M
yyy
Age: 20
Sex: M
yyy
Age: 20
Sex: M
Hibernate: insert into USER (name, sex, age, user_id) values (?, ?, ?, ?)
插入数据OK!请在MySQL查看结果!