当前位置: 代码迷 >> Web前端 >> Webservice CXF+SPRING+HIBERNATE 范例
  详细解决方案

Webservice CXF+SPRING+HIBERNATE 范例

热度:594   发布时间:2012-10-11 10:16:10.0
Webservice CXF+SPRING+HIBERNATE 实例
  http://www.iteye.com/topic/563302
  http://www.iteye.com/topic/566664
  通过对xuzhfa123 这两篇贴子的阅读,自己也试着完成了一个例子
  服务器端开发:
  
  实体类开发UserInfo.java:
  public class UserInfo implements java.io.Serializable {   
    private static final long serialVersionUID = 2281022190032353574L;   
    private Long id;   
    private String name;   
    private Integer age;   
    //省略getter/setter……
  


  hibernate 映射文件开发UserInfo.hbm.xml(由myeclipse自动生成):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.UserInfo" table="M_USER" schema="TEST">
        <id name="id" type="java.lang.Long">
            <column name="ID" precision="22" scale="0" />
            <generator class="sequence"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="10" not-null="true" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="AGE" precision="22" scale="0" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


hibernate.cfg.xml文件:
 
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
<session-factory>
	<property name="connection.username">test</property>
	<property name="connection.url">
		jdbc:oracle:thin:@10.33.6.237:1521:orcl
	</property>
	<property name="dialect">
		org.hibernate.dialect.Oracle9Dialect
	</property>
	<property name="myeclipse.connection.profile">odbc</property>
	<property name="connection.password">test</property>
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="show_sql">true</property>
	<property name="connection.autocommit">true</property>
	<mapping resource="entity/UserInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>
  


  web.xml文件:
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

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

  


  数据持久层dao接口->HibernateDao.java:
  
public interface HibernateDao {
	public void save(UserInfo object);   
    public void delete(UserInfo object);   
    public void update(UserInfo object);   
    public UserInfo get(Long id); 
}
  


  dao实现类->HibernateImpl.java

public class HibernateDaoImpl extends HibernateDaoSupport implements HibernateDao{
	
	public void delete(UserInfo object) {
		
	}

	public UserInfo get(Long id) {
		return (UserInfo) getHibernateTemplate().get(UserInfo.class, id);
	}

	public void save(UserInfo object) {
	   this.getHibernateTemplate().save(object);
		
	}

	public void update(UserInfo object) {
	   getHibernateTemplate().update(object);
	}

}



业务层IService接口->IService.java

@WebService
public interface IService {
   public void save(@WebParam(name="user") UserInfo object);   
    public void update(@WebParam(name="user")UserInfo object);   
    public @WebResult(name="name") String get(@WebParam(name="id")Long id); 

}



IService实现类->ServiceImpl.java
@WebService
public class ServiceImpl implements IService{
	private HibernateDao dao;

	public HibernateDao getDao() {
		return dao;
	}

	public void setDao(HibernateDao dao) {
		this.dao = dao;
	}


	public String get(Long id) {
		return dao.get(id).getName();
	}

	public void save(UserInfo object) {
		dao.save(object);
	}

	public void update(UserInfo object) {
		dao.update(object);
	}

}

  


Spring 配置及WebService发布配置:
 
<?xml version="1.0" encoding="UTF-8"?>
<beans	
xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd">
	
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
 
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
		>
		<property name="configLocation"
			value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	
	<bean id="hdao" class="dao.impl.HibernateDaoImpl">
	<property name="sessionFactory" ref="sessionFactory"></property>
	
	</bean>
	<bean id="service" class="service.impl.ServiceImpl">
		<property name="dao" ref="hdao"></property>
	</bean>
	
	<jaxws:endpoint id="myService" address="/myService" implementorClass="service.impl.ServiceImpl">
       <!--这里很重要,一定不能漏掉-->
        <jaxws:implementor ref="service">
        </jaxws:implementor>
	</jaxws:endpoint>
	
	</beans>
  

到此服务器端就算开发完毕,可以通过访问http://localhost:8080/wssprhib/myService?wsdl来查看wsdl文件

客户端开发:
  新建java project 命名为test
打开cmd,运行 wsdl2java -p wssprhib.service -d d:\  htttp://localhost:8080/wssprhib/myService?wsdl
将wsdl文件转化成java文件,将产生的java文件复制到test工程src下,接下来就可以写测试方法了
client.java:
public class Client {
	public  Client(){
		
	}
	
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("/wssprhib/beans.xml");
		IService is=(IService) context.getBean("client");
		UserInfo u=new UserInfo();
		u.setName("1234");
		u.setAge(1);
		is.save(u);
		System.out.println(is.get(181l));
	}
}



client-beans.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
    <bean id="client" class="service.HelloWorld" 
      factory-bean="clientFactory" factory-method="create"/>
    
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	  <property name="serviceClass" value="service.HelloWorld"/>
	  <property name="address" value="http://localhost:8080/cxftest/HelloWorld"/>
	</bean>
</beans>
<!-- END SNIPPET: beans -->

运行client,查看sql语句,就可以看到成功访问数据库了
1 楼 xuesheng0147 2012-04-26  
lz好:
     我执行完:wsdl2java -p wssprhib.service -d d:\  htttp://localhost:8080/wssprhib/myService?wsdl
将wsdl文件转化成java文件,将产生的java文件复制到test工程src下,之后。
这个方法报错:public ServiceImplService(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
        super(wsdlLocation, serviceName, features);
    }  并报错提示:The constructor Service(URL, QName, WebServiceFeature[]) is undefined
  相关解决方案