hibernate 是对jdbc进行轻量级封装的 orm 框架,充当项目的持久层.
要使用 hibernate首先就需要继续配置,
引包:下载hibernate然后加入jar包
同时引入mysql的jar包
<1> 首先,手动去创建一张测试用的表,这里数据库选择mysql
CREATE TABLE `employee` (`id` int(10) NOT NULL auto_increment,`name` varchar(20) NOT NULL,`email` varchar(20) NOT NULL,`hiredate` date NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;
然后去手动录入数据
<2>创建我们的domain对象
建一个包,名为com.sun.domain,在包下建立一个java文件名为:Employee.java
这里面装的都是表里面的所有的字段
package com.sun.domain;public class Employee { private static final long serialVersionUID = 1L; private Integer id; private String name; private String email; private java.util.Date hiredate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public java.util.Date getHiredate() { return hiredate; } public void setHiredate(java.util.Date hiredate) { this.hiredate = hiredate; }}
<3>在com.sun.domain下创建对象和表的映射关系Employee.hbm.xml
标签下name是domain对象的属性名, 是和该对像属性名相对应的表的字段名
<4>手动配置我们的hibernate.cfg.xml文件,该文件用于配置 连接的数据库的类型,driver, 对象关系映射文件
com.mysql.jdbc.Driver jdbc:mysql://127.0.0.1:3306/test
root 123456 true org.hibernate.dialect.MySQLInnoDBDialect
<5>写一个手动测试文件,因为hibernate是一个数据库持久层框架,所以,不只有web(j2ee)能用,se也能有使用,这里就用se进行测试
创建一个包com.sun.seriver,在下面创建一个文件Student.java
package com.sun.seriver;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.sun.domain.Employee;public class Student { public static void main(String[] args) { // TODO Auto-generated method stub Student.delEmpoyee(); //删除// Student.upEmpoyee();//更新// Student.selectEmpoyee();//查询// Student.addEmpoyee();//添加 } private static void delEmpoyee() { Configuration cfg=new Configuration(); SessionFactory sf = cfg.configure("hibernate.cfg.xml").buildSessionFactory(); Session session = sf.openSession(); Transaction ts=session.beginTransaction(); Employee emp=(Employee) session.load(Employee.class, 2); session.delete(emp); ts.commit(); } private static void upEmpoyee() { Configuration cfg=new Configuration(); SessionFactory sf = cfg.configure("hibernate.cfg.xml").buildSessionFactory(); Session session = sf.openSession(); Transaction ts= session.beginTransaction(); Employee emp=(Employee)session.load(Employee.class, 1); emp.setName("usagi"); ts.commit(); } //SQL SELECT private static void selectEmpoyee() { Configuration cfg=new Configuration(); SessionFactory sf = cfg.configure("hibernate.cfg.xml").buildSessionFactory(); Session session = sf.openSession(); Employee emp=(Employee) session.load(Employee.class, 2); System.out.println(emp.getId()+" "+emp.getName()+" "+emp.getEmail()); session.close(); } //INSERT SELECT private static void addEmpoyee() { Employee s = new Employee(); s.setName("sunzhiyan"); s.setEmail("999@qq.com"); s.setHiredate(new Date()); System.out.println("test is ok"); Configuration cfg=new Configuration(); SessionFactory sf = cfg.configure("hibernate.cfg.xml").buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); sf.close(); } }
这样,基本的hibernate的基本配置和测试就完成了