Hibernate+jsp+struts+spring做增删该查,
同样还是web项目,这里只做了一张表,做一个测试,例子。主要是建Hibernate 的时候要非常注意,有时间了整理一下建Hiberbnate 的时候需要注意的事项

这里我是建了5个包,其实只要四个就好,那个service包和action包可以放在一起的,其他的是1.实体包;2.接口包(写方法名的);3.接口实现包(实现接口的方法);4.action包(数据逻辑)
一、实体包,这里是自动生成的,但是强迫症还是把它放在这里了啊
package com.chinasofti.sh2.entity; /**
* Information entity. @author MyEclipse Persistence Tools
*/ @SuppressWarnings("serial")
public class Information implements java.io.Serializable { // Fields private Integer id;
private String name;
private String department;
private String position;
private String password;
private String tel;
private String lervel; // Constructors /** default constructor */
public Information() {
} /** full constructor */
public Information(String name, String department, String position,
String password, String tel, String lervel) {
this.name = name;
this.department = department;
this.position = position;
this.password = password;
this.tel = tel;
this.lervel = lervel;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public String getDepartment() {
return this.department;
} public void setDepartment(String department) {
this.department = department;
} public String getPosition() {
return this.position;
} public void setPosition(String position) {
this.position = position;
} public String getPassword() {
return this.password;
} public void setPassword(String password) {
this.password = password;
} public String getTel() {
return this.tel;
} public void setTel(String tel) {
this.tel = tel;
} public String getLervel() {
return this.lervel;
} public void setLervel(String lervel) {
this.lervel = lervel;
} }
Information.java
二、接口包。里面有方法名,可是没有实际的方法
package com.chinasofti.sh2.dao;
import java.util.List;
import com.chinasofti.sh2.entity.Information;
public interface IInformationDAO {
void Insert(Information model);
void Update(Information model);
void Delete(Information model);
Information GetInformationById(int id);
List<Information> GetInformation();
List<Information> GetInformationPaging(int pageIndex,int pageSize);
}
IInformationDAO
三、接口实现包
package com.chinasofti.sh2.daoimpl; import java.util.List; import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate; import com.chinasofti.sh2.dao.IInformationDAO;
import com.chinasofti.sh2.entity.Information; public class InformationImpl implements IInformationDAO{
private SessionFactory sessionFactory;
private HibernateTemplate hibernateTemplate; public SessionFactory getSessionFactory() {
return sessionFactory;
} public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
} public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} @Override
public void Insert(Information model) {
// TODO Auto-generated method stub
hibernateTemplate.save(model);
} @Override
public void Update(Information model) {
// TODO Auto-generated method stub
hibernateTemplate.update(model);
} @Override
public void Delete(Information model) {
// TODO Auto-generated method stub
hibernateTemplate.delete(model);
} @Override
public Information GetInformationById(int id) {
// TODO Auto-generated method stub
return hibernateTemplate.get(Information.class, id);
} @Override
public List<Information> GetInformation() {
// TODO Auto-generated method stub
return null;
} @Override
public List<Information> GetInformationPaging(int pageIndex, int pageSize) {
// TODO Auto-generated method stub
return this.getHibernateTemplate().loadAll(Information.class).subList((pageIndex-1)*pageSize, pageIndex*pageSize);
} }
InformationImpl.java
四、Action包,处理逻辑问题的,
package com.chinasofti.sh2.service; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.chinasofti.sh2.dao.IInformationDAO;
import com.chinasofti.sh2.daoimpl.InformationImpl;
import com.chinasofti.sh2.entity.Information; public class InformationService { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
InformationImpl ii=(InformationImpl)context.getBean("informationservice");
//1.增加
// Information i=new Information();
// i.setName("王二小");
// i.setPassword("123123");
// i.setLervel("员工");
// i.setDepartment("654654");
// i.setPosition("4546");
// i.setTel("5");
// ii.Insert(i);
//2.删除
// Information i=new Information();
// i.setName("王二小");
// i.setPassword("123123");
// i.setLervel("员工");
// i.setDepartment("654654");
// i.setPosition("4546");
// i.setTel("5");
// i.setId(31);
// ii.Delete(i); //3.更改
// Information i=new Information();
// i.setName("111");
// i.setPassword("111");
// i.setLervel("111");
// i.setDepartment("111");
// i.setPosition("111");
// i.setTel("111");
// i.setId(25);
// ii.Update(i);
//4.根据ID查找 Information i= ii.GetInformationById(1);
System.out.println(i.getName()+"."+i.getLervel());
} }
InformationServace.java
package com.chinasofti.sh2.action; import java.util.List; import com.chinasofti.sh2.dao.IInformationDAO;
import com.chinasofti.sh2.entity.Information; public class InformationAction {
private IInformationDAO service; private int pageSize=5;//分页的
private int pageIndex=1; //分页的 private List<Information> result; public IInformationDAO getService() {
return service;
} public void setService(IInformationDAO service) {
this.service = service;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getPageIndex() {
return pageIndex;
} public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
} public List<Information> getResult() {
return result;
} public void setResult(List<Information> result) {
this.result = result;
} public String list(){
//输入输出的检测,要在Action里面写。
result=service.GetInformationPaging(pageIndex,pageSize);//分页的
return "list";
} }
InformationAction.java
五。jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'information.jsp' starting page</title> <style type="text/css">
tr {
text-align: center;
font-family: "微软雅黑";
font-size: 14px;
background-color: #003;
color:#F90;
}
</style> </head> <body>
<table width="80%" border="1" bgcolor="celeste">
<tr>
<td width="100">员工ID</td>
<td width="100">名字</td>
<td width="100">所在部门</td>
<td width="100">岗位</td>
<td width="100">员工密码</td>
<td width="100">电话号码</td>
<td width="100">级别</td>
<td width="300" colspan="3"><a href="">增加</a></td>
</tr>
<c:forEach var="ms" items="${result}">
<tr>
<td>${ms.id}</td>
<td>${ms.name}</td>
<td>${ms.department}</td>
<td>${ms.position}</td>
<td>${ms.password}</td>
<td>${ms.tel}</td>
<td>${ms.lervel}</td>
<td width="100"><a href="?id=${ms.id}">更改</a></td>
<td width="100"><a href="?id=${ms.id}">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
information.jsp
六、配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<constructor-arg ref="sessionFactory"></constructor-arg>
</bean>
<bean id="informationservice" class="com.chinasofti.sh2.daoimpl.InformationImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean> <bean id = "informationaction" class="com.chinasofti.sh2.action.InformationAction">
<property name="service" ref="informationservice"></property>
</bean> </beans>
spring
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://192.168.8.80:1433
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="myeclipse.connection.profile">
SQLServerDriver
</property> <property name="connection.autocommit">true</property>
<property name="show_sql">true</property> <mapping resource="com/chinasofti/sh2/entity/Salary.hbm.xml" />
<mapping resource="com/chinasofti/sh2/entity/Information.hbm.xml" />
<mapping resource="com/chinasofti/sh2/entity/Attendance.hbm.xml" /> </session-factory> </hibernate-configuration>
Hibernate
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="test" namespace="/" extends="struts-default">
<action name="information" class="informationaction" method="list">
<result name="list" type="dispatcher">/information.jsp</result>
</action>
</package> </struts>
struts
Hibernate+jsp+struts+spring做增删该查,的更多相关文章
- hibernate与struts框架实现增删改查
这里配置hibernate与struts不再过多赘述,配置搭建前文已经详细讲解,配置如下: hibernate.hbm.xml配置: <?xml version="1.0" ...
- Struts+Spring+Hibernate、MVC、HTML、JSP
javaWeb应用 JavaWeb使用的技术,比如SSH(Struts.Spring.Hibernate).MVC.HTML.JSP等等技术,利用这些技术开发的Web应用在政府项目中非常受欢迎. 先说 ...
- Struts,spring,hibernate三大框架的面试
Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...
- 用eclipse搭建SSH(struts+spring+hibernate)框架
声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- 使用struts+spring+hibernate组装web应用
这篇文章将讨论怎样组合几个着名的框架去做到松耦合的目的,怎样建立你的构架,怎样让你的各个应用层保持一致.富于挑战的是:组合这些框架使得每一层都以一种松耦合的方式彼此沟通,而与底层的技术无关.这篇文章将 ...
- Struts+Spring+Hibernate进阶开端(一)
入行就听说SSH,起初还以为是一个东西,具体内容就更加不详细了,总觉得高端大气上档次,经过学习之后才发现,不仅仅是高大上,更是低调奢华有内涵,经过一段时间的研究和学习SSH框架的基本原理与思想,总算接 ...
- velocity+spring mvc+spring ioc+ibatis初试感觉(与struts+spring+hibernate比较)
velocity+spring mvc+spring ioc+ibatis框架是我现在公司要求采用的,原因是因为阿里巴巴和淘宝在使用这样的框架,而我公司现在还主要是以向阿里巴巴和淘宝输送外派人员为 主 ...
- Struts,Spring,Hibernate优缺点
Struts跟Tomcat.Turbine等诸 多Apache项目一样,是开源软件,这是它的一大优点.使开发者能更深入的了解其内部实现机制. Struts开放源码框架的创建是为了使开发者在构建基于Ja ...
随机推荐
- Linux下的tar压缩解压缩命令详解
转载自http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压 ...
- S5PV210的电阻触摸屏&ADC控制器
一.ADC与触摸屏控制器结构框图 1.S5PV210一共支持10路模拟输入,分别为AIN0-AIN9.其中AIN0和AIN1是只做模拟输入的,AIN2-AIN9分别可以支持2个电阻式触摸屏,所以这个就 ...
- [原创.数据可视化系列之一]使用openlayers 3 显示聚合数据
在地图上显示点数据是最常用的地图展示功能之一,但是如果很多点在地图上显示,或造成密密麻麻的一片,无法正常看清楚,这个时候,一般有两种解决方案,一种是根据数据重要程度进行标注,重要的显示大一些,不重要的 ...
- C#批量插入数据到Sqlserver中的三种方式
本篇,我将来讲解一下在Sqlserver中批量插入数据. 先创建一个用来测试的数据库和表,为了让插入数据更快,表中主键采用的是GUID,表中没有创建任何索引.GUID必然是比自增长要快的,因为你生 成 ...
- ORACLE分区--表分区
.love_flying_snow Oracle表分区 Oracle . 废话少说,直接讲分区语法. Oracle表分区分为四种:范围分区,散列分区,列表分区和复合分区. 一:范围分区 就是根据数据库 ...
- spring的事物实现
Spring的事物主要有三个接口 PlatformTransactionManager. 根据TransactionDefinition配置的事物信息创建事物 TransactionDefinitio ...
- debug命令简介
debug命令不区分大小,debug的命令都是一个字母,后跟或不跟参数 1.debug [路径\文件] [参数] [参数]--[参数] debug相应程序 2. D(Dump) [地址] [范围] 显 ...
- NPOI操作excel
1.基本导出方法 private void ExportToExcel() { SaveFileDialog sdfExport = new SaveFileDialog(); sdfExport.F ...
- 为何PHP插入mysql的中文是乱码?【坑】
依然没有找到最终的解决方法,PHP插入的中文在phpmyadmin中看是乱码,但是用PHP获取之后显示正常: 可以在phpmyadmin中直接插入中文,在PHPmyadmin中显示正常,用PHP获取中 ...
- 消除a标签点击后产生的虚线框
为a标签添加这条属性: a:focus {outline:none;-moz-outline:none;}