mybatis @SelectProvider 查询 in条件动态拼接id

mybatis @SelectProvider 查询 in条件动态拼接id录标题

不想写mxl,sql不好调试,习惯用mybatis对应的Provider来处理。这里主要解决in条件中动态拼接id和避免sql注入的问题,废话不多说,直接show code(只是部分代码片段)

@Service
@Slf4j
public class StoreServiceImpl implements StoreService {
	@Autowired
    private StoreMapper storeMapper;
	
	@Override
    public APIResponseBody getStoreList() {
        StoreVo storeVo = new StoreVo();
		storeVo.setStoreName("步行街");
		String storeIdStr = "d7b82fdcf5f8444eaf66af2bddbba5ac,34d325c8596e4c528073a5699ddefeae,6fbfbff9b77046a7b316eef35c721ff9";
		String[] storeIdArr = storeIdStr.split(",");
		storeMapper.getStoreList(storeVo,storeIdArr);
    }	
}
@Component
public interface StoreMapper {
    @SelectProvider(type = StoreBuilder.class,method = "getStoreList")
    List<StoreEntity> getStoreList(@Param("storeVo") StoreVo storeVo,@Param("storeIdArr") String[] storeIdArr);
}
public class StoreBuilder {
    public String getStoreList(StoreVo storeVo, String[] storeIdArr){
        StringBuilder sql = new StringBuilder(128);
        //注意sql外面要用包起来
        sql.append("");
        return sql.toString();
    }
}
debug输出结果
[DEBUG] 2020-01-12 14:58:49.254 [http-nio-19201-exec-1] net.app.microservice.mapper.StoreMapper.getStoreList - ==>  Preparing: select * from stc_store where 1=1 and store_id in ( ? , ? , ? ) and store_name like concat('%',?,'%') 
[DEBUG] 2020-01-12 14:58:49.255 [http-nio-19201-exec-1] net.app.microservice.mapper.StoreMapper.getStoreList - ==> Parameters: d7b82fdcf5f8444eaf66af2bddbba5ac(String), 34d325c8596e4c528073a5699ddefeae(String), 6fbfbff9b77046a7b316eef35c721ff9(String), 步行街(String)

你可能感兴趣的:(mybatis,mybatis,sql)