目的:
掌握Hibernate的主要接口使用;
掌握设计持久化接口的基本方法;
熟悉提高Hibernate使用性能的方法;
内容:
使用Hibernate设计通用接口实现对Java对象进行增查改删(CRUD)的工作;
具体要求:实现CRUD接口具有通用性;可以在已有项
目中增加相应接口;提前设计好,最后一次实验演示,
并提交相关代码及本次实验报告。
设备及软件环境:
Myeclipes2014,Mysql
JDK1.8,hibernate5.2.10
过程及结果:
package org.lqz.sale.dao.impl;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.lqz.sale.dao.BaseDao;
import org.lqz.sale.utils.Page;
public class BaseDaoImpl implements BaseDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
// 带条件查询
public
Query query = this.getSession().createQuery(hql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
return (List
}
// 获取一条,根据主键id
public
return (T) this.getSession().get(entityClass, id);
}
// 分页查询,查询两次,一次查询总数,一次查询分页记录
public
Query query = this.getSession().createQuery(hql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
}
// 查询一次,获取记录总数
int count = query.list().size(); // 可以优化???? "select count(*) " +hql
page.setTotalRecord(count);
// 设置分页
query.setFirstResult((page.getPageNo() - 1) * page.getPageSize()); // 设置开始位置
query.setMaxResults(page.getPageSize()); // 设置获取几条
page.setResults((List
return page;
}
// 新增和修改,hibernate根据id是否为null自动判断
public
this.getSession().saveOrUpdate(entity);
}
// 集合保存,这时新增还是修改,就自动判断,调用时是否简洁。适合批量新增和修改时。(Mrecord控件)
public
for (T entity : entitys) {
this.saveOrUpdate(entity);// 为什么hibernate批量操作时,要用循环一条一条记录去更新?
}
}
// 按主键id删除
public
this.getSession().delete(get(entityClass, id));
}
// 批量删除
public
for (Serializable s : ids) {
deleteById(entityClass, s);
}
}
}
package org.lqz.sale.dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.lqz.sale.utils.Page;
public interface BaseDao {
// 查询所有,带条件查询
public
// 获取一条记录
public
// 分页查询,将数据封装到一个page分页工具类对象
public
// 新增和修改保存
public
// 批量新增和修改保存
public
// 单条删除,按id
public
// 批量删除
public
}
package org.lqz.sale.domain;
import java.io.Serializable;
import java.util.Set;
/**
* 分类
*
*/
public class Category implements Serializable {
private Set
private String id;
private String name;// 分类名
private int delFlag;// 删除标识{1表示已删除,0表示未删除}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + ", delFlag=" + delFlag + "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDelFlag() {
return delFlag;
}
public void setDelFlag(int delFlag) {
this.delFlag = delFlag;
}
public Set
return goods;
}
public void setGoods(Set
this.goods = goods;
}
}
package org.lqz.sale.domain;
import java.io.Serializable;
import java.util.Set;
/**
* 销售订单表
*
*
*/
public class Sale implements Serializable {
private Set
private User handler; // 销售单对经手人, 多对一
private String id;// 订单id
private Integer delFlag;// 删除标识{1表示已删除,0表示未删除}
@Override
public String toString() {
return "Sale [handler=" + handler + ", id=" + id + ", delFlag=" + delFlag + "]";
}
public Set
return saleGoods;
}
public void setSaleGoods(Set
this.saleGoods = saleGoods;
}
public User getHandler() {
return handler;
}
public void setHandler(User handler) {
this.handler = handler;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getDelFlag() {
return delFlag;
}
public void setDelFlag(Integer delFlag) {
this.delFlag = delFlag;
}
}
package org.lqz.sale.domain;
import java.io.Serializable;
import java.util.Set;
/**
* 出入库订单表
*
*
*/
public class Stock implements Serializable {
private Set
private User handler;// 经手人
private String id;
private int sign;// 出入库标识{0表示入库,1表示出库}
private int delFlag;// 删除标识{1表示已删除,0表示未删除}
public Set
return stockGoods;
}
public void setStockGoods(Set
this.stockGoods = stockGoods;
}
public User getHandler() {
return handler;
}
public void setHandler(User handler) {
this.handler = handler;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getSign() {
return sign;
}
public void setSign(int sign) {
this.sign = sign;
}
public int getDelFlag() {
return delFlag;
}
public void setDelFlag(int delFlag) {
this.delFlag = delFlag;
}
}
package org.lqz.sale.domain;
import java.io.Serializable;
import java.util.Set;
/**
* 用户
*
*
*/
public class User implements Serializable {
private Set
private Set
private Role role;
private String id;
private String name;
private String password;
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
}
public Set
return stocks;
}
public void setStocks(Set
this.stocks = stocks;
}
public Set
return sales;
}
public void setSales(Set
this.sales = sales;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
"1.0" encoding="UTF-8"?>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
"1.0" encoding="UTF-8"?>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
"1.0" encoding="UTF-8"?>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
"1.0" encoding="UTF-8"?>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
"1.0" encoding="UTF-8"?>
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
"1.0" encoding="UTF-8"?>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
org.hibernate.dialect.MySQLDialect
总结:
本次内容让我们使用Hibernate进行数据库操作,运用hibernate技术进行数据库增删查改的四种操作,更加熟悉hibernate的原理与应用。