Hibernate单表映射学习笔记之一——hibernalnate开发环境配置
1、什么是ORM?
Object/Relationship Mapping:对象/关系映射
2、写SQL语句不好之处:
(1)不同数据库使用的SQL语法不同(PL/SQL、T/SQL)
(2)同样的功能在不同的数据库中有不同的实现方式(分页SQL)
(3)过分依赖SQL语句对程序的移植和拓展不利
3、Hibernate
(1)ORM框架技术
(2)对JDBC进行了非常轻量的对象封装

4、其他ORM框架技术
(1)Mybatis(前身为iBatis)
(2)Toplink(现为Oracle As Toplink)
(3)EJB:本身就是JAVAEE规范
5、所需工具:
(1) Hibernate 核心包;
(2) Hibernate eclipse plugin;
6、创建Hibernate项目步骤:
(1)导入核心包及数据库驱动:

(2)创建配置文件;

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate?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> <mapping resource="Student.hbm.xml"/> </session-factory> </hibernate-configuration>
(3)创建持久化类;
package hibernate001;
//学生类
import java.util.Date;
public class Student{
//持久化类的设计原则
//1、公有的类
//2、提供共有的不带参数的默认的构造方法
//3、属性私有
//4、属性setter/getter封装
private int sid;//学号
private String name;//姓名
private String gender;//性别
private Date birthday;//出生日期
private String address;//地址
public Student(){
}
public Student(int sid, String name, String gender, Date birthday, String address) {
this.sid = sid;
this.name = 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 getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 "Student [sid=" + sid + ", name=" + name + ", gender=" + gender + ", birthday=" + birthday + ", address="
+ address + "]";
}
}
(4)创建对象——关系映射文件;

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2017-4-14 17:17:44 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="hibernate001.Student" table="STUDENT"> <id name="sid" type="int"> <column name="SID" /> <generator class="assigned" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="gender" type="java.lang.String"> <column name="GENDER" /> </property> <property name="birthday" type="java.util.Date"> <column name="BIRTHDAY" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" /> </property> </class> </hibernate-mapping>
(5)通过Hibernate API编写访问数据库代码:
我用的是Juntil4这个测试类
package hibernate001;
import org.hibernate.Transaction;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//测试类
public class StudentTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//穿件配置对象
Configuration configuration = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
//创建会话对象
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//打开会话
session = sessionFactory.openSession();
//打开事务
transaction = session.beginTransaction();
}
@After
public void destory(){
transaction.commit();//提交事务
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
@Test
public void testSavestudent(){
Student s1 = new Student(1, "张三丰", "男", new Date(), "武当山");
session.save(s1);
}
}

浙公网安备 33010602011771号