Skip to content

Commit

Permalink
提交测试DEMO
Browse files Browse the repository at this point in the history
Signed-off-by: hanbingleixue <[email protected]>
  • Loading branch information
hanbingleixue committed Jan 25, 2024
1 parent ab176f1 commit b184d1e
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>mysql</groupId>-->
<!-- <artifactId>mysql-connector-java</artifactId>-->
<!-- <version>5.0.2</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies>
<build>
<finalName>feign-consumer</finalName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import feign.codec.ErrorDecoder;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
Expand All @@ -31,11 +32,12 @@
* @since 2022-07-29
*/
@SpringBootApplication(scanBasePackages = {
"com.huaweicloud.spring.feign.consumer.controller",
"com.huaweicloud.spring.common.loadbalancer.feign",
"com.huaweicloud.spring.feign.api.configuration"
"com.huaweicloud.spring.feign.consumer.controller",
"com.huaweicloud.spring.common.loadbalancer.feign",
"com.huaweicloud.spring.feign.api.configuration"
})
@EnableFeignClients(basePackages = "com.huaweicloud.spring.feign.api")
@MapperScan({"com.huaweicloud.spring.feign.consumer.*"})
public class FeignConsumerApplication {
/**
* 启动类
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.huaweicloud.spring.feign.consumer.controller;

import com.huaweicloud.spring.feign.consumer.entity.Students;
import com.huaweicloud.spring.feign.consumer.mapper.DatasourceMapper;
import com.huaweicloud.spring.feign.consumer.mapper.StudentsMapper;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;
import javax.websocket.server.PathParam;

/**
* 指标采集测试接口
*
* @author zhp
* @since 2024-01-13
*/
@RestController
@RequestMapping("students")
public class MetricsController {
private static final int BATCH_SIZE = 10;

private static final int PARAM_INDEX = 2;

@Resource
private StudentsMapper studentsMapper;

@Resource
private DatasourceMapper datasourceMapper;

@Resource
private DataSource dataSource;

/**
* 根据ID查询数据
*
* @param id 表主键
* @return 学生信息
*/
@GetMapping("/getById")
public Students getById(@PathParam("id") int id) {
return studentsMapper.selectById(id);
}

/**
* 保存数据
*
* @param name 学生名称
* @param age 学生年龄
* @return 表主键
*/
@GetMapping("/saveStudents")
public int saveStudents(@PathParam("name") String name, @PathParam("age") int age) {
Students students = new Students();
students.setAge(age);
students.setName(name);
return studentsMapper.insert(students);
}

/**
* 根据ID更新数据
*
* @param name 学生名称
* @param age 学生年龄
* @param id 表主键
* @return 表主键
*/
@GetMapping("/updateStudents")
public int updateStudents(@PathParam("name") String name, @PathParam("age") int age, @PathParam("id") int id) {
Students students = new Students();
students.setAge(age);
students.setName(name);
students.setId(id);
return studentsMapper.updateById(students);
}

/**
* 根据ID更新数据
*
* @param id 表主键
* @return 表主键
*/
@GetMapping("/deleteById")
public int deleteById(@PathParam("id") int id) {
return studentsMapper.deleteById(id);
}

/**
* 根据ID批量查询数据
*
* @param ids 主键ID
* @return 表主键
*/
@GetMapping("/selectBatchIds")
public List<Students> selectBatchIds(@PathParam("ids") String ids) {
String[] idArray = ids.split(",");
List<String> idList = Arrays.asList(idArray);
return studentsMapper.selectBatchIds(idList);
}

/**
* 创建索引
*
* @param indexName 索引名称
*/
@GetMapping("/createIndex")
public void createIndex(@PathParam("indexName") String indexName) {
datasourceMapper.createIndex(indexName);
}

/**
* 删除索引
*
* @param indexName 索引名称
*/
@GetMapping("/deleteIndex")
public void deleteIndex(@PathParam("indexName") String indexName) {
datasourceMapper.deleteIndex(indexName);
}

/**
* 创建表
*
* @param tableName 表名
*/
@GetMapping("/createTable")
public void createTable(@PathParam("tableName") String tableName) {
datasourceMapper.createTable(tableName);
}

/**
* 删除表
*
* @param tableName 表名
*/
@GetMapping("/deleteTable")
public void deleteTable(@PathParam("tableName") String tableName) {
datasourceMapper.deleteTable(tableName);
}

/**
* 增加列
*
* @param tableName 表名
* @param columnName 列名
*/
@GetMapping("/addColumn")
public void addColumn(@PathParam("tableName") String tableName, @PathParam("columnName") String columnName) {
datasourceMapper.addColumn(tableName, columnName);
}

/**
* 批量插入数据
*
* @throws SQLException 链接异常
*/
@GetMapping("/insertBatch")
public void insertBatch() throws SQLException {
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
String sql = "INSERT INTO STUDENTS (NAME ,AGE) VALUES (?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
for (int i = 0; i < BATCH_SIZE; i++) {
statement.setString(1, "张三" + i);
statement.setInt(PARAM_INDEX, i);
statement.addBatch();
}
statement.executeBatch();
connection.commit();
statement.close();
connection.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.huaweicloud.spring.feign.consumer.entity;

import com.baomidou.mybatisplus.annotation.TableName;

/**
* 学生表对应的实体类
*
* @author zhp
* @since 2024-01-13
*/
@TableName("students")
public class Students {
private int id;

private String name;

private int age;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.huaweicloud.spring.feign.consumer.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
* 数据库Mapper
*
* @author zhp
* @since 2024-01-13
*/
@Mapper
public interface DatasourceMapper {
/**
* 创建索引
*
* @param indexName 索引名称
*/
@Update("CREATE INDEX ${indexName} ON students (`age`)")
void createIndex(@Param("indexName") String indexName);

/**
* 删除索引
*
* @param indexName 索引名称
*/
@Update("DROP INDEX ${indexName} ON students")
void deleteIndex(@Param("indexName") String indexName);

/**
* 创建表
*
* @param tableName 表名称
*/
@Update("CREATE TABLE ${tableName} (id INT, name VARCHAR(255))")
void createTable(@Param("tableName") String tableName);

/**
* 删除表
*
* @param tableName 表名称
*/
@Update("DROP TABLE ${tableName}")
void deleteTable(@Param("tableName") String tableName);

/**
* 修改表结构
*
* @param tableName 表名称
* @param columnName 字段名称
*/
@Update("ALTER TABLE ${tableName} ADD COLUMN ${columnName} INT")
void addColumn(@Param("tableName") String tableName, @Param("columnName") String columnName);
}
Loading

0 comments on commit b184d1e

Please sign in to comment.