mybatis-plus CURD
mybatis-plus
- 查询指定字段
.select(“字段名”,“字段名”)
EsinfoAreaKeeperObject esinfoAreaKeeperObject = areaKeeperObjectMapper.selectOne(new QueryWrapper<EsinfoAreaKeeperObject>()
.select("fldObjectGuid")
.eq("fldKeeperGuid", keeperId));
2.查询语法(网上找的图)
mybatis-plus 插入添加多个对象save方法
BaseMapper没有方法插入多个对象,但 IService 有插入多个对象的方法
private IAreaKeeperObjectService iAreaKeeperObjectService;
List<EsinfoAreaKeeperObject> areaKeeperObjectVos = esinfoAreakeeper.getAreaKeeperObjectVos();
iAreaKeeperObjectService.saveBatch(areaKeeperObjectVos);
关联查询,自动映射
- xml 中
type => 映射的对象,id=“cmap” => cmap 是映射对象的别称
collection,使用标签,property=>映射的对象,select=>使用的方法,column=>对应的关联值
result 标签中,column=>数据库字段名,property=>映射对象字段名
<resultMap type="com.es.base.info.vo.EsinfoObjectandownerVo" id="cmap">
<id property="fldGuid" column="fldGuid" />
<result column="fldObjectGuid" property="fldObjectGuid"/>
<result column="fldOwnerGuid" property="fldOwnerGuid"/>
<collection property="esInfoObjectVo" select="com.es.info.mapper.EsinfoObjectMapper.selectByfldObjectGuid" column="fldObjectGuid">
</collection>
<collection property="esinfoPersonbaseVo" select="com.es.info.mapper.EsinfoPersonbaseMapper.selectOnePersonbase" column="fldOwnerGuid">
</collection>
</resultMap>
- 实体类中
字段名要与 xml 中相同,可以不用全部映射
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value="对象")
public class EsinfoObjectandownerVo {
@ApiModelProperty(value = "主键")
private String fldGuid;
@ApiModelProperty(value = "小区主键")
private String fldAreaGuid;
@ApiModelProperty(value = "资源主键")
private String fldObjectGuid;
@ApiModelProperty(value = "客户主表主键")
private String fldOwnerGuid;
@ApiModelProperty(value = "居住关系")
private Integer fldIsOwner;
@ApiModelProperty(value = "是否计费(0否 1是)")
private Integer fldIsCharge;
@ApiModelProperty(value = "客户等级(是否是重要客户)")
private Integer fldRelation;
@ApiModelProperty(value = "开始日期")
private String fldBeginDate;
@ApiModelProperty(value = "结束日期")
private String fldEndDate;
@ApiModelProperty(value = "备注")
private String fldDesc;
@ApiModelProperty(value = "是否当前(0否 1是)")
private Integer fldIsCurrent;
@ApiModelProperty(value = "迁出客户原因")
private String fldOutReason;
@ApiModelProperty(value = "业主对象")
private List<EsinfoPersonbaseVo> esinfoPersonbaseVo;
@ApiModelProperty(value = "业主资源对象")
private EsInfoObjectVo esInfoObjectVo;
}
- 方法中
resultMap => 选择映射对象的别称,就是上面设置的
<select id="selectList" parameterType="java.lang.String" resultMap="cmap">
select fldOwnerGuid,fldObjectGuid from esinfo_objectandowner where fldObjectGuid = #{fldObjectGuid};
</select>
查询时,添加一行默认值的列
别名要是取的和映射字段名一样,也会自动映射
select #{传入的字段名} as 别名 from 表
循环查询,循环条件查询
collection => 循环对象
item => 每次循环出的对象
open => 循环体前加的值,close => 循环体后加的值,separator => 每次循环后加的值
<select id="selectListByObjectIds" resultType="com.es.base.info.vo.AreaKeeperObjectVo">
select distinct fldObjectGuid,fldIsHouse,#{keeperId} as fldKeeperGuid
from esinfo_areakeeperobject as a
where fldObjectGuid in
<foreach collection="objectIds" item="objectId" open="(" close=")" separator=",">
#{objectId}
</foreach>
</select>
查询时判断,赋予新值方法,case when then
case相当与你要查询的字段
select
case 字段名
when 条件1
then 满足条件的值
when 条件2
then 满足条件的值
when 条件3
then 满足条件的值
else
then 其余条件的值
end
as 别名
from 表名