首先搭建好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 getAll()
{
List ar = new ArrayList();
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 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 getAll(){
return this.dao.getAll();
}
}
在IUserService.java同级目录下添加IUserService.aegis.xml,命名规则为"接口名.aegis.xml"
web.xml的配置如下:
XFireServlet
org.codehaus.xfire.transport.http.XFireConfigurableServlet
0
XFireServlet
/services/*
XFireServlet
/services/XFireServlet/*
index.jsp
services.xml的配置如下:
UserService
com.xtgd.test.IUserService
com.xtgd.test.UserServiceImpl
application
成功发布到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 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();
}
}
}
最后,欢迎访问风格清新简洁的轻博客网站[指尖一刻]