hibernate是一种ORM框架,对JDBC进行轻量级的对象封装
首先第一步要下载Hibernate Tools for Eclipse plugins ,新建Java project
然后导入hibernate的jar,以及jdbc驱动的jar(用于连接数据库),juint的jar(测试用的)
hibernate.cfg.xml的配置:
依次是连接的数据库的用户名,用户密码,jdbc驱动,访问数据库的地址(用///表示的是回环地址,imooc是在MySQL中建的数据库),方言(用的是MySQL,所以改为MySQL)
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connectioin.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///imooc?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
下面依次是 输出到控制台,规范化输出,以创建的形式进行
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
实体类(按照Javabean)
public class Students {
private int sid;// 学号
private String sname;// 姓名
private String gender;// 性别
private Date birthday;// 出生日期
private String address;// 地址
public Students() {
//
}
public Students(int id,String name,String gender,Date birthday,String address){
sid=id;
sname=name;
this.gender=gender;
this.birthday=birthday;
this.address=address;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
+ ", address=" + address + "]";
}
}
在src下new->other->hibernate->选择最后的一个,创建对应的映射文件,别忘了在hibernate.cfg.xml上声明
<mapping resource="Students.hbm.xml"/>
接下来就可以进行测试
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//测试类
public class StudentdTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
//初始化会话工厂
@Before
public void init(){
//创建配置对象,读取hibernate.cfg.xml文件,完成初始化
Configuration config=new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获取会话工厂
sessionFactory=config.buildSessionFactory(serviceRegistry);
//会话对象
session=sessionFactory.openSession();
//开启事务
// transaction=session.beginTransaction();
}
@After
public void destory(){
//提交事务
// transaction.commit();
//关闭会话
session.close();
//关闭会话工厂
sessionFactory.close();
}
@Test
public void testSaveStudents(){
//生成学生对象
Students s=new Students(2,"张三","男",new Date(),"武当山");
session.doWork(new Work() {
@Override
public void execute(Connection arg0) throws SQLException {
// TODO Auto-generated method stub
arg0.setAutoCommit(true);
}
});
//保存对象进数据库
session.save(s);
session.flush();
}
}
这里我们是使用的自动提交,没有用到事务,不过一般用事务提交,
运行成功后,就可以看到数据库的变化了