最近基于需求用spring boot开发一个webservice服务,主要实现了对Oracle数据库的增删改查功能
------以下针对 查询 功能做以详细介绍------
- 项目通过接收解析xml文件请求传递参数实现通过'pid'(主键)查询单条数据或者通过'stime'(开始时间)----'etime'(结束时间)批量查询
- 创建spring boot项目在pom.xml文件中添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--webservice演示项目-->
<groupId>com.firesoon</groupId>
<artifactId>ws-demo</artifactId>
<version>0.0.1</version>
<name>ws-demo</name>
<description>Demo project for webservice</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--支持全栈式Web开发,包括Tomcat和spring-webmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- tag::springws[] -->
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
</dependency>
<!-- end::springws[] -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>orai18n</artifactId>
<version>19.3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>java11</id>
<activation>
<jdk>[11,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 创建entity层代码
- InpPatient.java
package com.firesoon.entity;
import lombok.Data;
@Data
public class InpPatient {
String hos_code;
String pid;
String modify_date;
String age;
String name;
String discharge_date;
}
- DwbResponse.java
package com.firesoon.entity;
import javax.xml.bind.annotation.*;
import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "getPatientResponse")
public class DwbResponse {
@XmlElement(name="patient")
protected List<InpPatient> ps;
public List<InpPatient> getPatients() {
return ps;
}
public void setPatients(List<InpPatient> ps1) {
this.ps = ps1;
}
}
- GetPatientRequest
package com.firesoon.entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import lombok.Data;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"pid", "stime", "etime"
})
@XmlRootElement(name = "getPatientRequest")
@Data
public class GetPatientRequest {
@XmlElement
protected String pid;
@XmlElement
protected String stime;
@XmlElement
protected String etime;
}
- controller层代码
DwbEndpoint.java
package com.firesoon.controller.ws;
import com.firesoon.entity.DwbResponse;
import com.firesoon.entity.GetPatientRequest;
import com.firesoon.services.DwbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
@Endpoint
public class DwbEndpoint {
private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";
@Autowired
private DwbService ds;
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getPatientRequest")
@ResponsePayload
public DwbResponse getPatients(@RequestPayload GetPatientRequest request) {
DwbResponse res = new DwbResponse();
if (request.getStime() != null && request.getEtime() != null) {
res.setPatients(ds.selectInpPatientWithTime(request.getStime(), request.getEtime()));
} else if (request.getPid() != null) {
res.setPatients(ds.selectInpPatient(request.getPid()));
}
return res;
}
}
- 在mapper文件下创建接口
package com.firesoon.mapper;
import com.firesoon.entity.InpPatient;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface InpPatientMapper {
List<InpPatient> queryPatient(@Param("pid") String pid);
List<InpPatient> queryPatientWithTime(@Param("stime") String stime, @Param("etime") String etime);
}
- 在service层下面创建pid与stime,etime接口并写接口实现类,下面没有列出来
package com.firesoon.services;
import com.firesoon.entity.InpPatient;
import java.util.List;
import java.util.Map;
public interface DwbService {
List<InpPatient> selectInpPatient(String pid);
List<InpPatient> selectInpPatientWithTime(String stime, String etime);
}
- 在resources下的mapper.xml文件中实现sql查询语句
-
<?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.firesoon.mapper.InpPatientMapper"> <select id="queryPatient" resultType="InpPatient"> select hos_code, pid, modify_date, age, name, discharge_date from INP_PATIENT_262 where pid = '${pid}' </select> <select id="queryPatientWithTime" resultType="InpPatient"> select hos_code, pid, modify_date, age, name, discharge_date from INP_PATIENT_262 where discharge_date between to_date('${stime}', 'YYYY-MM-DD') and to_date('${etime}', 'YYYY-MM-DD') </select> </mapper>
application.properties文件根据自己的数据库和端口书写
-
请求的xml文件根据请求类容修改
新建一个request.xml ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gs="http://spring.io/guides/gs-producing-web-service"> <soapenv:Header/> <soapenv:Body> <gs:getCountryRequest> <gs:name>Spain</gs:name> </gs:getCountryRequest> </soapenv:Body> </soapenv:Envelope> ``` ```shell script curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws ```
以上为一个webservice的select功能代码的大概实现步骤其余的insert,delete,update与以上代码大致相同未罗列
-