MyEclipse SSH初体验

来源:互联网 发布:网络推广怎么拿提成 编辑:程序博客网 时间:2024/03/29 14:51

 

17:44

SSHStruts+spring+hibernate框架.

本次体验使用的Struts2+spring3+hibernate3

1,创建ssh工程.

2,添加hibernate访问数据库

Windows->show view->dbExplorer

●创建db连接,本次体验使用mysql作为体验数据库,其他数据库应该类似

选择配置的连接右键->openconnection建立数据库连接.

●添加hibernate配置:两种方式:POJODao/SpringDao两者没有太大差异只是在配置时略有不同)

如果使用Spring Dao则需要先引用Spring支持Library

右键projectMyEclipse->Add Spring Cap….

一路默认即可

添加Hibernate支持:Project->右键->MyEclipse->add hibernate cap…

Next如果Pojo则选择Hibernate.xfg.xml,如果SpringDao则选择applicatonContext.xml

根据需要选择xml文件

选择数据库连接配置

选择实体类生成文件存储路径和包

Finish完成Hibernate配置

添加Spring persistence jdbc libraries工程右键->properties->javabuild path->MyEclipse->spring xxx persistence jdbc libraries

●生成HibernateDao实体数据

数据库连接中选择schma->Table右键->Hibernate Reverse Engineering来生成Dao

如果为pojodao选择basicdao

如果springdao选择spring dao

nextidgenerator中选择native根据需要选择one-to-many/many-to-one等选项

Next->finish则在相应的package中将生成对应的xxxdao.java,xxx.hbm.xml等文件

●添加业务层逻辑

添加相应处理接口这里使用IOwenerService接口,添加getOwners()

public interface IOwnerService {

public List getOwners();

}

 

添加OwenerService继承自IOwenerService,然后添加相应的Dao对象作为OwenerService的成员。

然后生成getter/Setter

public class OwnerServiceimplements IOwnerService {

public OwnersDAO getOwnerDao() {

return ownerDao;

}

public voidsetOwnerDao(OwnersDAO ownerDao) {

this.ownerDao = ownerDao;

}

private OwnersDAO ownerDao; //根据需要变更

public List getOwners() {

// TODO Auto-generated methodstub                

return ownerDao.findAll();

}

}

●添加表现层处理

Project->右键->Add Struts cap….

 

选择struts2 core/struts 2 spring(strutsspring的连接必须选择)

Finish完成Struts的添加

 

添加页面以及相应的action

action

publicclass ListAction extends ActionSupport {

publicCollection getOwners() {

returnowners;

}

public voidsetOwenrSrv(IOwnerService owenrSrv) {

this.owenrSrv = owenrSrv;

}

private IOwnerService owenrSrv;

privateCollection owners;

/**

 * @return

 */

publicString execute() {

//TODO Auto-generated method stub

owners= this.owenrSrv.getOwners();

returnSUCCESS;

}

}

 

Index.jsp

<%@page language="java" import="java.util.*"pageEncoding="UTF-8"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<%

Stringpath = request.getContextPath();

StringbasePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <basehref="<%=basePath%>">

   

    <title>My JSP 'index.jsp' startingpage</title>

<metahttp-equiv="pragma" content="no-cache">

<metahttp-equiv="cache-control" content="no-cache">

<metahttp-equiv="expires" content="0">   

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description" content="This is my page">

<!--

<linkrel="stylesheet" type="text/css"href="styles.css">

-->

  </head>

 

  <body>

     <p><a href="<s:urlaction='listAction'/>">List</a></p><br>

  </body>

</html>

 

List.jsp

<%@page language="java" import="java.util.*"pageEncoding="UTF-8"%>

<%@taglib prefix="s" uri="/struts-tags"%>

<%

Stringpath = request.getContextPath();

StringbasePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <basehref="<%=basePath%>">

   

    <title>My JSP 'List.jsp' startingpage</title>

   

<metahttp-equiv="pragma" content="no-cache">

<metahttp-equiv="cache-control" content="no-cache">

<metahttp-equiv="expires" content="0">   

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description" content="This is my page">

<!--

<linkrel="stylesheet" type="text/css"href="styles.css">

-->

 

  </head>

 

  <body>

  <table>

         <tbody>

           <s:iteratorvalue="items">

           <tr>

           <td>

                   <a><s:propertyvalue="Username"/></a>

           </td>

           <td>

                   <a><s:propertyvalue="Descn"/></a>

           </td>

           </tr>

           </s:iterator>

    </tbody>

  </table>

  </body>

</html>

 

xml配置

Struts.xml配置如下

<?xmlversion="1.0" encoding="UTF-8" ?>

<!DOCTYPEstruts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<constantname="struts.enable.DynamicMethodInvocation" value="false"/> 

    <constantname="struts.devMode" value="false" />  

    <constant name="struts.objectFactory"value="spring"/>

   

 

<packagename="default" extends="struts-default">

<!-- listActionBean 对应到applicationContextbean -->

<actionname="listAction" class="listActionBean">

<result>/List.jsp</result></action></package></struts> 

 

Applicationcontext.xml

<?xmlversion="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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 

 

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">

<propertyname="driverClassName"

value="com.mysql.jdbc.Driver">

</property>

<propertyname="url"value="jdbc:mysql://localhost:3306"></property>

<propertyname="username" value="root"></property>

<propertyname="password" value="sa"></property>

</bean>

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<propertyname="dataSource">

<refbean="dataSource" />

</property>

<propertyname="hibernateProperties">

<props>

<propkey="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<propkey="hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

<propertyname="mappingResources">

<list>

<value>./sshDemo/Entities/Owners.hbm.xml</value>

<value>./sshDemo/Entities/Pets.hbm.xml</value>

<value>./sshDemo/Entities/Types.hbm.xml</value>

<value>./Visits.hbm.xml</value>

</list>

</property>

</bean>

<beanid="OwnersDAO" class="sshDemo.Entities.OwnersDAO">

<propertyname="sessionFactory">

<refbean="sessionFactory" />

</property>

</bean>

<beanid="PetsDAO" class="sshDemo.Entities.PetsDAO">

<propertyname="sessionFactory">

<refbean="sessionFactory" />

</property>

</bean>

<beanid="TypesDAO" class="sshDemo.Entities.TypesDAO">

<propertyname="sessionFactory">

<refbean="sessionFactory" />

</property>

</bean>

<beanid="VisitsDAO" class="sshDemo.Entities.VisitsDAO">

<propertyname="sessionFactory">

<refbean="sessionFactory" />

</property>

</bean>

<!--依赖注入-->

<beanid="ownerSerivce" class="sshDemo.bll.OwnerService">

<propertyname="ownerDao">

<refbean="OwnersDAO" />

</property>

</bean>

<beanid="listActionBean" class="sshDemo.Actions.ListAction">

<propertyname="owenrSrv">

<refbean="ownerSerivce" />

</property>

</bean>

</beans>

 

Web.xml配置

<?xmlversion="1.0" encoding="UTF-8"?>

<web-appversion="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">

 <context-param>

         <param-name>contextConfigLocation</param-name>

         <param-value>/WEB-INF/classes/applicationContext.xml</param-value>

 </context-param>

 <filter>

 <filter-name>struts2</filter-name>

 <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

 </filter>

 <filter-mapping>

 <filter-name>struts2</filter-name>

  <url-pattern>/*</url-pattern>

 </filter-mapping>

 <welcome-file-list>

 <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

 <login-config>

  <auth-method>BASIC</auth-method>

 </login-config>

 <listener>

         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

 </listener>

</web-app>

至此,已经完成了ssh架构的初次体验。

 

原创粉丝点击