Java Web开发 SSH配置文件案例详解(eclipse开发自存)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Web开发 SSH配置文件案例详解(eclipse开发自存)相关的知识,希望对你有一定的参考价值。
本文并没有具体的Dao,Service,Action代码实现,知识分享了下SSH整合框架中 那些配置文件的模板,仅供参考,有不对的地方欢迎大家指正
简单实例:实体类Departmentinfo 与 Employeeinfo 如下
package com.prj.bean; import java.util.HashSet; import java.util.Set; /** * Departmentinfo entity. @author MyEclipse Persistence Tools */ public class Departmentinfo implements java.io.Serializable { // Fields private Integer departmentId; private String departmentName; private Set employeeinfos = new HashSet(0); // Constructors /** default constructor */ public Departmentinfo() { } /** full constructor */ public Departmentinfo(String departmentName, Set employeeinfos) { this.departmentName = departmentName; this.employeeinfos = employeeinfos; } // Property accessors public Integer getDepartmentId() { return this.departmentId; } public void setDepartmentId(Integer departmentId) { this.departmentId = departmentId; } public String getDepartmentName() { return this.departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } public Set getEmployeeinfos() { return this.employeeinfos; } public void setEmployeeinfos(Set employeeinfos) { this.employeeinfos = employeeinfos; } }
package com.prj.bean; import java.sql.Timestamp; /** * Employeeinfo entity. @author MyEclipse Persistence Tools */ public class Employeeinfo implements java.io.Serializable { // Fields private Integer employeeNo; private Departmentinfo departmentinfo; private String employeeName; private String job; private Double salary; private Timestamp birthday; // Constructors /** default constructor */ public Employeeinfo() { } /** full constructor */ public Employeeinfo(Departmentinfo departmentinfo, String employeeName, String job, Double salary, Timestamp birthday) { this.departmentinfo = departmentinfo; this.employeeName = employeeName; this.job = job; this.salary = salary; this.birthday = birthday; } // Property accessors public Integer getEmployeeNo() { return this.employeeNo; } public void setEmployeeNo(Integer employeeNo) { this.employeeNo = employeeNo; } public Departmentinfo getDepartmentinfo() { return this.departmentinfo; } public void setDepartmentinfo(Departmentinfo departmentinfo) { this.departmentinfo = departmentinfo; } public String getEmployeeName() { return this.employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getJob() { return this.job; } public void setJob(String job) { this.job = job; } public Double getSalary() { return this.salary; } public void setSalary(Double salary) { this.salary = salary; } public Timestamp getBirthday() { return this.birthday; } public void setBirthday(Timestamp birthday) { this.birthday = birthday; } }
映射文件:Departmentinfo.hbm.xml 与 Employeeinfo.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.prj.bean.Departmentinfo" table="departmentinfo" catalog="employeedb"> <id name="departmentId" type="java.lang.Integer"> <column name="DepartmentId" /> <generator class="native" /> </id> <property name="departmentName" type="java.lang.String"> <column name="DepartmentName" length="20" /> </property> <set name="employeeinfos" inverse="true"> <key> <column name="Dept" /> </key> <one-to-many class="com.prj.bean.Employeeinfo" /> </set> </class> </hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.prj.bean.Employeeinfo" table="employeeinfo" catalog="employeedb"> <id name="employeeNo" type="java.lang.Integer"> <column name="EmployeeNo" /> <generator class="native" /> </id> <many-to-one name="departmentinfo" class="com.prj.bean.Departmentinfo" fetch="select"> <column name="Dept" /> </many-to-one> <property name="employeeName" type="java.lang.String"> <column name="EmployeeName" length="20" /> </property> <property name="job" type="java.lang.String"> <column name="Job" length="20" /> </property> <property name="salary" type="java.lang.Double"> <column name="Salary" precision="10" /> </property> <property name="birthday" type="java.sql.Timestamp"> <column name="Birthday" length="19" /> </property> </class> </hibernate-mapping>
主配置文件:hibernate.cfg.xml
<?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="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="com/prj/bean/Departmentinfo.hbm.xml" /> <mapping resource="com/prj/bean/Employeeinfo.hbm.xml" /> </session-factory> </hibernate-configuration>
struts.xml
<?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="emp" extends="struts-default" namespace="/"> <action name="employee" class="employeeActionBean"> <result name="succ">/index.jsp</result> <result name="remove_succ" type="redirect">/index.jsp</result> </action> <action name="department" class="departmentActionBean"> <result name="succ">/index.jsp</result> </action> </package> </struts>
applicationContext.xml
<?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"> <!-- DataSource数据源 --> <bean id="dataSourceBean" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> </property> <property name="url" value="jdbc:mysql://localhost:3306/employeedb?useUnicode=true&characterEncoding=utf-8"> </property> <property name="username" value="root"></property> <property name="password" value="liweidong"></property> </bean> <!-- SessionFactory --> <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入Hibernate的主配置文件hibernate.cfg.xml --> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> <!-- 注入DataSource数据源 --> <property name="dataSource" ref="dataSourceBean"/> </bean> <!-- DAO --> <!-- 抽象Bean --> <bean id="baseDaoBean" abstract="true"> <property name="sessionFactory" ref="sessionFactoryBean"/> </bean> <bean id="employeeDaoBean" class="com.prj.dao.impl.EmployeeDao" parent="baseDaoBean"/> <bean id="departmentDaoBean" class="com.prj.dao.impl.DepartmentDao" parent="baseDaoBean"/> <!-- 业务层 --> <bean id="employeeServiceBean" class="com.prj.biz.impl.EmployeeService"> <!-- 注入数据访问对象 --> <property name="employeeDao" ref="employeeDaoBean"/> </bean> <bean id="departmentServiceBean" class="com.prj.biz.impl.DepartmentService"> <!-- 注入数据访问对象 --> <property name="departmentDao" ref="departmentDaoBean"/> </bean> <!-- WEB层 --> <bean id="employeeActionBean" class="com.prj.web.action.EmployeeAction"> <!-- 注入业务对象 --> <property name="employeeService" ref="employeeServiceBean"/> </bean> <bean id="departmentActionBean" class="com.prj.web.action.DeparmentAction"> <!-- 注入业务对象 --> <property name="departmentService" ref="departmentServiceBean"/> </bean> </beans>
web.xml 的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- OpenSessionInViewFilter:用于解决延迟加载异常 --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <!-- 设置spring容器中SessionFactory的bean名称 --> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactoryBean</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ContextLoaderListener的作用:在服务器启动时,加载Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 默认在WEB-INF下加载spring配置文件 --> <!-- 设置contextConfigLocation参数,用于指定spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
以上是关于Java Web开发 SSH配置文件案例详解(eclipse开发自存)的主要内容,如果未能解决你的问题,请参考以下文章