1.why——应用程序采用面向对象的设计方式,数据持久化采用关系的设计方式,彼此之间没法对应。
2.what
2.1:本质——ORM
2.1.1:什么是ORM——对象-关系映射;实现对象到关系数据库中表的自动持久化。
2.1.2:组成——Object+Relation+Mapping
3.应用程序持久化的常用技术
3.1:sql/jdbc——最基本
3.2:hibernate——orm
3.3:iBATIS——
3.4:Apache ojb——
3.5:jdo——
3.6:entity bean——
4.hibernate接口
*延迟模式——根据需要产生数据。
4.1:Session——连接、增加、根据id的删、改、查
4.1.1:查询方法【load——get】——load会报对象未找到异常,get不会;load支持延迟加载,而get不支持。
4.2:Transaction——事务
4.3:Query——查询多条记录。
4.3.1:【list——iterator】——list不支持延迟,iterator支持延迟;list只有一条SQL语句,在query.list()时产生/iterator在查询全部id,再通过代理Po对象的get方法查询单条记录。
5.hibernate流程
5.1:书写hibernate配置文件——hibernate-cfg.xml
*用该文件中的mapping resource标签指定映射文件位置。
*
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433/test</property>
<property name="dialect">org.hibernate.dialect.SybaseDialect</property>
<!-- 设置方言,连接某个具体的数据库 -->
<property name="myeclipse.connection.profile">test67</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="show_sql">true</property>
<!-- 打印出hibernate中的SQL语句 -->
<property name="format_sql">true</property>
<mapping resource="com/lovo/po/UserPO.hbm.xml" />
<!-- 在配置文件中指明mapping文件的位置。 -->
</session-factory>
</hibernate-configuration>
5.2:创建Po对象——
*集合类型的属性需要申明为list、set
*需要标识属性,如id
package com.lovo.po;
public class UserPO {
private int id;
private String username;
private String password;
/**
* 无参数构造方法
*/
public UserPO() {
}
/**
* 带参数的构造方法
* @param id
* @param username
*/
public UserPO(int id, String username) {
this.id = id;
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
5.3:书写mapping文件——类名.hbm.xml
<?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>
<class name="com.lovo.po.UserPO" table="t_user">
<!-- 关连Po类和表 -->
<id name="id" column="pk_id" type="int">
<!-- 属性从特殊的先开始写 -->
<generator class="increment"></generator>
<!-- 实现标识字段在hibernate中的自动增长 -->
</id>
<property name="username" column="f_name" type="string"></property>
<!-- 数据类型可以为Java的和hibernate的 -->
<property name="password" column="f_password" type="string"></property>
<!-- 实现类属性和表列的关连,并通过type指明字段在hibernate中的类型 -->
</class>
</hibernate-mapping>
UserDao。Java
package com.lovo.dao;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.lovo.db.HibernateSessionFactory;
import com.lovo.po.UserPO;
public class UserDAO {
public void addUser(UserPO user) {
Session session = HibernateSessionFactory.getSession();//获得连接
Transaction trans = session.beginTransaction();//创建事务
session.save(user);//添加记录
trans.commit();
session.close();
}
public void modUser(UserPO user) {
Session session = HibernateSessionFactory.getSession();
Transaction trans = session.beginTransaction();
session.update(user);//修改记录,需要根据标识属性来改
trans.commit();
session.close();
}
public void delUser(UserPO user) {
Session session = HibernateSessionFactory.getSession();
Transaction trans = session.beginTransaction();
session.delete(user);//删除记录,需要根据标识属性来删
trans.commit();
session.close();
}
public void getUser(int id) {查询方法一:通过Session接口,需要根据标识属性来查
Session session = HibernateSessionFactory.getSession();
// UserPO user = (UserPO)session.get(UserPO.class, id);
// System.out.println(user);
UserPO user = (UserPO) session.load(UserPO.class, id);//load会报异常,支持延迟;get不报错,不支持延迟
System.out.println(user);
session.close();
}
public void queryUser() {查询方法二:通过hql语句
String hql = "from UserPO";//hql——支持面向对象
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery(hql);//查询多条记录,并放入query容器中。
// List<UserPO> list = query.list();//只有一条SQL语句,并查询数据库
// for(UserPO userPO : list){
// System.out.println(userPO.getUsername());
// }
Iterator it = query.iterate();//查询多条记录。查询全表找出表中的ID。
while (it.hasNext()) {
UserPO user = (UserPO) it.next();
String username = user.getUsername();//通过代理模式根据id查询数据库
System.out.println(username);
}
session.close();
}
public void useQueryUpdate(UserPO user) {查询方法三:通过Query接口查询多条记录
String hql = "update UserPO set password = :password "
+ "where username = :username";
//
//
//
Session session = HibernateSessionFactory.getSession();
Transaction trans = session.beginTransaction();
Query query = session.createQuery(hql);
// query.setString(0, user.getPassword());hql语句按位绑定(?)
// query.setString(1, user.getUsername());
// query.setString("mima", user.getPassword());hql语句按名绑定(任意取)
// query.setString("xinming", user.getUsername());//注意 :username 没有空格
// query.setParameter(0, user.getPassword(),Hibernate.STRING);
// query.setParameter(1, user.getUsername());//以上2个方法一样,在需要更明确的数据类型时用第一个,其他时间可以用第二个
//hibernate.DATE;hibenate.TIMETAMPE
query.setProperties(user);按对象绑定;hql中参数绑定方式为:属性名
query.executeUpdate();
trans.commit();
session.close();
}
public void useSQLQuery() {
String sql = "select * from t_user";
Session session = HibernateSessionFactory.getSession();
Query query = session.createSQLQuery(sql);
List<Object[]> list = query.list();
for (Object[] userPO : list) {
System.out.println(userPO[0] + " " + userPO[1] + " "
+ userPO[2]);
}
session.close();
}
public void useCriteria(int page) {查询方法四:利用Criteria
Session session = HibernateSessionFactory.getSession();
Criteria criteria = session.createCriteria(UserPO.class);
//方法:restriction.and;
// criteria.add(Restrictions.sqlRestriction("pk_id = 3"));//为criteria添加查询条件;例如Restrictions.and、Restriction.or
criteria.addOrder(Order.desc("id"));
criteria.addOrder(Order.asc("username"));//以username降序排列
分页
criteria.setFirstResult(0 + 2 * page);//每页的第一条记录是总记录数的第几条
criteria.setMaxResults(2);//每页显示的记录数
List<UserPO> list = criteria.list();//查询数据库
for (UserPO userPO : list) {
System.out.println(userPO.getUsername());
}
session.close();
}
public void useDetachedCriteria(DetachedCriteria dc){查询方法五:利用DetachedCriteria
Session session = HibernateSessionFactory.getSession();
Criteria criteria = dc.getExecutableCriteria(session);//将离线的criteria容器对象转为在线对象。
List<UserPO> list = criteria.list();//从容器中读取数据
for (UserPO userPO : list) {
System.out.println(userPO.getUsername());
}
session.close();
}
}
调用类
public class TestService {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserPO user = new UserPO();
//user.setId(1);
user.setUsername("FRMM");
user.setPassword("WYAJWW");
UserDAO dao = new UserDAO();
dao.useSQLQuery();
}
}
分享到:
相关推荐
Hibernate.jar包,Hibernate可以应用在任何使用JDBC的场合,包含 hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
包含hibernate所有所需jar包还有一些其他包日志包、jpa支持包等: 列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0....
Hibernate连接SQLite配置步骤: 1、添加SQLite库: 1.1、将两个jar包:com.zy.hibernate.dialect.SQLiteDialect.jar、sqlite-jdbc-3.7.2.jar拷贝到“\WEB-INF\lib”文件夹下; 2、配置hibernate: 2.1、将...
Manning出版 Java Persistence With Hibernate 第二版 清晰pdf Hibernate, an open source project that is almost 15 years old. In a recent poll, Hibernate was among the top five tools used by many Java ...
hibernate
论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts 2+Hibernate+Spring实现)论坛系统项目(Struts...
hibernate 资料hibernate 资料hibernate 资料hibernate 资料
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate...
Hibernate连表查询 Hibernate连表查询
Java Persistence with Hibernate is the new bible of Hibernate. As a major revision of the popular Hibernate in Action, it builds on the same single example application to introduce and explain the ...
hibernate
Hibernate稳定版(hibernate-release-5.3.23.Final.zip),Hibernate ORM 是一个为应用程序、库和框架提供对象/关系映射 (ORM) 支持的库。它还提供了 JPA 规范的实现,这是 ORM 的标准 Java 规范。
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端...
hibernate5 hibernate PDF 讲义 动力 hibernate ssh hibernate5 节点
hibernate annotation hibernate3
HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...
本书内容深入浅出,先讲述持久层设计与ORM,再由Hibernate概述、Hibernate基础Hibernate高级特性顺序展开,直至Hibernate实战,重点讲述了Hibernate的基础语法、基础配置、O/R映射、数据关联、数据检索、HQL实用技术...
Hibernate对象的生命周期 Hibernate对象的生命周期 Hibernate对象的生命周期
hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3hibernate3