通过OR映射,完成插入数据功能
使用的hibernate的版本是3.2.5
数据库MySQL
下载地址:http://download.csdn.net/detail/lf19820717/2746950
1.导入jar包
hibernate中lib文件夹下面的jar包全部导入。
并且导入MySQL的驱动包
2.创建项目
3.为了方便起见,图中的User.hbm.xml可以在eg文件夹下找到。
hibernate.cfg.xml可以在etc文件件下找到。
4.User为一个实体类,3个属性,实现get,set方法
package cn.itcast.hibernate.domain;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
5.user.hbm.xml为映射文件,对应数据库中相应的表
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.hibernate.domain">
<class name="User">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<property name="birthday" />
</class>
</hibernate-mapping>
6.hibernate.cfg.xml为连接数据库的配置信息( 其中的一些属性的设置可以在etc目录下面的hibernate.properties中找到)
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 在控制台显示sql语句 -->
<property name="show_sql">true</property>
<!-- 数据库的方言,连接对应的数据库,不同的数据库有不同的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库URL -->
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password">root</property>
<!-- 是否自动创建表?update表示更新,create表示创建,用update防止重新部署项目时表被删除 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 格式化SQL语句,让sql语句在控制台打印规范些 -->
<property name="hibernate.format_sql">true</property>
<!-- 映射文件路径 -->
<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
7.Base.java 测试类(用来初始化Hibernate,并且插入数据)
package cn.itcast.hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.itcast.hibernate.domain.User;
public class Base {
/**
* @param args
*/
public static void main(String[] args) {
// hibernate初始化,就像jdbc连的时候要注册驱动信息
Configuration configuration = new Configuration();
configuration.configure();
// 获取SessionFoctory对象
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 获取session对象
Session session = sessionFactory.openSession();
//开启事务
Transaction transaction=session.beginTransaction();
User user = new User();
user.setBirthday(new Date());
user.setName("name");
session.save(user);
transaction.commit();
// 关闭连接
session.close();
}
}
1.ORM Object Relation Mapping(对象关系映射)
2.hibernate是开源的,下载的代码,可以修改。
3.在hibernate.propertiees中有所有数据库的配置信息。方言,用户名,密码,url,驱动名称
4.localhost:3306=///
5.sessionFactory相当于jdbc中的DriverManager类。
6.hibernate中自动创建表的属性hbm2ddl.auto create/update
7.查看创建表的语句:show create table user;
8.查看数据库支持的引擎:show engines;