CRUD


增@Insert

@Insert("INSERT INTO `user`(`user_name`, `password`, `phone`, `email`, `gender`,`icon`, `number`, `post_id`, `post_name`) "
        + "values(#{userName}, #{password}, #{phone}, #{email}, #{gender}, #{icon}, #{number}, #{postId}, #{postName});")
void insert(UserEntity customerUserEntity);

删@Delete

@Delete("delete from `user` where user_name = #{userName}")
void deleteUserByUserName(String userName);

查@Select

普通查询

//查全表
@Select("SELECT * FROM `alarm_records`")
List<AlarmRecordEntity> getAllAlarmRecords();

//查某列(某字段)
@Select("SELECT name FROM `user`")
List<AlarmRecordEntity> getAllAlarmRecords();

//根据某字段查所有符合条件的
@Select("SELECT * from `user` WHERE number = #{number};")
UserEntity queryUserByNumber(@Param("number") String number);

//多条件查询
@Select("SELECT * from `user` WHERE number = #{number} and name = #{name};")
UserEntity queryUserByNumber(@Param("number") String number);

重复的只取一次

遍历某个字段,重复的只取一个(时间戳最近的一个)

把company换成*,即取出整条记录

示例为alarm_records_company表中,company重复的按alarm_time时间最近的取值。

mapper

@Select(" select classtab.company FROM (SELECT company, MAX(alarm_time) alarm_time FROM alarm_records_company GROUP BY company) " +
        "tmp LEFT JOIN alarm_records_company classtab ON " +
        "classtab.company = tmp.company AND classtab.alarm_time = tmp.alarm_time;")
List<String> getAllCompany();

条件查询@SelectProvider

适用不能提前预知哪个条件来查询

前端返回需要查询的参数,AlarmService.class层做逻辑判断,不为空的参数即为要查询的

mapper

@SelectProvider(type = AlarmService.class, method = "selectRecordsByDynamicSql")
List<AlarmRecordEntity> getAlarmRecordsByFeature(HashMap<String,String> features);

AlarmService.class

public String selectRecordsByDynamicSql(HashMap<String,String> features){
    String sql = new SQL(){
        {
            SELECT("*");
            FROM("alarm_records");
            if (!ObjectUtil.isEmptyString(features.get("monitor"))){
                WHERE("monitor = '"+features.get("monitor")+"'");
            }
            if (!ObjectUtil.isEmptyString(features.get("monitorClass"))){
                WHERE("monitor_class = '"+features.get("monitorClass")+"'");
            }
            if (!ObjectUtil.isEmptyString(features.get("monitorValue"))){
                WHERE("monitor_value = '"+features.get("monitorValue")+"'");
            }
            if (!ObjectUtil.isEmptyString(features.get("startTime"))){
                WHERE("alarm_time >= '"+features.get("startTime")+"'");
            }
            if (!ObjectUtil.isEmptyString(features.get("endTime"))){
                WHERE("alarm_time <= '"+features.get("endTime")+"'");
            }
        }
    }.toString();
    return sql;
}

改@Update

```sql
//根据某个字段更新该记录的另一个字段
@Update(“UPDATE user SET icon=#{icon} WHERE user_name=#{userName};”)
void updateIconByUserName(@Param(“userName”) String userName, @Param(“icon”) String icon);

//在整条记录上修改各个字段(id主键不会变)
@Update(“UPDATE alarm_settings “ +
“SET “ +
monitor_class = #{monitorClass}, “ +
monitor_value = #{monitorValue}, “ +
lower_limit = #{lowerLimit}, “ +
upper_limit = #{upperLimit}, “ +
message = #{message} “ +
“WHERE id=#{id}”)
public int updateAlarmSetting(AlarmSettingsEntity alarmSettingsEntity);

其他资料

分组查询:https://blog.csdn.net/weixin_41885239/article/details/115933896?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168204908516800180666125%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168204908516800180666125&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-2-115933896-null-null.142^v86^insert_down1,239^v2^insert_chatgpt&utm_term=sql%20GROUP%20BY&spm=1018.2226.3001.4187


文章作者: Luan-bx
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Luan-bx !
  目录