Code Monkey home page Code Monkey logo

Comments (6)

chengliang4810 avatar chengliang4810 commented on June 27, 2024 1

我只是给出了一种模式您如果想要其他的可以自行实现毕竟我也不知道你要什么id或者说id本来就是跟着业务走的而不是框架提供的如果我提供了雪花id那么可能使用雪花id的人很方便但是如果您使用变种的雪花id那么我是否又要增加一个呢

如果由您来实现每一种生成方式肯定不合适,比较理想的方案应该是您来定个标准接口即可,例如下方代码

public interface PrimaryKeyGenerator {
        Serializable getPrimaryKey();
}

您的程序中只需要调用该接口的实例来获取结果即可,当然在这个标准接口下您可以增加一些常用的Id生成策略,比如雪花ID、UUId之类的。

from easy-query.

xuejmnet avatar xuejmnet commented on June 27, 2024

@chengliang4810 可以通过以下方式来实现

public interface HasId{
     String getId();
     void setId(String id);
}

@Data
public class MyUser implements HasId{
      private String id;
}

拦截器判断

 @Override
    public boolean apply(Class<?> entityClass) {
        return HasId.class.isAssignableFrom(entityClass);
    }

当然这只是一种手段你也可以使用注解模式

@Data
@HasIdAnnotation
public class MyUser{
      private String id;
}


    @Override
    public void configureInsert(Class<?> entityClass, EntityInsertExpressionBuilder entityInsertExpressionBuilder, Object entity) {



        EntityMetadata entityMetadata = entityInsertExpressionBuilder.getRuntimeContext().getEntityMetadataManager().getEntityMetadata(entityClass);


        String singleKey = entityMetadata.getSingleKeyProperty();//multi keys will throw exception
        ColumnMetadata columnOrNull = entityMetadata.getColumnOrNull(singleKey);
        if(columnOrNull!=null){
            Object idValue=columnOrNull.getGetterCaller().apply(entity);
            if(idValue==null){
                columnOrNull.getSetterCaller().call(entity,"我的id");
            }
        }        




//        EntityMetadata entityMetadata = entityInsertExpressionBuilder.getRuntimeContext().getEntityMetadataManager().getEntityMetadata(entityClass);
//
//        Collection<String> keyProperties = entityMetadata.getKeyProperties();
//        if(EasyCollectionUtil.isSingle(keyProperties)){
//            String singleKey = EasyCollectionUtil.first(keyProperties);
//            ColumnMetadata columnOrNull = entityMetadata.getColumnOrNull(singleKey);
//            if(columnOrNull!=null){
//                Object idValue=columnOrNull.getGetterCaller().apply(entity);
//                if(idValue==null){
//                    columnOrNull.getSetterCaller().call(entity,"我的id");
//                }
//            }
//        }
        //apply处判断是否有注解HasIdAnnotation
    }

from easy-query.

xuejmnet avatar xuejmnet commented on June 27, 2024

我只是给出了一种模式您如果想要其他的可以自行实现毕竟我也不知道你要什么id或者说id本来就是跟着业务走的而不是框架提供的如果我提供了雪花id那么可能使用雪花id的人很方便但是如果您使用变种的雪花id那么我是否又要增加一个呢

from easy-query.

xuejmnet avatar xuejmnet commented on June 27, 2024

非常棒的一个想法

from easy-query.

xuejmnet avatar xuejmnet commented on June 27, 2024
image image image

赋值会在interceptor之前进行,如果有不需要赋值的可以自行在拦截器里面取消掉 版本2.0.36+

from easy-query.

xuejmnet avatar xuejmnet commented on June 27, 2024

@chengliang4810
http://www.easy-query.com/easy-query-doc/guide/adv/auto-key.html

from easy-query.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.