当前位置: 首页 > 工具软件 > pinyin-plus > 使用案例 >

Mybatis-Plus使用基础

东郭京
2023-12-01

实体类

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("area")
public class AreaMp implements Serializable {

    private static final long serialVersionUID=1L;

    /**
     * 主键
     */
      @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 区域名称
     */
    private String name;

    /**
     * 区域类型:省/市/区
     */
    private Integer areaType;

    /**
     * 父级编号
     */
    private Integer parentId;

    /**
     * 是否主要区域(用于标示省会城市) 0:否 1:是
     */
    private Integer isPrimary;

    /**
     * 拼音首字母
     */
    private String pinyinInitial;

    /**
     * 名称全拼
     */
    private String fullPinyin;

    /**
     * 名称全拼,逗号分隔
     */
    private String fullWordPinyin;

    /**
     * 添加记录时间
     */
    private Integer addtime;

    /**
     * 修改记录时间
     */
    private Integer modtime;


}

Dao

public interface AreaMpDao extends IService<AreaMp> {

}

@Service
public class AreaMpDaoImpl extends ServiceImpl<AreaMpMapper, AreaMp> implements AreaMpDao {

}

Mapper

public interface AreaMpMapper extends BaseMapper<AreaMp> {

}

查询

  • 主键
AreaMp areaMp1 = areaMpDao.getById(id);
// SELECT id,name,area_type,parent_id,is_primary,pinyin_initial,full_pinyin,full_word_pinyin,addtime,modtime FROM area WHERE id=?

QueryWrapper<AreaMp> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(AreaMp::getId,id);
AreaMp areaMp2 = areaMpDao.getOne(wrapper);
// SELECT id,name,area_type,parent_id,is_primary,pinyin_initial,full_pinyin,full_word_pinyin,addtime,modtime FROM area WHERE (id = ?)

LambdaQueryWrapper<AreaMp> rapper = Wrappers.<AreaMp>lambdaQuery()
        .eq(AreaMp::getId,id);
AreaMp areaMp3 = areaMpDao.getOne(rapper);
// SELECT id,name,area_type,parent_id,is_primary,pinyin_initial,full_pinyin,full_word_pinyin,addtime,modtime FROM area WHERE (id = ?)
  • 列表
LambdaQueryWrapper<AreaMp> rapper = Wrappers.<AreaMp>lambdaQuery()
                .eq(AreaMp::getAreaType,2)
                .like(AreaMp::getName,"河");
List<AreaMp> list = areaMpDao.list(rapper);
//  SELECT id,name,area_type,parent_id,is_primary,pinyin_initial,full_pinyin,full_word_pinyin,addtime,modtime FROM area WHERE (area_type = ? AND name LIKE ?)
  • 分页
Page<AreaMp> page = new Page<>(1, 3);
        LambdaQueryWrapper<AreaMp> rap = Wrappers.<AreaMp>lambdaQuery()
                .eq(AreaMp::getAreaType,2)
                .orderByDesc(AreaMp::getId)
                .orderByAsc(AreaMp::getAreaType);
Page<AreaMp> mpPage = areaMpDao.page(page, rap);
// SELECT COUNT(1) FROM area WHERE (area_type = ?)
// SELECT id,name,area_type,parent_id,is_primary,pinyin_initial,full_pinyin,full_word_pinyin,addtime,modtime FROM area WHERE (area_type = ?) ORDER BY id DESC,area_type ASC LIMIT ?,?


Page<AreaMp> areaMpPage = areaMpDao.lambdaQuery()
                .eq(AreaMp::getAreaType, 2)
                .orderByDesc(AreaMp::getId)
                .page(new Page<>(1, 6));
// SELECT COUNT(1) FROM area WHERE (area_type = ?)
// SELECT id,name,area_type,parent_id,is_primary,pinyin_initial,full_pinyin,full_word_pinyin,addtime,modtime FROM area WHERE (area_type = ?) ORDER BY id DESC LIMIT ?,?

新增

AreaMp add = new AreaMp();
areaMpDao.save(add);
        
areaMpDao.saveBatch(Collections.singletonList(add));

更新

UpdateWrapper<AreaMp> updateWrapper = new UpdateWrapper<>();
updateWrapper.lambda()
        .eq(AreaMp::getId,666)
        .set(AreaMp::getIsPrimary,1);
boolean update = areaMpDao.update(updateWrapper);

Wrapper<AreaMp> whereWrapper = Wrappers.<AreaMp>lambdaUpdate()
        .eq(AreaMp::getId,666);

AreaMp up = new AreaMp();
up.setIsPrimary(1);
boolean upda = areaMpDao.update(up, whereWrapper);

删除

boolean b = areaMpDao.removeById(10000);
// DELETE FROM area WHERE id=?
        
boolean remove = areaMpDao.remove(Wrappers.<AreaMp>lambdaQuery().eq(AreaMp::getAreaType, 4));

下一篇

Mybatis-Plus使用进阶

 类似资料: