同样还是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做增删该查,的更多相关文章

  1. hibernate与struts框架实现增删改查

    这里配置hibernate与struts不再过多赘述,配置搭建前文已经详细讲解,配置如下: hibernate.hbm.xml配置: <?xml version="1.0" ...

  2. Struts+Spring+Hibernate、MVC、HTML、JSP

    javaWeb应用 JavaWeb使用的技术,比如SSH(Struts.Spring.Hibernate).MVC.HTML.JSP等等技术,利用这些技术开发的Web应用在政府项目中非常受欢迎. 先说 ...

  3. Struts,spring,hibernate三大框架的面试

    Struts,spring,hibernate三大框架的面试 1.Hibernate工作原理及为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3 ...

  4. 用eclipse搭建SSH(struts+spring+hibernate)框架

    声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...

  5. Struts+Spring+Hibernate整合入门详解

    Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者:  Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念       St ...

  6. 使用struts+spring+hibernate组装web应用

    这篇文章将讨论怎样组合几个着名的框架去做到松耦合的目的,怎样建立你的构架,怎样让你的各个应用层保持一致.富于挑战的是:组合这些框架使得每一层都以一种松耦合的方式彼此沟通,而与底层的技术无关.这篇文章将 ...

  7. Struts+Spring+Hibernate进阶开端(一)

    入行就听说SSH,起初还以为是一个东西,具体内容就更加不详细了,总觉得高端大气上档次,经过学习之后才发现,不仅仅是高大上,更是低调奢华有内涵,经过一段时间的研究和学习SSH框架的基本原理与思想,总算接 ...

  8. velocity+spring mvc+spring ioc+ibatis初试感觉(与struts+spring+hibernate比较)

    velocity+spring mvc+spring ioc+ibatis框架是我现在公司要求采用的,原因是因为阿里巴巴和淘宝在使用这样的框架,而我公司现在还主要是以向阿里巴巴和淘宝输送外派人员为 主 ...

  9. Struts,Spring,Hibernate优缺点

    Struts跟Tomcat.Turbine等诸 多Apache项目一样,是开源软件,这是它的一大优点.使开发者能更深入的了解其内部实现机制. Struts开放源码框架的创建是为了使开发者在构建基于Ja ...

随机推荐

  1. MSSQL日志传送出现“LSN 太晚,无法应用到数据库”

    一个月之前配置了日志传送的数据库,在今天早上收到作业警报:"LSRestore_ServerName_Databasename"运行失败,到历史记录中查看,错误信息如下 消息 20 ...

  2. UIViewController中addChildViewController的作用

    当在一个ViewController中添加一个子ViewController时,UI部分可以直接通过addSubView的方法添加,例如: 在一个ViewControllerA中添加ViewContr ...

  3. prolog 内部谓词

    内部谓词 和其他语言一样,prolog也提供一些基本的输入输出函数. 内部谓词是指已经在prolog中事先定义好的谓词,在内存中的动态数据库中是没有内部谓词子句的.(当我们运行某个.pl 文件的时候, ...

  4. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  5. [ZT]嵌入视频播放器代码

    http://www.cnblogs.com/liulanglang/archive/2007/11/29/976638.html 页面插入REAL播放器代码: < id=video1 styl ...

  6. Canvas画图在360浏览器中跑偏的问题

    问题描述,canvas画图的js代码中编写的是画正方形的代码,结果在360浏览器上变成了长方形,不知道怎么回事,请问各位大神是否遇到过此类问题? <!DOCTYPE html> <h ...

  7. Oracle并行执行特性应用初探

    1.      序 在历史数据转出测试过程中,通过不断的优化,包括SQL调整和数据库调整,从AWR中看到,基本上难以进行更多的性能提升,于是准备试试并行执行的特性,从这个任务的特点来分析,也比较适合采 ...

  8. JavaScript DOM编程艺术读书笔记(二)

    第五章 最佳实践 平稳退化(graceful degradation):如果正确使用了JavaScript脚本,可以让访问者在他们的浏览器不支持JavaScript的情况下仍能顺利地浏览你网站.虽然某 ...

  9. Android_demo之生成二维码

    今天我们来学习一个自动生成二维码 的写法.我们经常能见到各种二维码,比如公众号的二维码,网址的,加好友的,支付的二维码等等.其实每一个二维码只是利用图片的形式展示出来的,实际是一些字符串.而这个字符串 ...

  10. etc这个目录

    自己对他的记忆最深了,因为每次你添加新的软件向电脑里时,软件都会有一个自己的配置文件,那么你修改这个配置文件的某个选项,就可以改变软件的某个功能. 或者是某个外设都有自己的配置文件. 其实这个配置文件 ...