深圳人才网站建设,中国十大最著名品牌,个人网站搭建模拟感想,有什么建筑网站Spring中Service对应的实体Entity类上不写TableName(value)的注解#xff0c;怎样对应到数据表#xff1f;
在使用 MyBatis-Plus 时#xff0c;如果实体类没有使用 TableName(value) 注解来指定数据库表名#xff0c;MyBatis-Plus 会根据默认的命…Spring中Service对应的实体Entity类上不写TableName(value)的注解怎样对应到数据表
在使用 MyBatis-Plus 时如果实体类没有使用 TableName(value) 注解来指定数据库表名MyBatis-Plus 会根据默认的命名策略将实体类名称映射到数据库表名。默认情况下MyBatis-Plus 会将实体类名称转换为下划线分隔的小写表名。
默认命名策略
MyBatis-Plus 的默认命名策略是将驼峰命名法的实体类名称转换为下划线分隔的小写表名。例如
实体类 User 将映射到数据库表 user。实体类 UserProfile 将映射到数据库表 user_profile。
示例代码
假设有一个实体类 User
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.IdType;public class User {TableId(value id, type IdType.AUTO)private Long id;private String name;// getters and setters
}在没有 TableName 注解的情况下MyBatis-Plus 会默认将这个实体类映射到名为 user 的数据库表。
自定义命名策略
如果你希望使用不同的命名策略可以通过配置文件来进行配置。例如可以配置为将实体类名称直接映射为表名不进行转换
在 application.properties 或 application.yml 文件中进行配置
mybatis-plus.global-config.db-config.table-underlinefalse或者使用自定义的命名策略 定义自定义命名策略 import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MyBatisPlusConfig {Beanpublic GlobalConfig globalConfig() {GlobalConfig globalConfig new GlobalConfig();globalConfig.setDbConfig(new GlobalConfig.DbConfig().setTablePrefix(tbl_));return globalConfig;}
}配置文件中启用自定义配置 在 application.properties 或 application.yml 文件中进行配置 mybatis-plus.global-config.db-config.table-prefixtbl_这样实体类 User 将会映射到名为 tbl_user 的数据库表。
总结
如果不使用 TableName(value) 注解MyBatis-Plus 会根据默认的命名策略将实体类名称映射到数据库表名。默认情况下MyBatis-Plus 会将驼峰命名法的实体类名称转换为下划线分隔的小写表名。你可以通过配置文件或自定义命名策略来控制这种映射规则。