以webService为客户端获取List泛型结果集

首先搭建好webService,添加XFire1.2Core Libraries 和XFire1.2HTTP Client Libraries,连接上数据库,下例以oracle为数据库。

连接Oralce,连接类为DBConnection.java

package com.xtgd.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class DBConnection {
	
	private String driver="oracle.jdbc.driver.OracleDriver";
	
	private String url="jdbc:oracle:thin:@localhost:1521:orcl";
	
	private String username="scott";
	
	private String pwd="tiger";
	
	private Connection conn=null;
	
	
	public Connection getConn(){
		try {
			Class.forName(driver);
			conn=DriverManager.getConnection(url,username,pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	
	public void closeConn(ResultSet rs,PreparedStatement ps,Connection conn){
		try {
			if(rs!=null){
				rs.close();
			}
			if(rs!=null){
				rs.close();
			}
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
		
		
	

}

 

写好UserDao方法 UserDao.java

 

package com.xtgd.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDao
{
	DBConnection b = new DBConnection();

	private PreparedStatement pre = null;

	private ResultSet rs = null;

	private Connection conn = null;

	/*
	 * 全查询
	 */

	public List<TUser> getAll()
	{
		List<TUser> ar = new ArrayList<TUser>();
		try
		{
			String sql = "select * from t_user";
			conn = b.getConn();
			pre = conn.prepareStatement(sql);
			rs = pre.executeQuery();
			while (rs.next())
			{
				TUser user = new TUser();
				user.setUId(rs.getInt("u_id"));
				user.setUPersonid(rs.getString("u_personid"));
				user.setUPersonname(rs.getString("u_personname"));
				user.setUPassword(rs.getString("u_password"));
				user.setUTel(rs.getString("u_tel"));
				user.setUEmail(rs.getString("u_email"));
				user.setUState(rs.getInt("u_state"));
				user.setURemark(rs.getString("u_remark"));
				ar.add(user);
			}

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			b.closeConn(rs, pre, conn);
		}
		return ar;
	}

	

}

 

实体类为TUser.entity

package com.xtgd.test;

import java.io.Serializable;

/**
 * TUser entity.
 * 
 * @author MyEclipse Persistence Tools
 */

public class TUser implements Serializable {

	// Fields

	private Integer UId;
	private String UPersonid;
	private String UPersonname;
	private String UPassword;
	private String UTel;
	private String UEmail;
	private int UState = 1;
	private String URemark;

	// Constructors

	/** default constructor */
	public TUser() {
	}

	/** full constructor */
	public TUser(String UPersonid, String UPersonname, String UPassword,
			String UTel, String UEmail, int UState, String URemark) {
		this.UPersonid = UPersonid;
		this.UPersonname = UPersonname;
		this.UPassword = UPassword;
		this.UTel = UTel;
		this.UEmail = UEmail;
		this.UState = UState;
		this.URemark = URemark;
	}

	// Property accessors

	public Integer getUId() {
		return this.UId;
	}

	public void setUId(Integer UId) {
		this.UId = UId;
	}

	public String getUPersonid() {
		return this.UPersonid;
	}

	public void setUPersonid(String UPersonid) {
		this.UPersonid = UPersonid;
	}

	public String getUPersonname() {
		return this.UPersonname;
	}

	public void setUPersonname(String UPersonname) {
		this.UPersonname = UPersonname;
	}

	public String getUPassword() {
		return this.UPassword;
	}

	public void setUPassword(String UPassword) {
		this.UPassword = UPassword;
	}

	public String getUTel() {
		return this.UTel;
	}

	public void setUTel(String UTel) {
		this.UTel = UTel;
	}

	public String getUEmail() {
		return this.UEmail;
	}

	public void setUEmail(String UEmail) {
		this.UEmail = UEmail;
	}

	public int getUState() {
		return UState;
	}

	public void setUState(int state) {
		UState = state;
	}

	public String getURemark() {
		return this.URemark;
	}

	public void setURemark(String URemark) {
		this.URemark = URemark;
	}

}


写好接口IUserService.java

package com.xtgd.test;

import java.util.List;
//Generated by MyEclipse

public interface IUserService {
		
	public List<TUser> getAll();
}


 

 实现类UserServiceImpl.java

package com.xtgd.test;

import java.util.List;

//Generated by MyEclipse

public class UserServiceImpl implements IUserService {

	private UserDao dao = new UserDao();

	/***************************************************************************
	 * 查询所有
	 */
	public List<TUser> getAll(){
		return this.dao.getAll();
	}
}


在IUserService.java同级目录下添加IUserService.aegis.xml,命名规则为"接口名.aegis.xml"

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
        该文件用来描述IUsersService接口中getAll()方法返回值的类型
        该文件必须与IUsersService位于同一目录中,且该文件遵循如下命名规则
        webservice接口名.aegis.xml 如本文件IUsersService.aegis.xml
    -->

<mappings>
	<method name="getAll">
            <return-type componentType="#users"/>
    </method>
	 <!-- 映射getAll()方法返回值类型 -->
    <mapping>
        <component name="users" class="com.xtgd.test.TUser" componentType="com.xtgd.test.TUser"/>
    </mapping>
</mappings>

 

web.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
  
  <!-- 添加mapping -->
  <servlet-mapping>
  	<servlet-name>XFireServlet</servlet-name>
  	<url-pattern>/services/XFireServlet/*</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

services.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
	<service>
		<name>UserService</name>
		<serviceClass>com.xtgd.test.IUserService</serviceClass>
		<implementationClass>
			com.xtgd.test.UserServiceImpl
		</implementationClass>
		<style>wrapped</style>
		<use>literal</use>
		<scope>application</scope>
	</service>
</beans>

 

成功发布到tomcat上之后,下面开始测试,测试类为Test.java,注意导入正确的包!

package com.xtgd.test;

import java.util.List;
import com.xtgd.test.*;

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

public class Test {

	public static void main(String[] args) {
		// 构建service
		Service service = (Service) new ObjectServiceFactory()
				.create(IUserService.class);
		try {
			// 获得自定义webservice接口
			IUserService usersService = (IUserService) new XFireProxyFactory()
					.create(service,
							"http://localhost:8080/08_01_webservice/services/UserService");
			// 调用该webservice中定义的getUsers()方法
			List<TUser> list = usersService.getAll();
			// 输出list长度
			System.out.println(list.size());
			// 遍历list
			for (TUser users : list) {
				System.out.println("personid:" + users.getUPersonid()
						+ "\tpersonname:" + users.getUPersonname());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
posted @ 2013-10-29 22:26  问笑  阅读(606)  评论(0编辑  收藏  举报