一 ..建java项目
工具 eclipse
file->new ->Java Project
二.导入jar包
刚开始只导入几个相关的jar包 后来真正运行时老出错 说少类 所以我就将很多jar包都导进去了 各位如果能甄选出来哪些是不需要的 下面评论提示下
下载 jar包(一)http://download.csdn.net/detail/a1059484278/9779055 下载jar包(二)http://download.csdn.net/detail/a1059484278/9779060
三.开始配置文件 写代码了
1.建javaBean
package com.example.hibernate;
import java.sql.Date;
public class Customer{
private String uuId; //注意 类型 为String 唯一主键
private String customerName;
private Integer age;
private java.sql.Date updateTime;
public String getUuId() {
return uuId;
}
public void setUuId(String uuId) {
this.uuId = uuId;
}
public java.sql.Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
2.建立javaBean.Customer的映射文件 Customer.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="javaBean.Customer" table="customer">
<id name="uuId" column="uu_id">
<generator class="uuid" />
</id>
<property name="customerName" column="customer_name"/>
<property name="age" column="age"/>
<property name="updateTime" column="update_time"/>
</class>
</hibernate-mapping>
3.建立 hibernate.cfg.xml
可复制如下代码
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- mysql数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- mysql数据库名称 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_test?useUnicode=true&characterEncoding=UTF-8</property> <!-- 写后面的主要是为了传汉字参数时不乱码 -->
<!-- 数据库的登陆用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库的登陆密码 -->
<property name="hibernate.connection.password"></property>
<!-- 方言:为每一种数据库提供适配器,方便转换 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="javaBean/Customer.hbm.xml"></mapping><!-- 配置POJO映射文件 -->
</session-factory>
</hibernate-configuration>
4.建表 在数据库中建立此表 编写测试类 HibernateTest
package javaBean;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cf=new Configuration().configure();//此处 如果配置文件有问题 会出错
SchemaExport e=new SchemaExport(cf);
e.create(true, true);
}
}
//如果打印 drop table if exists customer_test
create table customer_test (uu_id varchar(255) not null, customer_name varchar(255), age integer, update_time date, primary key (uu_id)) 则 成功!!
5.添加表数据 还是在测试类里写
package javaBean;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Configuration cf=new Configuration().configure();
// SchemaExport e=new SchemaExport(cf);
// e.create(true, true);
Configuration cf=new Configuration().configure();
SessionFactory sf=cf.buildSessionFactory();
Session session=null;
session=sf.openSession();
session.beginTransaction();
session.getTransaction().commit();
try{
session = sf.openSession();
//开启事务
session.beginTransaction();
Customer c=new Customer();//开始给对象set数据
c.setAge(22);
c.setCustomerName("李云龙");
c.setUpdateTime(new Date(System.currentTimeMillis()));
session.save(c);
//提交事务
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally{
if(session != null){
if(session.isOpen()){
//关闭session
session.close();
}
}
}
}
}
添加成功!!!
这里只是简单介绍一下这个框架来建表 添加数据 各位如果要深入使用 请务必 要加以研究.
最后上一下这个项目的目录图