SpringBoot学习(15)— 一个实体映射相同结构的多张数据表

介绍

在项目开发中,经常会分表,所以会导致多个表会拥有相同结构,对应一个实体的需求

使用步骤

  • 根据表结构建立实体类
package com.eceibs.report.entity;

import org.springframework.stereotype.Repository;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Repository
public class DynamicLog {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;

    private Integer userId;

    private String loginName;

    private String username;

    private String ip;

    private Integer duration;

    private String action;

    private Integer operateId;

    private Integer trainplanId;

    private Integer authType;

    private String actionType;

    private String subActionType;

    private Integer client;

    private Integer createdDay;

    private Integer updatedAt;

    private Integer createdAt;

}

  • 创建实体类对应的Mapper
package com.eceibs.report.mapper.log;

import com.eceibs.report.entity.DynamicLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

@Mapper
@Repository
public interface DynamicLogMapper {

    @Select("select id,user_id,login_name,username,ip,duration,action,operate_id,trainplan_id,auth_type,sub_action_type,client,created_day,updated_at,created_at from intest_dynamic_log limit 10")
    List findAll();

    /**
     *
     * @param logTable  表名,字段名使用 ${}
     * @param superUserIds   字符串名使用 #{}   会自带引号
     * @return
     */
    @Select("SELECT SUM(duration) AS total_duration FROM ${logTable} WHERE user_id NOT IN (#{superUserIds}) AND is_deleted=0 ")
    BigInteger getTotalDuration(String logTable, String superUserIds);
}

小结

与正常的映射没啥区别,只需要在对应的SQL里注意写入需要操作的表

你可能感兴趣的:(SpringBoot学习(15)— 一个实体映射相同结构的多张数据表)