forked from sermant-io/Sermant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hanbingleixue <[email protected]>
- Loading branch information
1 parent
ab176f1
commit b184d1e
Showing
9 changed files
with
519 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
...mer/src/main/java/com/huaweicloud/spring/feign/consumer/controller/MetricsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...n/feign-consumer/src/main/java/com/huaweicloud/spring/feign/consumer/entity/Students.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...consumer/src/main/java/com/huaweicloud/spring/feign/consumer/mapper/DatasourceMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.