Hibernate实现CRUD(附项目源码)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本下载包与书籍的实战篇紧密结合. 下载包中的程序是一个简明的示例论坛程序,按照循序渐进的原则,分为不同的步骤: 步骤1: 建立最简单的项目基础结构:Board/User/Article三个类,包含最基本的配置文件。同时,在这一步中,建立测试类,刻画项目各个类之间的契约关系。 步骤2: 对文章的种类增加“投票”类型,展示对多型的处理。 步骤3: 增加webwork实现的GUI。 加入中文支持。 步骤4: 升级到Hibernate 3。 环境说明 --------- 程序包中的程序经过测试的运行环境为: 1, Tomcat 5.0.25 2, Hibernate 2.1.7 3, Hibernate 3.0.2 4, Eclipse 3.0.1 (作为IDE) 5, ant 1.6(作为build工具) 6, java sdk 1.4.2 7, mysql 4.1 (作为后台数据库) 8, 至少250M硬盘空间(hibernate 2,3和webwork就将占据138M) 虽然在更高版本中应该可以不加修改的运行,但我们对此不加以保证。(v1.2将对Hibernate 3.0.4进行测试) 安装说明 --------- 1, 确认jdk 1.4.2已经正确安装; 2,将本程序包解压,假设目录为: x:\rs\hib-samples 3, 从hibernate的sf下载区下载2.1.7与3.0.2版本: http://sourceforge.net/project/showfiles.php?group_id=40712 4,将下载的hibernate包解压到x:\rs\hib-samples\hibernate-2.1与x:\rs\hib-samples\hibernate-3,目录结构应该为: x: + rs + hib-samples <--- 本文件包解压的目录 + forum-step1-db-first-middlegen + forum-step1-db-first-synchronizer + forum-step2 + ... + hibernate-2.1 <--- Hibernate 2.1.7解压到这里 + bin <--- 确认bin与doc目录就在解压后的hibernate-2.1目录中 + doc + ... + hibernate-3.0 <--- Hibernate 3.0.2解压到这里 + bin preference -> Java -> Build Path -> User Librarys,分别建立名为Hibernate2,hibernate3,mysql-jdbc的用户库,分别包含对应的lib文件(如hibernate2包含hibernate-2.1目录下的hibernate2.jar及其lib目录下的所有jar文件); 11, 在您的eclipse 中,导入各个目录下的.project文件,以建立工程.(工程名为step1,step2,step3,step4)。在工程的类路径配置中,使用上一步创建的用户库(step1-step3使用Hibernate2,step4使用hibernate3)。确保没有编译错误。 好了,您现在可以开始阅读代码,作一些自己的改变,看看您能得到什么结果 :) 中文处理说明 ------------ 关于中文的处理,是在step3加入的,因此在step1以及step2中,请使用英文进行实验。具体进行的处理为全程采用UTF-8编码. 1,mysql创建时,字符集必须选择UTF-8 2,在mysql jdbc连接的url中,必须指定采用utf-8 encoding。 jdbc:mysql://localhost/forum?useUnicode=true&characterEncoding=utf-8&mysqlEncoding=utf8 3,在jsp页面中,指定页面采用UTF-8编码. 假若在您的项目中,必须使用GBK编码,则在以上的各个地方,都需要把UTF-8更换为GBK方可正常使用。 版本更新 ----------- v1.2 (PLAN,TBD) * 转换到JTA * 给出一个脱离DAO模式的例子 * 给出for hibernate 3的xdoclet例子 v1.1 (2005.5.25) * 分离db目录下的build.xml * 增加了对投票的图形显示 * 修正了和webwork相关的一个中文bug v1.0 (2005.4.20) * 初始版本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值