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~

相关推荐
小莫分享4 分钟前
github 镜像节点
java
链上Sniper12 分钟前
智能合约状态快照技术:实现 EVM 状态的快速同步与回滚
java·大数据·linux·运维·web3·区块链·智能合约
大只鹅34 分钟前
Springboot3整合ehcache3缓存--XML配置和编程式配置
spring boot·缓存
缘来是庄1 小时前
设计模式之建造者模式
java·设计模式·建造者模式
小湘西1 小时前
Apache HttpClient 的请求模型和 I/O 类型
java·http·apache
找不到、了1 小时前
MySQL的窗口函数介绍
数据库·mysql
沃夫上校1 小时前
Feign调Post接口异常:Incomplete output stream
java·后端·微服务
q567315231 小时前
Java Selenium反爬虫技术方案
java·爬虫·selenium
张小洛1 小时前
Spring IOC容器核心阶段解密:★Bean实例化全流程深度剖析★
java·后端·spring·ioc容器·bean实例化
执笔诉情殇〆1 小时前
springboot集成达梦数据库,取消MySQL数据库,解决问题和冲突
数据库·spring boot·mysql·达梦