Spring Boot通过配置文件支持数据库自定义表名

直接上干货:

例如一个叫xxx的项目,yml文件里加上这段

xml 复制代码
xxxproject:
    db:
      xxxTable: xxx_dbname #自定义的数据库表名

创一个Configuration类放表名和Mapper

java 复制代码
// XxxProjectAutoConfiguration.java

@Configuration
@MapperScan(basePackages = "cn.com.xxxproject.db.mapper")
public class XxxProjectAutoConfiguration {
    
    @Value("${xxxproject.db.xxxTable}")
    private String xxxTable;
    @Autowired
    private XxxMapper xxxMapper;
    
    @Bean
    public DatabaseManager databaseManager() {
        return new DatabaseManager (xxxTable, xxxMapper);
    }
}

看到这聪明的你应该已经知道后续该怎么做了

java 复制代码
// DatabaseManager.java

public class DatabaseManager extends AbstractManager<XxxDO> {
    
    private static final String DEFAULT_LOCK_TABLE_NAME = "xxx_dbname";
    
    private final XxxMapper xxxMapper;
    
    private final String localIp;
    
    private final String xxxTable;
    
    public DatabaseLockManager(String xxxTable, XxxMapper xxxMapper) {
        this.xxxMapper = xxxMapper;
        this.localIp = getLocalIp();
        if (StringUtils.isNullOrEmpty(xxxTable)) {
            xxxTable = DEFAULT_LOCK_TABLE_NAME;
        }
        this.xxxTable = xxxTable;
    }

	// 随便写个方法,根据自己需求来定制
	@Override
    protected boolean isExists(Long id) {
        return xxxMapper.isExists(xxxTable, id);
    }

	@Override
    protected XxxDO selectForUpdate(String xxxKey) {
        return xxxMapper.selectForUpdate(xxxTable, xxxKey);
    }
}
xml 复制代码
<!-- LockMapper.xml -->

<?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="cn.com.xxxproject.db.mapper.XxxMapper">

</mapper>
java 复制代码
// LockMapper.java

@Mapper
public interface LockMapper {
    
    @Select("select * from ${xxxTable} where id = #{id}")
    @Results(id = "XxxMap",
             value = { @Result(property = "id", column = "id"), @Result(property = "xxxKey", column = "xxx_key"),
                 @Result(property = "nodeInfo", column = "node_info"), @Result(property = "count", column = "count"),
                 @Result(property = "version", column = "version"),
                 @Result(property = "createTime", column = "create_time"),
                 @Result(property = "updateTime", column = "update_time") })
    boolean isExists(String xxxTable, Long id);

	@Select("select * from ${xxxTable} where xxx_key= #{xxxKey} for update")
    @ResultMap("XxxMap")
    XxxDO selectForUpdate(String xxxTable, String xxxKey);
}    

完事!


顺便放两个关于Mybatis的好文章:

mybatis注解在方法直接编写SQL@Select@Insert@Update@Delete


MyBatis框架核心之注解使用@ResultMap及多表查询

第一篇是偏入门操作,第二篇的4.3多表查询真的适合反复细品

本篇用到:结果集映射相关注解

1、通过@Results、@Result注解定义结果集映射

如果实体字段的名称与数据库表字段名称不一致时,我们就需要显式的指定映射关系。这是通过@Results、@Result注解来指定的,例如为UserMapper的selectById指定映射关系:

java 复制代码
@Select("SELECT id,name FROM user where id= #{id}")
@Results(id = "userMap", value = {
      @Result(property = "id", column = "id", javaType = Integer.class,jdbcType = JdbcType.INTEGER,id = true),
      @Result(property = "name", column = "name",javaType = String.class,jdbcType = JdbcType.VARCHAR)})
public User selectById(int id);

其中:

@Results注解:id属性用于给这个映射关系起一个名字(这里指定的为userMap),其内部还包含了一个@Result[]来表示实体属性和数据库表字段的映射关系

@Result注解:property属性是java实体属性的名称,column表示对应的数据库字段的名称。javaType和JdbcType属性可以不指定。

2、通过@ResultMap复用结果集映射

上述方法electById方法已经通过@Results注解指定了结果映射关系,可以通过@ResultMap来引用@Results的id属性值进行复用。如在UserMapper的selectAll方法进行复用:

java 复制代码
@Select("SELECT id,name FROM user")
@ResultMap("userMap")
public List<User> selectAll();

THX~

相关推荐
qq_508823403 小时前
金融数据库--3Baostock
数据库·金融
Lionel_SSL3 小时前
《深入理解Java虚拟机》第三章读书笔记:垃圾回收机制与内存管理
java·开发语言·jvm
记得开心一点嘛3 小时前
手搓Springboot
java·spring boot·spring
悦数图数据库4 小时前
图技术重塑金融未来:悦数图数据库如何驱动行业创新与风控变革
数据库·金融
九河云4 小时前
华为云 GaussDB:金融级高可用数据库,为核心业务保驾护航
网络·数据库·科技·金融·华为云·gaussdb
老华带你飞4 小时前
租房平台|租房管理平台小程序系统|基于java的租房系统 设计与实现(源码+数据库+文档)
java·数据库·小程序·vue·论文·毕设·租房系统管理平台
独行soc4 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
脑子慢且灵4 小时前
[JavaWeb]模拟一个简易的Tomcat服务(Servlet注解)
java·后端·servlet·tomcat·intellij-idea·web
华仔啊5 小时前
SpringBoot 中 6 种数据脱敏方案,第 5 种太强了,支持深度递归!
java·后端
异常驯兽师6 小时前
Spring 中处理 HTTP 请求参数注解全解析
java·spring·http