编写接口
一个项目的结构大致如下
--.idea
--.mvn
-- src
|--main
|-- java
|--com.example.amanda
|-- action
|-- base
|-- action
|-- config
|-- controller
|-- handler
|-- mapper
|-- pojo
|-- service
|-- constant
|-- controller
|-- mapper
|-- pojo
|-- request
|-- service
|-- util
-- AmandaApplication
|-- resources
|--test
-- pom.xml
-- README.md
- New Project - New Spring Initializer - Dependencies - Spring Web | Spring Boot DevTools | JDBC API | MySQL Driver
- 数据库配置 - application.yml || application.properties
server:
port: 8080
spring:
datasource:
url: jdbc:oracle:thin:@47.99.59.172:1521:ORCL
username: ODSADM
password: HWods666
driver-class-name: oracle.jdbc.driver.OracleDriver
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 150
mybatis:
mapper-locations: classpath:mapper/*.xml
- 建立实体类 跟数据库字段保持一致
-
防止报错 创建类时添加无参构造方法
public ChartRule(){ } -
建立Mapper 接口 定义操作数据库的动作 --- 创建Java Interface 定义function 以及返回的数据类型
-
建立Mapper的XML 文件,写具体的SQL -- xml文件的基本配置
-
如果mybatis引入报错,则在pom.xml 文件中的 dependecies添加依赖
-
如果mybatis 报错URI is not registered (Settings | Lauguage & Framework | Schemas and DTDS):
File - Settings - Lauguage & Framework - Schemas and DTDs - Ignored schemas and DTDS 中添加报错的链接 - apply - ok -
select | insert | deletet | update 都可以 但注意 id 与 上一步Mapper中的function 进行匹配
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.amanda.mapper.ChartRuleMapper"> <select id="findA" resultType="com.example.amanda.entity.ChartRule"> SELECT CHARTID, CHARTNAME,RULENAME FROM ODSADM.ODS_CHARTRULE </select> </mapper>
-
建立Service 类,处理业务逻辑在Controller 类中展示处理结果
-
建立Controller 类中展示处理结果
package com.example.amanda.Controller; import java.util.List; import com.example.amanda.entity.ChartRule; import com.example.amanda.service.ChartService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @Api(tags = "测试接口") @RestController @RequestMapping("/test") public class TestController { @Autowired private ChartService ChartService; @ApiOperation(value = "获取信息") @PostMapping("/Amanda") public Animal getName(){ return new Animal("Bryan", 25); } @ApiOperation(value = "获取表规则") @PostMapping("/abc") public List<ChartRule> getChartRule(){ return ChartService.findA(); } }
在这里设置了 @Api(tags = "测试接口") 在API 调试页面会展示成以下效果
在最上方设置 @RequestMapping("/test"), 之后controlller类里面使用 PostMapping 或者是 GetMapping
常见问题 接口中写了requestpara data 要写为null
this.http.request('eac_wip_move_b/list_wip_move_by_oper', null, {
dateTime: time,
productionType: this.productionType,
productId: this.productionId}).subscribe(res => {
reduce 函数的使用
var arr = [3,9,4,3,6,0,9];
var sum = arr.reduce(function (prev, cur) {
return prev + cur;
},0);
non - arrow function are forbidden 报错function() 修改成 ()=>{}