MyEclipse SSH初體驗

來源:互聯網
上載者:User

 

17:44

SSH即Struts+spring+hibernate架構.

本次體驗使用的Struts2+spring3+hibernate3

1,建立ssh工程.

2,添加hibernate訪問資料庫

Windows->show view->dbExplorer

●建立db串連,本次體驗使用mysql作為體驗資料庫,其他資料庫應該類似

●選擇配置的串連右鍵->openconnection建立資料庫連接.

●添加hibernate配置:兩種方式:POJODao/SpringDao兩者沒有太大差異只是在配置時略有不同)

如果使用Spring Dao則需要先引用Spring支援Library

右鍵project:MyEclipse->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->java
build path->MyEclipse->spring xxx persistence jdbc libraries

●產生HibernateDao實體資料

資料庫連接中選擇schma->Table右鍵->Hibernate Reverse Engineering來產生Dao

如果為pojodao選擇basicdao

如果springdao選擇spring dao

next在id
generator中選擇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 OwnerService
implements IOwnerService {

public OwnersDAO getOwnerDao() {

return ownerDao;

}

public void
setOwnerDao(OwnersDAO ownerDao) {

this.ownerDao = ownerDao;

}

private OwnersDAO ownerDao; //根據需要變更

public List getOwners() {

// TODO Auto-generated method
stub                

return ownerDao.findAll();

}

}

●添加表現層處理

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

 

選擇struts2 core/struts 2 spring(從struts到spring的串連必須選擇)

Finish完成Struts的添加

 

添加頁面以及相應的action

●action

public
class ListAction extends ActionSupport {

public
Collection getOwners() {

return
owners;

}

public void
setOwenrSrv(IOwnerService owenrSrv) {

this.owenrSrv = owenrSrv;

}

private IOwnerService owenrSrv;

private
Collection owners;

/**

 * @return

 */

public
String execute() {

//
TODO Auto-generated method stub

owners
= this.owenrSrv.getOwners();

return
SUCCESS;

}

}

 

●Index.jsp

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

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

<%

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 'index.jsp' starting
page</title>

<meta
http-equiv="pragma" content="no-cache">

<meta
http-equiv="cache-control" content="no-cache">

<meta
http-equiv="expires" content="0">   

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

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

<!--

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

-->

  </head>

 

  <body>

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

  </body>

</html>

 

●List.jsp

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

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

<%

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 'List.jsp' starting
page</title>

   

<meta
http-equiv="pragma" content="no-cache">

<meta
http-equiv="cache-control" content="no-cache">

<meta
http-equiv="expires" content="0">   

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

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

<!--

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

-->

 

  </head>

 

  <body>

  <table>

 
        <tbody>

   
        <s:iterator
value="items">

   
        <tr>

   
        <td>

   
                <a><s:property
value="Username"/></a>

   
        </td>

   
        <td>

   
                <a><s:property
value="Descn"/></a>

   
        </td>

   
        </tr>

   
        </s:iterator>

    </tbody>

  </table>

  </body>

</html>

 

●xml配置

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>

<constant
name="struts.enable.DynamicMethodInvocation" value="false"
/> 

    <constant
name="struts.devMode" value="false" />  

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

   

 

<package
name="default" extends="struts-default">

<!-- listActionBean 對應到applicationContext的bean -->

<action
name="listAction" class="listActionBean">

<result>/List.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">

 

 

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

<property
name="driverClassName"

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

</property>

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

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

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

</bean>

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

<property
name="dataSource">

<ref
bean="dataSource" />

</property>

<property
name="hibernateProperties">

<props>

<prop
key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

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

</props>

</property>

<property
name="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>

<bean
id="OwnersDAO" class="sshDemo.Entities.OwnersDAO">

<property
name="sessionFactory">

<ref
bean="sessionFactory" />

</property>

</bean>

<bean
id="PetsDAO" class="sshDemo.Entities.PetsDAO">

<property
name="sessionFactory">

<ref
bean="sessionFactory" />

</property>

</bean>

<bean
id="TypesDAO" class="sshDemo.Entities.TypesDAO">

<property
name="sessionFactory">

<ref
bean="sessionFactory" />

</property>

</bean>

<bean
id="VisitsDAO" class="sshDemo.Entities.VisitsDAO">

<property
name="sessionFactory">

<ref
bean="sessionFactory" />

</property>

</bean>

<!--
依賴注入
-->

<bean
id="ownerSerivce" class="sshDemo.bll.OwnerService">

<property
name="ownerDao">

<ref
bean="OwnersDAO" />

</property>

</bean>

<bean
id="listActionBean" class="sshDemo.Actions.ListAction">

<property
name="owenrSrv">

<ref
bean="ownerSerivce" />

</property>

</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">

 <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架構的初次體驗。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.