首页 技术 正文
技术 2022年11月10日
0 收藏 728 点赞 2,887 浏览 9098 个字

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

Hibernate+jsp+struts+spring做增删该查,

这里我是建了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%>" rel="external nofollow" > <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="" rel="external nofollow" >增加</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}" rel="external nofollow" rel="external nofollow" >更改</a></td>
<td width="100"><a href="?id=${ms.id}" rel="external nofollow" rel="external nofollow" >删除</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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289