Java-hibernate的Hello World
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
<property></property> 标签下name是domain对象的属性名,<column/>是和该对像属性名相对应的表的字段名
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.sun.domain"> <class name="Employee" table="employee" > <id name="id" type="java.lang.Integer"> <generator class="increment"></generator> </id> <property name="name" type="java.lang.String"> <column name="name" not-null="true"/> </property> <property name="email" type="java.lang.String"> <column name="email" not-null="true"/> </property> <property name="hiredate" type="java.util.Date"> <column name="hiredate" not-null="true"/> </property> </class> </hibernate-mapping>
<4>手动配置我们的hibernate.cfg.xml文件,该文件用于配置 连接的数据库的类型,driver, 对象关系映射文件
<?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>
<!--数据库的连接--> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
<!--数据库的用户名和密码-->
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <mapping resource="com/sun/domain/Employee.hbm.xml"/> </session-factory> </hibernate-configuration>
<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的基本配置和测试就完成了