Hibernate+jsp+struts+spring做增删该查,
2024-10-15 11:05:58
同样还是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
最新文章
- 《CLR.via.C#第三版》第二部分第12章节 泛型 读书笔记(六)
- 怎样让 Web 项目暴露在外的服务坚不可摧?
- 黄聪:使用srvany.exe将任何程序作为Windows服务运行
- Editor扩展之查看Prefab用在那儿
- apache配置虚拟主机
- php判断浏览器语言
- Neusoft(3)增加自己的内核模块
- MINIX3 进程调度分析
- [搜片神器]之DHT网络爬虫的C++程序初步开源
- How to log in to Amazon EC2 using PEM format from SecureCRT
- UVA 714 Copying Books 二分
- ExifInterface 多媒体文件附加信息
- vue-router2.x
- Java第九周学习总结
- ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQL
- 微信小程序爬坑日记
- python bz2模块
- 你不知道的javascript读书笔记3
- php的常量
- 实现一套山寨springMVC