[]转]Xfire Spring Hibernate 发布WebService

一、 准备工作
1、下载安装Eclipse Version: 3.3.1.1 和MyEclipse6.0.1
2、下载安装MySQL Server 5.1
3、下载MySQL驱动 mysql-connector-java-5.0.8-bin.jar
4、 使用MySQL管理工具创建数据库easyLife和一张user表。我使用的是EMS SQL Manager 2007 for MySQL。
表结构如下:
[table]
|id|integer|
|username|Varchar(20)|
|userpwd|Varchar(20)|
[/table]
插入一条数据待测试用。

二、实例代码

连接数据库

打开MyEclipse Database Explorer 新建一个数据连接:
[img]http://dl2.iteye.com/upload/attachment/0099/0363/2ee134d8-d8d4-34f8-af21-de467b240b36.jpg[/img]

创建项目
1、打开MyEclipse 新建一个Web Service Project项目输入项目名称XfireWebService。
[img]http://dl2.iteye.com/upload/attachment/0099/0365/3234ad8c-c2e2-3a04-a4d5-70a9dd93f123.jpg[/img]

2、下一步默认
[img]http://dl2.iteye.com/upload/attachment/0099/0367/28d21908-cfa3-3762-bc9a-16b32f754ed1.jpg[/img]

3、下一步,勾选XFire 1.2 HTTP Client Libraries。
[img]http://dl2.iteye.com/upload/attachment/0099/0369/f655858e-24c8-3c6f-a770-c129fbf94562.jpg[/img]

4、点击View and edit libraries 去掉spring-1.2.6.jar 这个包(不去掉会和后面添加的Spring冲突)
5、右键添加MyEclipse的Spring Capabilities,方便起见选上所有的包,选择Copy checked…,下一步默认完成。
[img]http://dl2.iteye.com/upload/attachment/0099/0371/564c85b1-34f7-3414-9757-7e4c6ed19e08.jpg[/img]

6、再添加Hibernate Capabilities,同样选上所有包。
[img]http://dl2.iteye.com/upload/attachment/0099/0373/647666f9-bb11-322c-a08f-be4c34f759ee.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0099/0375/7691bc70-9f5c-3342-a729-5eb31433e20a.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0099/0377/c6f963e5-ee97-39f7-a4ff-cd2e137a0e38.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0099/0379/372856bf-2fec-3248-ab26-29f8828b9c07.jpg[/img]

下一步去掉复选框完成。
[img]http://dl2.iteye.com/upload/attachment/0099/0381/9f4040dc-87da-36ad-bb8f-272c0d68da6e.jpg[/img]

点击Replace替换所有包
[img]http://dl2.iteye.com/upload/attachment/0099/0389/d1bd5bd3-15b3-30b8-84e0-582e5283f6fa.jpg[/img]

7、然后配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
classpath:org/codehaus/xfire/spring/xfire.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>


8、建立一个com.easylife包,再新建一个webservice
[img]http://dl2.iteye.com/upload/attachment/0099/0391/2c55d156-7c6b-3e34-b947-84a4309ff395.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0099/0393/05c95e49-51a3-374a-948b-cacba1fd90d2.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0099/0395/400ecc88-0520-3050-bce1-4f6a89a62200.jpg[/img]

9、修改ILoginDAO
package com.easylife;
//Generated by MyEclipse

public interface ILoginDAO {

public boolean login(String username, String userpwd);

}

修改LoginDAOImpl

package com.easylife;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
//Generated by MyEclipse

public class LoginDAOImpl extends HibernateDaoSupport implements ILoginDAO {

@SuppressWarnings("unchecked")
public boolean login(String username, String userpwd) {
boolean flag = false;
String hql = "FROM User AS u WHERE u.username=? AND u.userpwd=?";
Query q = super.getSession().createQuery(hql);
q.setString(0, username);
q.setString(1, userpwd);
List all = q.list();
if (all.size() > 0) {
flag = true;
//SysUsers t = (SysUsers) all.get(0);
}
return flag;
}

}



10、打开MyEclipse Data Explorer视图找到user表使用Hibernate Reverse Engineering
[img]http://dl2.iteye.com/upload/attachment/0099/0397/4f8e60fb-350d-318f-b18c-11c82613edd0.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0099/0399/88c6f463-b341-33b3-8fe2-bd1b6921f694.jpg[/img]

11、在applicationContext.xml 末尾添加一个bean
<bean id="LoginDAOImpl" class="com.easylife.LoginDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>


12、在WEB-INF下建立xfire-servlet.xml
[img]<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/Login"><!--此为服务名-->
<ref bean="login" />
</entry>
</map>
</property>
</bean>
<bean id="login"
class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory" />
</property>

<property name="xfire">
<ref bean="xfire" />
</property>

<property name="serviceBean">
<ref bean="LoginDAOImpl" /><!--在 applicationContext.xml 中装配的类-->
</property>

<property name="serviceClass">
<value>com.easylife.ILoginDAO</value><!--接口-->
</property>
</bean>
</beans>[/img]

13、部署项目

14、测试服务
http://localhost:8080/XfireWebService/services/Login?wsdl

15、java调用WebService
package com.easylife;

import java.net.MalformedURLException;
import java.util.Iterator;
import java.util.List;

import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;

import com.easylife.User;
import com.easylife.ILoginDAO;

public class UserServiceWSTest {
public static void main(String[] args) throws Exception {
// 创建服务实例
Service srvcModel = new ObjectServiceFactory()
.create(ILoginDAO.class);
XFireProxyFactory factory = // 创建代理工厂实例
new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String helloWorldURL = "http://localhost:8080/XfireWebService/services/Login";
ILoginDAO srvc = null;
try { // 获取指定位置的web服务对象
srvc = (ILoginDAO) factory.create(srvcModel, helloWorldURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
// 调用只返回String的方法.在接口名.aegis.xml中不用设置方法名
boolean result = srvc.login("admin","123");
System.out.println(result);


Run As Java Application返回True

完毕
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值