All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jnoee.xo.jpa.entity.StringToIdEntity Maven / Gradle / Ivy

The newest version!
package com.github.jnoee.xo.jpa.entity;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import com.github.jnoee.xo.jpa.dao.DaoUtils;


/**
 * 字符串转换成IdEntity转换器工厂。
 */
public class StringToIdEntity implements ConverterFactory {
  @Override
  public  Converter getConverter(Class targetType) {
    return new LongToIdEntityConverter<>(targetType);
  }

  /**
   * 字符串转换成IdEntity转换器。
   * 
   * @param  转换类型
   */
  private class LongToIdEntityConverter implements Converter {
    private final Class toClass;

    /**
     * 构造方法。
     * 
     * @param toClass 转换目标类
     */
    public LongToIdEntityConverter(Class toClass) {
      this.toClass = toClass;
    }

    /**
     * 将字符串值转换为IdEntity对象。
     * 
     * @param id ID
     * @return 返回IdEntity对象。
     */
    public T convert(String id) {
      if (id == null) {
        return null;
      }
      return DaoUtils.getEntity(toClass, Long.parseLong(id));
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy