1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
/**
* 基础通用BaseMapper
*/
public interface IBaseMapper<E> extends BaseMapper<E> {
default Class<E> getEntityClass() {
return (Class<E>) ReflectionKit.getSuperClassGenericType(this.getClass(), BaseMapper.class, 0);
}
@DeleteProvider(type = Provider.class, method = "truncate")
void truncate(String tableName);
default void truncate() {
truncate(getEntityClass().getAnnotation(TableName.class).value());
}
@SelectProvider(type = Provider.class, method = "select")
List<E> select(String tableName);
default List<E> select() {
return select(getEntityClass().getAnnotation(TableName.class).value());
}
@InsertProvider(type = Provider.class, method = "insert")
int insert(String tableName, String val);
default int insert(String val) {
return insert(getEntityClass().getAnnotation(TableName.class).value(), val);
}
/**
* Provider 使用 Map 接收
*/
@UpdateProvider(type = Provider.class, method = "update")
int update(@Param("tableName") String tableName, @Param("col") String col, @Param("colValue") String colValue, @Param("id") String id);
default int update(String col, String colValue, String id) {
return update(getEntityClass().getAnnotation(TableName.class).value(), col, colValue, id);
}
class Provider {
public String truncate(String tableName) {
return "TRUNCATE TABLE %s".formatted(tableName);
}
public String select(String tableName) {
return "select * from %s;".formatted(tableName);
}
public String insert(String tableName, String roleName) {
return new SQL() {{
INSERT_INTO(tableName);
VALUES("id", "'%s'".formatted(IdWorker.getId()));
VALUES("role_name", "'%s'".formatted(roleName));
}}.toString();
}
/**
* 特殊传参
*/
public String update(Map<String, String> map) {
return """
update %s set %s = '%s' where id = '%s' ;
""".formatted(map.get("tableName"), map.get("col"), map.get("colValue"), map.get("id"));
}
}
}
|