Spring + Mybatis + WebService(CXF) 发布一个接口

Spring + Mybatis + WebService(CXF) 发布一个接口

声明:我是一只小菜鸟,下面如果有错误的地方请多多包含,烦请指出并更正

这个是结构:
这个是结构

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <!-- 字符编码过滤器,必须放在过滤器的最上面 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>characterEncoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-spring.xml</param-value>
    </context-param>
    <!-- 配置cxf拦截器 -->
    <servlet>
        <servlet-name>cxfServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxfServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

spring.xml (applicationContext-spring.xml)

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!-- 引入配置文件 -->
    <context:property-placeholder file-encoding="UTF-8" location="classpath:jdbc.properties"/>
    <!-- 开启注解的使用 -->
    <context:annotation-config/><!-- 开启注解 -->
    <!-- 自动扫描的包目录-->
    <context:component-scan base-package="cn.xinle.call.service"/>
    <!-- spring与mybaties的整合 -->
    <import resource="applicationContext-dao.xml"/>
    <!-- spring与cxf的整合 --> 
    <import resource="applicationContext-cxf-server.xml"/>
</beans>

spring整合Mybatis (applicationContext-dao.xml)

JDBC (偷点小懒就不放进来了哦)

<?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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <!-- 配置数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置别名包扫描 -->
        <property name="typeAliasesPackage" value="cn.xinle.call.domain"/>
        <!-- 配置mapping扫描 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    <!-- 配置sqlsessionfactory -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="username" value="${jdbc.jdbcUsername}"/>
        <property name="password" value="${jdbc.jdbcPassword}"/>
        <property name="url" value="${jdbc.jdbcUrl}"/>
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- setDataSource 方法里面传一个dataSource对象 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置 接口与 mapper的映射 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.xinle.call.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 开启事务注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

spring整合cxf (applicationContext-cxf-server.xml)

<?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.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd">

    <bean id="cityService" class="cn.xinle.call.service.serviceImpl.CityServiceImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <!--提供的webservice基于jaxws的服务端
            address :此服务接口的请求路径
            serviceClass :提供功能的实现类全类名
    -->
    <jaxws:server id="myService" address="city">
        <jaxws:serviceBean>
            <ref bean="cityService"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

我们结构现在已经完成了 现在就需要写我们的具体代码了

Service

@WebService
public interface CityService {
    /**
     * 测试
     * @param cid
     * @return
     */
    @WebMethod
    String selectByCid(@WebParam(name = "queryParam",targetNamespace = "http://service.call.xinle.cn/") String cid);

个人理解:
1、@WebService: 声明这是一个WebService接口
2、@WebParam: 跟Param 用法一样 name 就是接收的参数名

3、targetNamespace : 表示的是命名空间(可能很多人不理解命名空间是什么意思,小弟在这里献丑啦)

targetNamespace :
<xs:schema xmlns:xs="url/2001/XMLSchema"
  targetNamespace="http://a.name/space">
  <xs:element name="address" type="xs:string" />
  </xs:schema>

就比如上面这段targetNamespace 它表示的意思是address这个元素是属于"http://a.name/space"命名空间的。如果我们不指定targetNamespace,那么address是属于什么命名空间是不知道的,它肯定不是属于“url/2001/XMLSchema”命名空间。指定了这个以后,就能让我们定义的schema中的元素都有自己的命名空间。这个命名空间都是自己定义的。

ServiceImpl

@Component("cityService")
@WebService
public class CityServiceImpl implements CityService {
@Autowired
    private CityDao cityDao;
    /**
     * 测试
     * @param cid
     * @return
     */
    @Override
    public String selectByCid(String cid) {
        System.out.println("参数为:" + cid);
        try{
        City city = cityDao.findById(cid);
        System.out.println("返回结果:"+"无异常"+  city.toString());
                return city.toString();}
        catch (Exception e){
            City city = new City();
          return city.toString();}
    }
    }

解释一下 @Component:
就是当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类把他交给Spring容器管理。

Dao

public interface CityDao {
    City findById(String patientid);
    }

没啥解释的

mapper下面的CityDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
  dao映射文件
-->
<mapper namespace="cn.xinle.call.dao.CityDao">
    <select id="findById" resultType="cn.xinle.call.domain.City" parameterType="java.lang.String">
        select * from ss_user_info where patientid = #{patientid,jdbcType=VARCHAR}
    </select>
</mapper>

到此我们服务端就完事了 然后但是我们需要接口调用这里 我说一下我用过几种方式

项目跑起来 然后打开 http://localhost:8080/call_war_exploded/ws/city?wsdl
在这里插入图片描述
打完收工

结尾:路漫漫其修远兮,吾将上下而求索。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值