Hibernate实现CRUD(附项目源码)

Hibernate实现CRUD(附项目源码),第1张

org.hibernate.dialect.OracleDialect

oracle.jdbc.driver.OracleDriver

mine

mine

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

true

update

applicationContext.xml

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

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

applicationContext-beans.xml

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 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 request;

@Override

public void setRequest(Map 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

没有任何员工信息

ID LASTNAME EMAIL BIRTH CREATETIME delete edit

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

Delete

Edit

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/788386.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存