ORM提供了实现持久化层的另一种模式,它采用映射元数据来描述对象关系的映射,使得ORM中间件能在任何一个应用的业务逻辑层和数据库层之间充当桥梁。Java典型的ORM中间件有:Hibernate,ibatis,speedframework。
ORM的方法论基于三个核心原则:
· 简单:以最基本的形式建模数据。
· 传达性:数据库结构被任何人都能理解的语言文档化。
ORM的方法论基于三个核心原则:
· 简单:以最基本的形式建模数据。
· 传达性:数据库结构被任何人都能理解的语言文档化。
· 精确性:基于数据模型创建正确标准化了的结构。
ORM的环境架构:一个软件开发工具 eclipse 作为小白在开发工具中的JDK环境配置花费了很长时间与百度同学交流,然后要进行Hibernate中lib文件复制到新建项目的lib子文件下
JAVA与数据库连接
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver<alue>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/User<alue>
</property>
<property name="username">
<value>root<alue>
</property>
<property name="password">
<value>123456<alue>
</property>
</bean>
配置Hibernate核心配置文件(hibernate.cfg.xml)
创建数据库:
编写对象与对象映射
public class User {
private int ID;
private String NAME;
private String AGE;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getNAME() {
return NAME;
}
public void setNAME(String nAME) {
NAME = nAME;
}
public String getAGE() {
return AGE;
}
public void setAGE(String aGE) {
AGE = aGE;
}
}
初始化SessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/zrrd/lgx/pojo/User.hbm.xml<alue>
<st>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
配置Student.hbm.xml文件
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.test.wws.pojo.user" table="user">
<id name="ID" column="ID" type="java.lang.Integer">
<!-- native Assigned increment identity uuid.hex uuid.string foreign -->
<generator class="native" />
</id>
<property name="NAME" column="name" />
<property name="AGE" column="AGE" />
</class>
</hibernate-mapping>
测试类:
package com.test.wws;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class TestDemo {
@Test
public void HouseImpl() {
Configuration qq= new Configuration().configure();
SessionFactory sessionFactory = qq.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction ww= session.beginTransaction();
Customer www = (Customer) session.get(Customer.class, 1);
System.out.println(c.toString());
ww.commit();
session.close();
sessionFactory.close();
}
}
运行结果:
、
通过本次Hibernate的demo学习发现了许多不足,主要增强了程序员的主要技能信息的检索,发现了纸上所说与动手操作的区别,同志仍需努力啊!!!!!!