Skip to content

LuomingXuOrg/JavaUtil

Repository files navigation

java工具类

Build status image image

储存不知道什么时候就会用到的工具类
名称 位置
密码 pwd
对list排序 sort
对list进行分页操作 page
entity之间的转化 converter
从sql生成框架需要的文件 frameworkFile
aop实现类 aop
分布式ID生成类 snowflake
全国高等学校名单(2019/6/15) college
mybatis column配置生成 mybatisColumns

Test, PR

如果有测试的代码请新建"/test"目录, 在此目录下进行测试

image

ID Generate Example

public class GenerateID
{
    public static long nextID()
    {
        Snowflake idGen = new Snowflake(1, 1, 1566885913535L);
        return idGen.nextId();
    }
}
// if you do not use default callback
public class CustomSnowflake extends Snowflake
{
    public CustomSnowflake(long dataCenterId, long workerId, long projectStartEpoch)
    {
        super(dataCenterId, workerId, projectStartEpoch);
    }

    @Override
    protected boolean timeBackwardsCallback(long timestamp, long lastTimestamp)
    {
        // do something
        return true;
    }
}

Cglib Converter Example

@Slf4j
public class ExampleConverter implements Converter
{
    /**
     * 自定义拷贝
     *
     * @param value   源对象
     * @param target  目标对象的class
     * @param context context目标对象set方法(就是个String类型的, 不知道为什么要用Object)
     * @return 目标对象
     */
    @Override
    @SuppressWarnings("unchecked")
    public Object convert(Object value, Class target, Object context)
    {
        log.info("value: {}", value.getClass().toString());
        log.info("target: {}", target.getName());
        log.info("context: {}", context.getClass().toString());

        if (value instanceof InnerModelDO)
        {
            InnerModelDTO obj = new InnerModelDTO();
            WrapperConverter.copy(value, obj, false, null);
            return obj;
        }

        if (value instanceof List)
        {
            List list = (List) value;
            if (!list.isEmpty() && list.get(0) instanceof InnerModelDO)
            {
                return WrapperConverter.copyList(list, InnerModelDTO.class, false, null);
            }
            else
            {
                return Collections.emptyList();
            }
        }

        return value;
    }
}

MyBatis columns gen

public class gen
{
    public static void main(String[] args)
    {
        String path = "C:\\Users\\Desktop\\script.sql";
        String formerColumnsResult = "<id column=\"id\" jdbcType=\"BIGINT\" property=\"id\"/>\n" +
                "<result column=\"comment\" jdbcType=\"VARCHAR\" property=\"comment\"/>\n" +
                "<result column=\"creator_id\" jdbcType=\"BIGINT\" property=\"creatorId\"/>\n" +
                "<result column=\"creator_name\" jdbcType=\"VARCHAR\" property=\"creatorName\"/>\n" +
                "<result column=\"create_time\" jdbcType=\"TIMESTAMP\" property=\"createTime\"/>";
        
        CompleteFullColumns.generate(path, formerColumnsResult);
    }
    
    /*
    result will like: 
    comment.id as comment_id,
    comment.comment as comment_comment,
    comment.creator_id as comment_creator_id,
    comment.creator_name as comment_creator_name,
    comment.create_time as comment_create_time,
    <id column="comment_id" jdbcType="BIGINT" property="id"/>
    <result column="comment_comment" jdbcType="VARCHAR" property="comment"/>
    <result column="comment_creator_id" jdbcType="BIGINT" property="creatorId"/>
    <result column="comment_creator_name" jdbcType="VARCHAR" property="creatorName"/>
    <result column="comment_create_time" jdbcType="TIMESTAMP" property="createTime"/>
    */
}

maven dependency

<dependency>
    <groupId>com.github.luomingxuorg</groupId>
    <artifactId>JavaUtil</artifactId>
    <version>$version</version>
    <!-- if you have "SLF4J: Class path contains multiple SLF4J bindings." warning
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
        </exclusion>
    </exclusions>
    -->
</dependency>