[]转]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。
表结构如下:
id integer
username Varchar(20)
userpwd Varchar(20)

插入一条数据待测试用。

二、实例代码

连接数据库

打开MyEclipse Database Explorer 新建一个数据连接:
[]转]Xfire Spring Hibernate 发布WebService

创建项目
1、打开MyEclipse 新建一个Web Service Project项目输入项目名称XfireWebService。
[]转]Xfire Spring Hibernate 发布WebService

2、下一步默认
[]转]Xfire Spring Hibernate 发布WebService

3、下一步,勾选XFire 1.2 HTTP Client Libraries。
[]转]Xfire Spring Hibernate 发布WebService

4、点击View and edit libraries 去掉spring-1.2.6.jar 这个包(不去掉会和后面添加的Spring冲突)
5、右键添加MyEclipse的Spring Capabilities,方便起见选上所有的包,选择Copy checked…,下一步默认完成。
[]转]Xfire Spring Hibernate 发布WebService

6、再添加Hibernate Capabilities,同样选上所有包。
[]转]Xfire Spring Hibernate 发布WebService

[]转]Xfire Spring Hibernate 发布WebService

[]转]Xfire Spring Hibernate 发布WebService

[]转]Xfire Spring Hibernate 发布WebService

下一步去掉复选框完成。
[]转]Xfire Spring Hibernate 发布WebService

点击Replace替换所有包
[]转]Xfire Spring Hibernate 发布WebService

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
[]转]Xfire Spring Hibernate 发布WebService
[]转]Xfire Spring Hibernate 发布WebService
[]转]Xfire Spring Hibernate 发布WebService

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
[]转]Xfire Spring Hibernate 发布WebService
[]转]Xfire Spring Hibernate 发布WebService

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

完毕

你可能感兴趣的:(spring,Hibernate)