mybatis plus 的动态表名的配置
时间: 2023-09-28 16:03:25 浏览: 101
MyBatis Plus 支持动态表名的配置,可以通过在 SQL 中使用占位符 `{}` 或 `#{}` 来指定动态表名,然后在代码中通过传入参数来替换占位符。
下面是一个示例:
```java
@Mapper
public interface UserMapper {
@Select("SELECT * FROM ${tableName} WHERE id = #{id}")
User selectById(@Param("tableName") String tableName, @Param("id") Long id);
}
```
在上面的代码中,`tableName` 参数指定了动态表名,然后在 SQL 中使用 `${tableName}` 占位符来引用它。另外,`id` 参数是普通的参数,使用 `#{}` 占位符来引用。
使用时,可以通过传入 `tableName` 参数来指定具体的表名,例如:
```java
@Autowired
private UserMapper userMapper;
User user = userMapper.selectById("user_table", 123L);
```
在上面的代码中,传入的 `tableName` 参数为 `"user_table"`,会被替换到 SQL 中的 `${tableName}` 占位符中,从而实现动态表名的配置。
相关问题
MyBatis-Plus表名加上“”
通过使用MyBatis-Plus的`TableName`注解,我们可以为实体类设置表名,例如:
```java
@TableName(value = "table_name")
public class User {
//...
}
```
在`@TableName`注解中传入要设置的表名即可。如果表名是动态变化的,可以使用`${}`占位符配合properties文件进行配置,例如:
```java
@TableName(value = "${tableName}")
public class User {
//...
}
```
然后在resources目录下新建一个properties文件进行配置,例如application.properties:
```properties
tableName=table_name
```
这样MyBatis-Plus就会自动将`${tableName}`替换成`table_name`了。
java中调用数学函数加前缀,mybatis-plus 表名添加前缀的实现方法
1. Java中调用数学函数加前缀:
可以使用Java的Math类来调用数学函数,例如Math.sin()、Math.cos()等。如果要在函数名前面加前缀,可以使用静态导入的方式,将Math类中的所有函数都导入进来,然后在调用时直接写函数名即可。
例如:
```
import static java.lang.Math.*;
public class Test {
public static void main(String[] args) {
double x = PI / 2;
double y = sin(x);
System.out.println("sin(" + x + ") = " + y);
}
}
```
输出结果为:
```
sin(1.5707963267948966) = 1.0
```
2. Mybatis-plus表名添加前缀:
Mybatis-plus可以通过配置文件中的tablePrefix属性来给表名添加前缀,例如:
```
mybatis-plus:
global-config:
db-config:
table-prefix: prefix_
```
这样,在使用Mybatis-plus的Mapper接口操作数据库时,所有的表名都会加上前缀"prefix_"。例如,如果有一个表名为"user",在Mapper接口中查询时就需要写成"prefix_user"。
阅读全文
相关推荐














