-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
v1ll4n
committed
Jul 18, 2024
1 parent
8bb2d56
commit 28d8bfa
Showing
5 changed files
with
100 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
desc( | ||
title: "Fetch '${' from ID and check the Source from server/controller", | ||
type: audit | ||
) | ||
|
||
${*.xml}.xpath("//mapper/*[contains(., '${') and @id]/@id") as $id; | ||
$id | ||
check $id; |
37 changes: 37 additions & 0 deletions
37
java-database-mapper/sample/com/example/controller/UserController.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,37 @@ | ||
package com.example.controller; | ||
|
||
import com.example.User; | ||
import com.example.mapper.UserMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/users") | ||
public class UserController { | ||
|
||
@Autowired | ||
private UserMapper userMapper; | ||
|
||
@PostMapping("/create") | ||
public String createUser(@RequestParam String tableName, @RequestBody User user) { | ||
userMapper.insertUser(tableName, user); | ||
return "User created successfully!"; | ||
} | ||
|
||
@GetMapping("/get") | ||
public User getUser(@RequestParam String tableName, @RequestParam String columnName, @RequestParam String username) { | ||
return userMapper.selectUserByUsername(tableName, columnName, username); | ||
} | ||
|
||
@PutMapping("/update") | ||
public String updateUserEmail(@RequestParam String tableName, @RequestParam String username, @RequestParam String email) { | ||
userMapper.updateUserEmail(tableName, email, username); | ||
return "User email updated successfully!"; | ||
} | ||
|
||
@DeleteMapping("/delete") | ||
public String deleteUser(@RequestParam String tableName, @RequestParam String username) { | ||
userMapper.deleteUser(tableName, username); | ||
return "User deleted successfully!"; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
java-database-mapper/sample/com/example/mapper/UserMapper.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,20 @@ | ||
package com.example.mapper; | ||
|
||
import com.example.User; | ||
import org.apache.ibatis.annotations.Param; | ||
import java.util.Map; | ||
|
||
public interface UserMapper { | ||
void insertUser(@Param("tableName") String tableName, @Param("user") User user); | ||
|
||
User selectUserByUsername(@Param("tableName") String tableName, | ||
@Param("columnName") String columnName, | ||
@Param("value") String value); | ||
|
||
void updateUserEmail(@Param("tableName") String tableName, | ||
@Param("email") String email, | ||
@Param("username") String username); | ||
|
||
void deleteUser(@Param("tableName") String tableName, | ||
@Param("username") String username); | ||
} |
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,34 @@ | ||
<?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.example.mapper.UserMapper"> | ||
|
||
<!-- 选择表名和插入数据的示例 --> | ||
<insert id="insertUser" parameterType="com.example.User"> | ||
INSERT INTO ${tableName} (username, email, age) | ||
VALUES (#{username}, #{email}, #{age}) | ||
</insert> | ||
|
||
<!-- 使用动态表名和动态字段名查询用户信息 --> | ||
<select id="selectUserByUsername" parameterType="map" resultType="com.example.User"> | ||
SELECT ${columnName} as username, email, age | ||
FROM ${tableName} | ||
WHERE ${columnName} = #{value} | ||
</select> | ||
|
||
<!-- 更新用户信息 --> | ||
<update id="updateUserEmail" parameterType="map"> | ||
UPDATE ${tableName} | ||
SET email = #{email} | ||
WHERE username = #{username} | ||
</update> | ||
|
||
<!-- 删除用户 --> | ||
<delete id="deleteUser" parameterType="map"> | ||
DELETE FROM ${tableName} | ||
WHERE username = #{username} | ||
</delete> | ||
|
||
</mapper> |