同样还是web项目,这里只做了一张表,做一个测试,例子。主要是建Hibernate 的时候要非常注意,有时间了整理一下建Hiberbnate 的时候需要注意的事项
这里我是建了5个包,其实只要四个就好,那个service包和action包可以放在一起的,其他的是1.实体包;2.接口包(写方法名的);3.接口实现包(实现接口的方法);4.action包(数据逻辑)
一、实体包,这里是自动生成的,但是强迫症还是把它放在这里了啊
1 package com.chinasofti.sh2.entity; 2 3 /** 4 * Information entity. @author MyEclipse Persistence Tools 5 */ 6 7 @SuppressWarnings("serial") 8 public class Information implements java.io.Serializable { 9 10 // Fields 11 12 private Integer id; 13 private String name; 14 private String department; 15 private String position; 16 private String password; 17 private String tel; 18 private String lervel; 19 20 // Constructors 21 22 /** default constructor */ 23 public Information() { 24 } 25 26 /** full constructor */ 27 public Information(String name, String department, String position, 28 String password, String tel, String lervel) { 29 this.name = name; 30 this.department = department; 31 this.position = position; 32 this.password = password; 33 this.tel = tel; 34 this.lervel = lervel; 35 } 36 37 // Property accessors 38 39 public Integer getId() { 40 return this.id; 41 } 42 43 public void setId(Integer id) { 44 this.id = id; 45 } 46 47 public String getName() { 48 return this.name; 49 } 50 51 public void setName(String name) { 52 this.name = name; 53 } 54 55 public String getDepartment() { 56 return this.department; 57 } 58 59 public void setDepartment(String department) { 60 this.department = department; 61 } 62 63 public String getPosition() { 64 return this.position; 65 } 66 67 public void setPosition(String position) { 68 this.position = position; 69 } 70 71 public String getPassword() { 72 return this.password; 73 } 74 75 public void setPassword(String password) { 76 this.password = password; 77 } 78 79 public String getTel() { 80 return this.tel; 81 } 82 83 public void setTel(String tel) { 84 this.tel = tel; 85 } 86 87 public String getLervel() { 88 return this.lervel; 89 } 90 91 public void setLervel(String lervel) { 92 this.lervel = lervel; 93 } 94 95 }
Information.java
二、接口包。里面有方法名,可是没有实际的方法
1 package com.chinasofti.sh2.dao; 2 3 import java.util.List; 4 5 import com.chinasofti.sh2.entity.Information; 6 7 8 9 public interface IInformationDAO { 10 void Insert(Information model); 11 void Update(Information model); 12 void Delete(Information model); 13 Information GetInformationById(int id); 14 List<Information> GetInformation(); 15 List<Information> GetInformationPaging(int pageIndex,int pageSize); 16 }
IInformationDAO
三、接口实现包
1 package com.chinasofti.sh2.daoimpl; 2 3 import java.util.List; 4 5 import org.hibernate.SessionFactory; 6 import org.springframework.orm.hibernate3.HibernateTemplate; 7 8 import com.chinasofti.sh2.dao.IInformationDAO; 9 import com.chinasofti.sh2.entity.Information; 10 11 12 public class InformationImpl implements IInformationDAO{ 13 private SessionFactory sessionFactory; 14 private HibernateTemplate hibernateTemplate; 15 16 public SessionFactory getSessionFactory() { 17 return sessionFactory; 18 } 19 20 public void setSessionFactory(SessionFactory sessionFactory) { 21 this.sessionFactory = sessionFactory; 22 } 23 24 public HibernateTemplate getHibernateTemplate() { 25 return hibernateTemplate; 26 } 27 28 public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { 29 this.hibernateTemplate = hibernateTemplate; 30 } 31 32 @Override 33 public void Insert(Information model) { 34 // TODO Auto-generated method stub 35 hibernateTemplate.save(model); 36 } 37 38 @Override 39 public void Update(Information model) { 40 // TODO Auto-generated method stub 41 hibernateTemplate.update(model); 42 } 43 44 @Override 45 public void Delete(Information model) { 46 // TODO Auto-generated method stub 47 hibernateTemplate.delete(model); 48 } 49 50 @Override 51 public Information GetInformationById(int id) { 52 // TODO Auto-generated method stub 53 return hibernateTemplate.get(Information.class, id); 54 } 55 56 @Override 57 public List<Information> GetInformation() { 58 // TODO Auto-generated method stub 59 return null; 60 } 61 62 @Override 63 public List<Information> GetInformationPaging(int pageIndex, int pageSize) { 64 // TODO Auto-generated method stub 65 return this.getHibernateTemplate().loadAll(Information.class).subList((pageIndex-1)*pageSize, pageIndex*pageSize); 66 } 67 68 69 70 71 72 73 }
InformationImpl.java
四、Action包,处理逻辑问题的,
1 package com.chinasofti.sh2.service; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 7 import com.chinasofti.sh2.dao.IInformationDAO; 8 import com.chinasofti.sh2.daoimpl.InformationImpl; 9 import com.chinasofti.sh2.entity.Information; 10 11 public class InformationService { 12 13 /** 14 * @param args 15 */ 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 19 InformationImpl ii=(InformationImpl)context.getBean("informationservice"); 20 //1.增加 21 // Information i=new Information(); 22 // i.setName("王二小"); 23 // i.setPassword("123123"); 24 // i.setLervel("员工"); 25 // i.setDepartment("654654"); 26 // i.setPosition("4546"); 27 // i.setTel("5"); 28 // ii.Insert(i); 29 //2.删除 30 // Information i=new Information(); 31 // i.setName("王二小"); 32 // i.setPassword("123123"); 33 // i.setLervel("员工"); 34 // i.setDepartment("654654"); 35 // i.setPosition("4546"); 36 // i.setTel("5"); 37 // i.setId(31); 38 // ii.Delete(i); 39 40 //3.更改 41 // Information i=new Information(); 42 // i.setName("111"); 43 // i.setPassword("111"); 44 // i.setLervel("111"); 45 // i.setDepartment("111"); 46 // i.setPosition("111"); 47 // i.setTel("111"); 48 // i.setId(25); 49 // ii.Update(i); 50 //4.根据ID查找 51 52 Information i= ii.GetInformationById(1); 53 System.out.println(i.getName()+"."+i.getLervel()); 54 } 55 56 }
InformationServace.java
1 package com.chinasofti.sh2.action; 2 3 4 import java.util.List; 5 6 import com.chinasofti.sh2.dao.IInformationDAO; 7 import com.chinasofti.sh2.entity.Information; 8 9 10 11 public class InformationAction { 12 private IInformationDAO service; 13 14 private int pageSize=5;//分页的 15 private int pageIndex=1; //分页的 16 17 private List<Information> result; 18 19 20 public IInformationDAO getService() { 21 return service; 22 } 23 24 public void setService(IInformationDAO service) { 25 this.service = service; 26 } 27 28 public int getPageSize() { 29 return pageSize; 30 } 31 32 public void setPageSize(int pageSize) { 33 this.pageSize = pageSize; 34 } 35 36 public int getPageIndex() { 37 return pageIndex; 38 } 39 40 public void setPageIndex(int pageIndex) { 41 this.pageIndex = pageIndex; 42 } 43 44 public List<Information> getResult() { 45 return result; 46 } 47 48 public void setResult(List<Information> result) { 49 this.result = result; 50 } 51 52 public String list(){ 53 //输入输出的检测,要在Action里面写。 54 result=service.GetInformationPaging(pageIndex,pageSize);//分页的 55 return "list"; 56 } 57 58 59 }
InformationAction.java
五。jsp界面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>My JSP ‘information.jsp‘ starting page</title> 14 15 <style type="text/css"> 16 tr { 17 text-align: center; 18 font-family: "微软雅黑"; 19 font-size: 14px; 20 background-color: #003; 21 color:#F90; 22 } 23 </style> 24 25 </head> 26 27 <body> 28 <table width="80%" border="1" bgcolor="celeste"> 29 <tr> 30 <td width="100">员工ID</td> 31 <td width="100">名字</td> 32 <td width="100">所在部门</td> 33 <td width="100">岗位</td> 34 <td width="100">员工密码</td> 35 <td width="100">电话号码</td> 36 <td width="100">级别</td> 37 <td width="300" colspan="3"><a href="">增加</a></td> 38 </tr> 39 <c:forEach var="ms" items="${result}"> 40 <tr> 41 <td>${ms.id}</td> 42 <td>${ms.name}</td> 43 <td>${ms.department}</td> 44 <td>${ms.position}</td> 45 <td>${ms.password}</td> 46 <td>${ms.tel}</td> 47 <td>${ms.lervel}</td> 48 <td width="100"><a href="?id=${ms.id}">更改</a></td> 49 <td width="100"><a href="?id=${ms.id}">删除</a></td> 50 </tr> 51 </c:forEach> 52 </table> 53 </body> 54 </html>
information.jsp
六、配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 7 8 9 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 10 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 11 </bean> 12 13 14 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 15 <constructor-arg ref="sessionFactory"></constructor-arg> 16 </bean> 17 <bean id="informationservice" class="com.chinasofti.sh2.daoimpl.InformationImpl"> 18 <property name="hibernateTemplate" ref="hibernateTemplate"></property> 19 </bean> 20 21 <bean id = "informationaction" class="com.chinasofti.sh2.action.InformationAction"> 22 <property name="service" ref="informationservice"></property> 23 </bean> 24 25 26 27 </beans>
spring
1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <!-- Generated by MyEclipse Hibernate Tools. --> 6 <hibernate-configuration> 7 8 <session-factory> 9 <property name="dialect"> 10 org.hibernate.dialect.SQLServerDialect 11 </property> 12 <property name="connection.url"> 13 jdbc:sqlserver://192.168.8.80:1433 14 </property> 15 <property name="connection.username">sa</property> 16 <property name="connection.password">123456</property> 17 <property name="connection.driver_class"> 18 com.microsoft.sqlserver.jdbc.SQLServerDriver 19 </property> 20 <property name="myeclipse.connection.profile"> 21 SQLServerDriver 22 </property> 23 24 25 <property name="connection.autocommit">true</property> 26 <property name="show_sql">true</property> 27 28 29 <mapping resource="com/chinasofti/sh2/entity/Salary.hbm.xml" /> 30 <mapping resource="com/chinasofti/sh2/entity/Information.hbm.xml" /> 31 <mapping resource="com/chinasofti/sh2/entity/Attendance.hbm.xml" /> 32 33 </session-factory> 34 35 </hibernate-configuration>
Hibernate
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4 <package name="test" namespace="/" extends="struts-default"> 5 <action name="information" class="informationaction" method="list"> 6 <result name="list" type="dispatcher">/information.jsp</result> 7 </action> 8 </package> 9 10 11 12 </struts>
struts
时间: 2024-10-08 09:27:35