• 使用CXF开发WebService程序的总结(七):Spring+CXF+Mybatis+Mysql共同打造的服务端示例


    通过该demo,可以 熟悉下 spring+cxf+maven+mybatis+mysql等常见后端技术整合

    1. 在前面的 父工程 ws_parent 中 添加依赖

    由于原来的项目是使用的cxf依赖,现在需要集成spring,mybatis,所以需要在父工程添加依赖,最新的父工程的pom文件如下:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<groupId>com.lonely.webservice</groupId>
    	<artifactId>ws_parent</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>pom</packaging>
    
    	<properties>
    		<spring-version>4.1.7.RELEASE</spring-version>
    		<mybatis.version>3.4.1</mybatis.version>
    		<mybatis.spring.version>1.3.0</mybatis.spring.version>
    		<pagehelper.version>4.0.0</pagehelper.version>
    		<mysql.version>5.1.32</mysql.version>
    		<druid.version>1.0.9</druid.version>
    		<jstl.version>1.2</jstl.version>
    		<servlet-api.version>2.5</servlet-api.version>
    		<jsp-api.version>2.0</jsp-api.version>
    		<slf4j.api.version>1.7.10</slf4j.api.version>
    		<logback.version>1.1.7</logback.version>
    		<dbcp.version>1.4</dbcp.version>
    	</properties>
    
    	<!-- 配置依赖 -->
    	<dependencyManagement>
    		<dependencies>
    
    			<!-- spring依赖 -->
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-core</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-beans</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-tx</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-context</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-context-support</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-web</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-webmvc</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-aop</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-aspects</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-jdbc</artifactId>
    				<version>${spring-version}</version>
    			</dependency>
    
    			<!-- cxf依赖 -->
    			<dependency>
    				<groupId>org.apache.cxf</groupId>
    				<artifactId>cxf-core</artifactId>
    				<version>3.1.6</version>
    			</dependency>
    			<dependency>
    				<groupId>org.apache.cxf</groupId>
    				<artifactId>cxf-rt-frontend-jaxws</artifactId>
    				<version>3.1.6</version>
    			</dependency>
    			<dependency>
    				<groupId>org.apache.cxf</groupId>
    				<artifactId>cxf-rt-transports-http-jetty</artifactId>
    				<version>3.1.6</version>
    			</dependency>
    
    
    			<!-- mybatis依赖 -->
    			<dependency>
    				<groupId>org.mybatis</groupId>
    				<artifactId>mybatis</artifactId>
    				<version>${mybatis.version}</version>
    			</dependency>
    			<dependency>
    				<groupId>org.mybatis</groupId>
    				<artifactId>mybatis-spring</artifactId>
    				<version>${mybatis.spring.version}</version>
    			</dependency>
    			<dependency>
    				<groupId>com.github.pagehelper</groupId>
    				<artifactId>pagehelper</artifactId>
    				<version>${pagehelper.version}</version>
    			</dependency>
    
    			<!-- mysql -->
    			<dependency>
    				<groupId>mysql</groupId>
    				<artifactId>mysql-connector-java</artifactId>
    				<version>${mysql.version}</version>
    			</dependency>
    
    			<!-- 连接池 -->
    			<dependency>
    				<groupId>com.alibaba</groupId>
    				<artifactId>druid</artifactId>
    				<version>${druid.version}</version>
    			</dependency>
    			<dependency>
    				<groupId>commons-dbcp</groupId>
    				<artifactId>commons-dbcp</artifactId>
    				<version>${dbcp.version}</version>
    			</dependency>
    
    			<!-- JSP相关 -->
    			<dependency>
    				<groupId>jstl</groupId>
    				<artifactId>jstl</artifactId>
    				<version>${jstl.version}</version>
    			</dependency>
    			<dependency>
    				<groupId>javax.servlet</groupId>
    				<artifactId>servlet-api</artifactId>
    				<version>${servlet-api.version}</version>
    				<scope>provided</scope>
    			</dependency>
    			<dependency>
    				<groupId>javax.servlet</groupId>
    				<artifactId>jsp-api</artifactId>
    				<version>${jsp-api.version}</version>
    				<scope>provided</scope>
    			</dependency>
    
    			<!-- 日志处理 -->
    			<dependency>
    				<groupId>org.slf4j</groupId>
    				<artifactId>slf4j-api</artifactId>
    				<version>${slf4j.api.version}</version>
    			</dependency>
    			<dependency>
    				<groupId>ch.qos.logback</groupId>
    				<artifactId>logback-classic</artifactId>
    				<version>${logback.version}</version>
    			</dependency>
    			<dependency>
    				<groupId>ch.qos.logback</groupId>
    				<artifactId>logback-core</artifactId>
    				<version>${logback.version}</version>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    
    	<!-- 配置插件信息 -->
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<configuration>
    					<source>1.8</source>
    					<target>1.8</target>
    					<encoding>UTF-8</encoding>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    
    	<modules>
    		<module>ws_server</module>
    		<module>ws_client</module>
    		<module>ws_ssm_server</module>
    	</modules>
    </project>

    2. 新建一个maven子工程  ws_ssm_server

    2.1 新建一个子工程,打包方式为 war格式,因为spring的很多依赖有重复,经整理后 其pom文件如下:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <parent>
        <groupId>com.lonely.webservice</groupId>
        <artifactId>ws_parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </parent>
      <artifactId>ws_ssm_server</artifactId>
      <packaging>war</packaging>
      
      
      <dependencies>
    		<!-- spring依赖 -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aspects</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    		</dependency>
    
    
    		<!-- 添加 cxf 依赖 -->
    		<dependency>
    			<groupId>org.apache.cxf</groupId>
    			<artifactId>cxf-core</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.cxf</groupId>
    			<artifactId>cxf-rt-frontend-jaxws</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.cxf</groupId>
    			<artifactId>cxf-rt-transports-http-jetty</artifactId>
    		</dependency>
    
    		<!-- mybatis依赖 -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis-spring</artifactId>
    		</dependency>
    
    		<!-- mysql -->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    		</dependency>
    
    		<!-- 连接池 -->
    		<dependency>
    			<groupId>commons-dbcp</groupId>
    			<artifactId>commons-dbcp</artifactId>
    		</dependency>
    
    		<!-- 日志处理 -->
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-api</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>ch.qos.logback</groupId>
    			<artifactId>logback-classic</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>ch.qos.logback</groupId>
    			<artifactId>logback-core</artifactId>
    		</dependency>
    	</dependencies>
      
    </project>
    2.2 spring整合cxf
    2.2.1 在web.xml中添加 cxf 拦截器处理器配置,以及 spring容器启动创建配置
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    
        <!-- 字符编码过滤器,必须放在过滤器的最上面 -->
        <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>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/application.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
    	<!-- 配置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>/cxf/*</url-pattern>
    	</servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>/index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    
    2.2.2 在resource目录下 创建文件夹 spring,在里面创建spring与cxf集成的配置文件 :  spring-cxf.xml
    这里注意要添加一个 jaxws的命名空间,以及引入两个 cxf中的 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"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<import resource="classpath:META-INF/cxf/cxf.xml" />
    	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    
    	<!-- 定义服务提供者 -->
    	<jaxws:endpoint implementor="#userService" address="/UserService" >
    		<!-- 添加入 in 拦截器 -->
    		<jaxws:inInterceptors>
    			<bean class="com.lonely.interceptor.AuthInterceptor"></bean>
    			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
    		</jaxws:inInterceptors>
    		<!-- 添加 出 out 拦截器 -->
    		<jaxws:outInterceptors>
    			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
    		</jaxws:outInterceptors>
    	</jaxws:endpoint>
    
    </beans>
    
    2.2.3  jaxws:endpoint 中的 implementor:对应的服务接口的实现类,而 address :对应发布的接口路径,注意在实际的接口实现类上 使用注解 @Component让spring 扫描发现管理,给个别名用于在了  implementor属性上

    UserService
    package com.lonely.service;
    
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebService;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    import com.lonely.adapter.MapConvertAdapter;
    import com.lonely.model.Clazz;
    import com.lonely.model.User;
    
    @WebService
    public interface UserService {
    
    	/**
    	 * 根据班级信息返回该班级的学生列表
    	 * 
    	 * @param clazz
    	 * @return
    	 */
    	List<User> findUsersByClass(Clazz clazz);
    
    	/**
    	 * 查看所有班级信息以及班级下的所有学生
    	 * 
    	 * @return
    	 */
    	@XmlJavaTypeAdapter(value = MapConvertAdapter.class)
    	Map<String, List<User>> findAll();
    }
    
    UserServiceImpl
    package com.lonely.service.impl;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.jws.WebService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import com.lonely.dao.UserDao;
    import com.lonely.model.Clazz;
    import com.lonely.model.User;
    import com.lonely.service.UserService;
    
    @Component("userService")
    @WebService
    public class UserServiceImpl implements UserService {
    
    	@Autowired
    	private UserDao userDao;
    
    	@Override
    	public List<User> findUsersByClass(Clazz clazz) {
    		return this.userDao.findUsersByClass(clazz);
    	}
    
    	@Override
    	public Map<String, List<User>> findAll() {
    		List<Clazz> clazzList = this.userDao.findAll();
    		if (clazzList != null && clazzList.size() != 0) {
    			Map<String, List<User>> map = new HashMap<>();
    			for (Clazz clazz : clazzList) {
    				map.put(clazz.getClassName(), clazz.getUsers());
    			}
    			return map;
    		}
    		return null;
    	}
    
    }
    


    2.3 spring整合mybatis
    2.3.1 在spring目录下,添加 spring与mybatis集成的配置 : spring-datasource.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: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="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="username" value="${jdbc.jdbcUsername}"></property>
    		<property name="password" value="${jdbc.jdbcPassword}"></property>
    		<property name="url" value="${jdbc.jdbcUrl}"></property>
    		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
    	</bean>
    
    	<!-- 配置sqlsessionfactory -->
    	<bean  id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<!-- 配置数据源 -->
    		<property name="dataSource" ref="dataSource"></property>
    		<!-- 配置mybatis配置 -->
    		<property name="configuration">
    			<bean class="org.apache.ibatis.session.Configuration">
    				<!-- 开启驼峰命名法 -->
    				<property name="mapUnderscoreToCamelCase" value="true"></property>
    				<!-- 当检测出未知列(或未知属性)时,如何处理,默认情况下没有任何提示,这在测试的时候很不方便,不容易找到错误。
    					可选值:
    						NONE : 不做任何处理 (默认值)
    						WARNING : 警告日志形式的详细信息
    						FAILING : 映射失败,抛出异常和详细信息 
    				-->
    				<property name="autoMappingUnknownColumnBehavior" value="WARNING"></property>
    			</bean>
    		</property>
    		<!-- 配置mapping扫描 -->
    		<property name="mapperLocations" value="classpath:mapper/**/*.xml"></property>
    		<!-- 配置别名包扫描 -->
    		<property name="typeAliasesPackage" value="com.lonely.model"></property>
    		<!-- 配置pagehelper 数据库方言 -->
    		<property name="plugins">
    			<list>
    				<bean class="com.github.pagehelper.PageHelper">
    					<property name="properties">
    						<value>dialect=mysql</value>
    					</property>
    				</bean>
    			</list>
    		</property>
    	</bean>
    	
    	
    	<!-- 配置 接口与 mapper的映射 -->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.lonely.dao"></property>
    		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
    	</bean>
    	
    	<!-- 配置事务 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    	
    	<!-- 开启事务注解 -->
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>
    
    2.3.2 在  basePackage 对应的路径下 编写 dao层的接口
    package com.lonely.dao;
    
    import java.util.List;
    
    import com.lonely.model.Clazz;
    import com.lonely.model.User;
    
    public interface UserDao {
    
    	List<User> findUsersByClass(Clazz clazz);
    
    	List<Clazz> findAll();
    }
    
    2.3.3 在 mapperLocations 对应的路径下 编写  mybatis的sql操作的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">
    
    <mapper namespace="com.lonely.dao.UserDao">
    
    	<resultMap type="clazz" id="clazzMap">
    		<id property="classId" column="classId"/>
    		<result property="className" column="className"/>
    		<collection property="users" ofType="user">
    			<id column="id" property="id"/>
    			<result property="userName" column="userName"/>
    			<result property="classId" column="classId"/>
    		</collection>
    	</resultMap>
    
        <!-- 根据班级信息查询所有学生 -->
        <select id="findUsersByClass" parameterType="clazz" resultType="user">
        	SELECT * from user u where u.classId = #{classId}
        </select>
        
        <!-- 查询所有班级信息以及班级下的所有学生列表 -->
        <select id="findAll" resultMap="clazzMap">
        	SELECT
    			*
    		FROM
    			clazz c
    		JOIN USER u ON c.classId = u.classId
        </select>
    </mapper>
    2.3.4 在这里 使用了 mybatis的一对多,所以在  Clazz实体类上 多加了一个属性  List<User> users;
    2.3.5 忘了 前面还需要一个 jdbc.properties
    ## jdbc环境配置
    jdbc.driverClassName                = com.mysql.jdbc.Driver
    jdbc.jdbcUrl                        = jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&allowMultiQueries=true
    jdbc.jdbcUsername                   = root
    jdbc.jdbcPassword                   = xxx
    #jdbc.logAbandoned                   = false
    #jdbc.initialSize                    = 20
    #jdbc.maxActive                      =1000
    #jdbc.maxIdle=100
    #jdbc.maxWait                        = 1000
    #jdbc.minEvictableIdleTimeMillis     = 300000
    #jdbc.minIdle                        = 10
    #jdbc.removeAbandoned                = true
    #jdbc.removeAbandonedTimeout         = 3000
    #jdbc.timeBetweenEvictionRunsMillis  = 10000
    #jdbc.testOnBorrow                   = false
    #jdbc.testWhileIdle                  = true
    #jdbc.validSql                       = select now()

    2.4 spring+cxf+mybatis
    2.4.1 在spring目录下 创建一个spring的核心文件  application.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.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    
    	<!-- 引入配置文件 -->
    	<context:property-placeholder file-encoding="UTF-8" location="classpath:properties/jdbc.properties"/>
    
    	<!-- 包扫描 -->
    	<context:component-scan base-package="com.lonely"></context:component-scan>
    
    	<!-- spring与mybaties的整合 -->
    	<import resource="spring-datasource.xml"/>
    
    	<!-- spring与cxf的整合 -->
    	<import resource="spring-cxf.xml"/>
    </beans>
    
    2.4.2  至此,spring+cxf+mybatis整合完毕,整个项目结构如下



    3.  使用wsdl2java工具生成客户端代码,跟原来的ws_client类似,直接测试即可












  • 相关阅读:
    WM_PAINT消息详解,使用InvalidateRect或InvalidateRgn函数刻意产生WM_PAINT消息(WIN7里有变化,“调整视觉效果”,将“启用桌面组合”去掉)
    delphi 大文件的读写 使用 MapviewOffile
    Delphi的子类化控件消息, 消息子类化
    wParam与lParam的区别
    为什么使用DLL
    大数据分包算法
    JSON如何序列图片
    AngularJs学习
    设计模式解读
    js模块化编程总结
  • 原文地址:https://www.cnblogs.com/duguxiaobiao/p/9128821.html
Copyright © 2020-2023  润新知