myeclipse ssh mysql_MyEclipse SSH初体验

17:44

SSH即Struts+spring+hibernate框架.

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

1,创建ssh工程.

1704493_1294226961Sf1k.png

2,添加hibernate访问数据库

Windows->show view->dbExplorer

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

1704493_1294226962gZlb.png

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

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

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

右键project:MyEclipse->Add Spring Cap….

1704493_12942269643Bl5.png

一路默认即可

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

1704493_12942269667xb5.png

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

根据需要选择xml文件

1704493_1294226967SQSW.png

选择数据库连接配置

1704493_1294226968oSSd.png

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

Finish完成Hibernate配置

添加Spring persistence jdbc libraries工程右键->properties->java

build path->MyEclipse->spring xxx persistence jdbc libraries

●生成HibernateDao实体数据

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

1704493_1294226969rtR9.png

如果为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….

1704493_12942269708Iqx.png

选择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+"/";

%>

/p>

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

href="">

My JSP 'index.jsp' starting

page

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

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

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

http-equiv="keywords"

content="keyword1,keyword2,keyword3">

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

List

●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+"/";

%>

/p>

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

href="">

My JSP 'List.jsp' starting

page

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

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

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

http-equiv="keywords"

content="keyword1,keyword2,keyword3">

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

value="items">

value="Username"/>

value="Descn"/>

●xml配置

Struts.xml配置如下

version="1.0" encoding="UTF-8" ?>

/p>

struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration

2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

name="struts.enable.DynamicMethodInvocation" value="false"

/>

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

value="spring"/>

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

name="listAction" class="listActionBean">

/List.jsp

Applicationcontext.xml

version="1.0" encoding="UTF-8"?>

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

id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource">

name="driverClassName"

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

name="url"

value="jdbc:mysql://localhost:3306">

name="username" value="root">

name="password" value="sa">

id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

name="dataSource">

bean="dataSource" />

name="hibernateProperties">

key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

key="hibernate.hbm2ddl.auto">update

name="mappingResources">

./sshDemo/Entities/Owners.hbm.xml

./sshDemo/Entities/Pets.hbm.xml

./sshDemo/Entities/Types.hbm.xml

./Visits.hbm.xml

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

name="sessionFactory">

bean="sessionFactory" />

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

name="sessionFactory">

bean="sessionFactory" />

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

name="sessionFactory">

bean="sessionFactory" />

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

name="sessionFactory">

bean="sessionFactory" />

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

name="ownerDao">

bean="OwnersDAO" />

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

name="owenrSrv">

bean="ownerSerivce" />

Web.xml配置

version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

contextConfigLocation

/WEB-INF/classes/applicationContext.xml

struts2

org.apache.struts2.dispatcher.FilterDispatcher

struts2

/*

index.jsp

BASIC

org.springframework.web.context.ContextLoaderListener

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值