基于Hibernate实现CRUD

 


目的: 

掌握Hibernate的主要接口使用;

掌握设计持久化接口的基本方法;

熟悉提高Hibernate使用性能的方法;

内容:

使用Hibernate设计通用接口实现对Java对象进行增查改删(CRUD)的工作;

具体要求:实现CRUD接口具有通用性;可以在已有项

目中增加相应接口;提前设计好,最后一次实验演示,

并提交相关代码及本次实验报告。

设备及软件环境:

      Myeclipes2014Mysql

      JDK1.8hibernate5.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 List find(String hql, Class entityClass, Object[] params) {

Query query = this.getSession().createQuery(hql);

if (params != null) {

for (int i = 0; i < params.length; i++) {

query.setParameter(i, params[i]);

}

}

return (List) query.list();

}

 

// 获取一条,根据主键id

public T get(Class entityClass, Serializable id) {

return (T) this.getSession().get(entityClass, id);

}

 

// 分页查询,查询两次,一次查询总数,一次查询分页记录

public Page findPage(String hql, Page page, Class entityClass, Object[] params) {

 

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) query.list());

return page;

}

 

// 新增和修改,hibernate根据id是否为null自动判断

public void saveOrUpdate(T entity) {

this.getSession().saveOrUpdate(entity);

}

// 集合保存,这时新增还是修改,就自动判断,调用时是否简洁。适合批量新增和修改时。(Mrecord控件)

public void saveOrUpdateAll(Collection entitys) {

for (T entity : entitys) {

this.saveOrUpdate(entity);// 为什么hibernate批量操作时,要用循环一条一条记录去更新?

}

}

// 按主键id删除

public void deleteById(Class entityClass, Serializable id) {

this.getSession().delete(get(entityClass, id));

}

// 批量删除

public void delete(Class entityClass, Serializable[] ids) {

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 List find(String hql, Class entityClass, Object[] params);

 

// 获取一条记录

public T get(Class entityClass, Serializable id);

 

// 分页查询,将数据封装到一个page分页工具类对象

public Page findPage(String hql, Page page, Class entityClass, Object[] params);

 

// 新增和修改保存

public void saveOrUpdate(T entity);

 

// 批量新增和修改保存

public void saveOrUpdateAll(Collection entitys);

 

// 单条删除,按id

public void deleteById(Class entityClass, Serializable id);

 

// 批量删除

public void delete(Class entityClass, Serializable[] ids);

}

 

package org.lqz.sale.domain;

 

import java.io.Serializable;

import java.util.Set;

 

/**

 * 分类

 *

 */

public class Category implements Serializable {

 

private Set goods; // 分类对商品 一对多

 

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 getGoods() {

return goods;

}

 

public void setGoods(Set goods) {

this.goods = goods;

}

 

}

 

package org.lqz.sale.domain;

 

import java.io.Serializable;

import java.util.Set;

 

/**

 * 销售订单表

 *

 

 *

 */

 

public class Sale implements Serializable {

 

private Set saleGoods; // 销售单对销售货物, 一对多

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 getSaleGoods() {

return saleGoods;

}

 

public void setSaleGoods(Set saleGoods) {

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 stockGoods; // 出入库与货物 一对多

private User handler;// 经手人

 

private String id;

private int sign;// 出入库标识{0表示入库,1表示出库}

private int delFlag;// 删除标识{1表示已删除,0表示未删除}

 

public Set getStockGoods() {

return stockGoods;

}

 

public void setStockGoods(Set stockGoods) {

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 stocks; // 用户与出入库 一对多

private Set sales;// 用户与销售 一对多

 

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 getStocks() {

return stocks;

}

 

public void setStocks(Set stocks) {

this.stocks = stocks;

}

 

public Set getSales() {

return sales;

}

 

public void setSales(Set sales) {

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">

"org.lqz.sale.domain">

"Category" table="category" dynamic-insert="true" dynamic-update="true">

"id" column="category_id">

"uuid" />

"name" column="name" />

"delFlag" column="del_flag" />

 

"goods" cascade="all" inverse="true">

"category_id">

"Good" />

   

 

"1.0" encoding="UTF-8"?>

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

"org.lqz.sale.domain">

"Role" table="role" dynamic-insert="true" dynamic-update="true">

"id" column="id">

"uuid" />

 

"name" column="name" />

 

"users" cascade="all" inverse="true">

"role_id">

"User" />

 

 

"1.0" encoding="UTF-8"?>

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

"org.lqz.sale.domain">

"Sale" table="sale" dynamic-insert="true" dynamic-update="true">

"id" column="sale_id">

"uuid" />

 

"delFlag" column="del_flag" />

 

"saleGoods" cascade="all-delete-orphan" inverse="true">

"sale_id">

"SaleGoods" />

 

"handler" class="User" column="handler_id" />

   

 

"1.0" encoding="UTF-8"?>

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

"org.lqz.sale.domain">

"Stock" table="stock" dynamic-insert="true"dynamic-update="true">

"id" column="stock_id">

"uuid" />

"sign" column="sign" />

"delFlag" column="del_flag" />

 

"stockGoods" cascade="all-delete-orphan" inverse="true">

"stock_id">

"StockGoods" />

 

"handler" class="User" column="handler_id" />

 

   

 

"1.0" encoding="UTF-8"?>

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

"org.lqz.sale.domain">

"User" table="user" dynamic-insert="true" dynamic-update="true">

"id" column="user_id">

"uuid" />

 

"name" column="name" />

"password" column="password" />

"role" class="Role" column="role_id">

 

"stocks" cascade="all" inverse="true">

"handler_id">

"Stock" />

"sales" cascade="all" inverse="true">

"handler_id">

"Sale" />

   

 

"1.0" encoding="UTF-8"?>

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 

 

 

"hibernate.dialect">

org.hibernate.dialect.MySQLDialect

 

"show_sql">false

"format_sql">false

"hbm2ddl.auto">update

 

"hibernate.enable_lazy_load_no_trans">true

"javax.persistence.validation.mode">none

 

"org/lqz/sale/domain/Category.hbm.xml" />

"org/lqz/sale/domain/Good.hbm.xml" />

"org/lqz/sale/domain/Sale.hbm.xml" />

"org/lqz/sale/domain/SaleGoods.hbm.xml" />

"org/lqz/sale/domain/Stock.hbm.xml" />

"org/lqz/sale/domain/StockGoods.hbm.xml" />

"org/lqz/sale/domain/User.hbm.xml" />

"org/lqz/sale/domain/Warehouse.hbm.xml" />

"org/lqz/sale/domain/Role.hbm.xml"/>

 

 基于Hibernate实现CRUD_第1张图片

基于Hibernate实现CRUD_第2张图片

基于Hibernate实现CRUD_第3张图片

基于Hibernate实现CRUD_第4张图片

基于Hibernate实现CRUD_第5张图片

基于Hibernate实现CRUD_第6张图片

基于Hibernate实现CRUD_第7张图片

基于Hibernate实现CRUD_第8张图片

基于Hibernate实现CRUD_第9张图片

基于Hibernate实现CRUD_第10张图片

基于Hibernate实现CRUD_第11张图片

基于Hibernate实现CRUD_第12张图片

基于Hibernate实现CRUD_第13张图片



总结:

    本次内容让我们使用Hibernate进行数据库操作,运用hibernate技术进行数据库增删查改的四种操作,更加熟悉hibernate的原理与应用。


你可能感兴趣的:(基于Hibernate实现CRUD)