Hibernate实现CRUD(附项目源码)

本文通过一个实例展示了如何使用Hibernate实现CRUD操作,包括配置文件如applicationContext.xml、applicationContext-beans.xml、struts.xml,以及EmployeeAction、EmployeeService、EmployeeDao等关键类的代码,覆盖了增删查改的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>

org.hibernate.dialect.OracleDialect

oracle.jdbc.driver.OracleDriver

mine

mine

jdbc:oracle:thin:@127.0.0.1:1521:orcl

true

update

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:tx=“http://www.springframework.org/schema/tx”

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<context:property-placeholder location=“classpath:db.properties” ignore-unresolvable=“true”/>

<bean id=“sessionFactory”

class=“org.springframework.orm.hibernate5.LocalSessionFactoryBean”>

com/ssh/entities/Department.hbm.xml

com/ssh/entities/Employee.hbm.xml

hibernate.cfg.xml

org.hibernate.dialect.OracleDialect

update

true

true

<tx:annotation-driven transaction-manager=“transactionManager”/>

applicationContext-beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?> false

/WEB-INF/views/emp-list.jsp

text/html inputStream

/WEB-INF/views/emp-input.jsp

/emp-list

3、类文件


EmployeeAction

package com.ssh.actions;

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.util.Date;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

import com.opensymphony.xwork2.Preparable;

import com.ssh.entities.Employee;

import com.ssh.service.DepartmentService;

import com.ssh.service.EmployeeService;

public class EmployeeAction extends ActionSupport implements RequestAware,

ModelDriven,Preparable{

private static final long serialVersionUID = 1L;

private EmployeeService employeeService;

public void setEmployeeService(EmployeeService employeeService) {

this.employeeService = employeeService;

}

private DepartmentService departmentService;

public void setDepartmentService(Depart 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 mentService departmentService) {

this.departmentService = departmentService;

}

public String list() {

request.put(“employees”,employeeService.getAll());

return “list”;

}

private Integer id;

public void setId(Integer id) {

this.id = id;

}

public InputStream inputStream;

public InputStream getInputStream() {

return inputStream;

}

public String delete() {

try {

employeeService.delete(id);

inputStream = new ByteArrayInputStream(“1”.getBytes(“UTF-8”));

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return SUCCESS;

}

public String input() {

request.put(“departments”, departmentService.getAll());

return INPUT;

}

public void prepareInput() {

if(id!=null) {

model = employeeService.get(id);

}

}

public String save() {

if(id == null) {

model.setCreateTime(new Date());

}

employeeService.saveOrUpdate(model);

System.out.println(“model”);

return SUCCESS;

}

public void prepareSave() {

if(id == null) {

model = new Employee();

}else {

model = employeeService.get(id);

}

}

private Map<String,Object> request;

@Override

public void setRequest(Map<String, Object> arg0) {

this.request = arg0;

}

@Override

public void prepare() throws Exception {}

private Employee model;

@Override

public Employee getModel() {

return model;

}

}

EmployeeService

package com.ssh.service;

import java.util.List;

import com.ssh.dao.EmployeeDao;

import com.ssh.entities.Employee;

public class EmployeeService {

private EmployeeDao employeeDao;

public void setEmployeeDao(EmployeeDao employeeDao) {

this.employeeDao = employeeDao;

}

public void saveOrUpdate(Employee employee) {

employeeDao.saveOrUpdate(employee);

}

public void delete(Integer id) {

employeeDao.delete(id);

}

public List getAll(){

List employees = employeeDao.getAll();

return employees;

}

public Employee get(Integer id) {

return employeeDao.get(id);

}

}

EmployeeDao

package com.ssh.dao;

import java.util.List;

import com.ssh.entities.Employee;

public class EmployeeDao extends BaseDao{

public void delete(Integer id) {

String hql = “DELETE From Employee e where e.id=?0”;

getSession().createQuery(hql).setParameter(0,id).executeUpdate();

}

public List getAll() {

//String hql = “From Employee e LEFT OUTER JOIN FETCH e.department”;

String hql = “From Employee”;

return getSession().createQuery(hql).list();

}

public void saveOrUpdate(Employee employee) {

getSession().saveOrUpdate(employee);

}

public Employee get(Integer id) {

return (Employee)getSession().get(Employee.class,id);

}

}

emp-list.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<%@ taglib prefix=“s” uri=“/struts-tags”%>

Insert title here
Employee List Page

<s:if test=“#request.employees == null||#request.employees.size() == 0”>

没有任何员工信息

</s:if>

<s:else>

ID LASTNAME EMAIL BIRTH CREATETIME delete edit

<s:iterator value=“#request.employees”>

${id } ${lastName } ${email } ${birth } ${createTime }

Delete

Edit

</s:iterator>

</s:else>

emp-input.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<%@ taglib prefix=“s” uri=“/struts-tags” %>

Insert title here
Employee Input Page

<s:form action=“emp-save” method=“post”>

<s:if test=“id != null”>

<s:textfield name=“lastName” label=“LastName” disabled=“true”></s:textfield>

<s:hidden name=“id”></s:hidden>

</s:if>

<s:else>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值