当前位置: 首页>>代码示例>>Java>>正文


Java IdWorker类代码示例

本文整理汇总了Java中com.baomidou.mybatisplus.toolkit.IdWorker的典型用法代码示例。如果您正苦于以下问题:Java IdWorker类的具体用法?Java IdWorker怎么用?Java IdWorker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


IdWorker类属于com.baomidou.mybatisplus.toolkit包,在下文中一共展示了IdWorker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
/**
  * <p>
  * 事务测试
  * </p>
  */
 public static void main(String[] args) {
     /*
      * 加载配置文件
*/
     InputStream in = TransactionalTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
     MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
     SqlSessionFactory sessionFactory = mf.build(in);
     SqlSession sqlSession = sessionFactory.openSession();
     /**
      * 插入
      */
     UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
     int rlt = userMapper.insert(new User(IdWorker.getId(), "1", 1, 1));
     System.err.println("--------- insertInjector --------- " + rlt);

     //session.commit();
     sqlSession.rollback();
     sqlSession.close();
 }
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:25,代码来源:TransactionalTest.java

示例2: main

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public static void main(String[] args) {
     /*
      * 加载配置文件
*/
     InputStream in = NoXMLTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
     MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
     SqlSessionFactory sessionFactory = mf.build(in);
     SqlSession sqlSession = sessionFactory.openSession();
     /**
      * 查询是否有结果
      */
     TestMapper testMapper = sqlSession.getMapper(TestMapper.class);
     testMapper.insert(new Test(IdWorker.getId(), "Caratacus"));
     List<Map<String, Object>> list = testMapper.selectMaps(null);
     List<Map<String, Object>> list1 = testMapper.selectMapsPage(RowBounds.DEFAULT, null);
     List<Map<String, Object>> list2 = testMapper.selectMapsPage(new Page<>(1, 5), null);
     System.out.println(list);
     System.out.println(list1);
     System.out.println(list2);
     testMapper.delete(null);

 }
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:23,代码来源:NoXMLTest.java

示例3: test

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
/**
 * <p>
 * 事务测试
 * </p>
 */
@Test
public void test() {
    SqlSession sqlSession = this.sqlSessionFactory().openSession();

    /**
     * 插入
     */
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    int rlt = userMapper.insert(new User(IdWorker.getId(), "1", 1, 1));
    System.err.println("--------- insertInjector --------- " + rlt);

    //session.commit();
    sqlSession.rollback();
    sqlSession.close();
}
 
开发者ID:baomidou,项目名称:mybatis-plus,代码行数:21,代码来源:TransactionalTest.java

示例4: populateKeys

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
/**
 * <p>
 * 自定义元对象填充控制器
 * </p>
 *
 * @param metaObjectHandler 元数据填充处理器
 * @param tableInfo         数据库表反射信息
 * @param ms                MappedStatement
 * @param parameterObject   插入数据库对象
 * @return Object
 */
protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,
                                     MappedStatement ms, Object parameterObject) {
    if (null == tableInfo || StringUtils.isEmpty(tableInfo.getKeyProperty()) || null == tableInfo.getIdType()) {
        /* 不处理 */
        return parameterObject;
    }
    /* 自定义元对象填充控制器 */
    MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
    if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
        if (IdType.ID_WORKER.equals(tableInfo.getIdType()) || IdType.UUID.equals(tableInfo.getIdType())) {
            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
            /* 自定义 ID */
            if (StringUtils.checkValNull(idValue)) {
                if (IdType.ID_WORKER.equals(tableInfo.getIdType())) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
                } else if (IdType.UUID.equals(tableInfo.getIdType())) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                }
            }
        }
        // 插入填充
        if (metaObjectHandler.openInsertFill()) {
            metaObjectHandler.insertFill(metaObject);
        }
    } else if (ms.getSqlCommandType() == SqlCommandType.UPDATE && metaObjectHandler.openUpdateFill()) {
        // 更新填充
        metaObjectHandler.updateFill(metaObject);
    }
    return metaObject.getOriginalObject();
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:42,代码来源:MybatisDefaultParameterHandler.java

示例5: test

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
@Test
public void test() throws Exception {
    double wucha = 0.05;
    int count = 1000;
    int wuchaNum = (int) (count * wucha);
    int high = count + wuchaNum;
    int low = count - wuchaNum;
    System.err.println("共有" + count + "个数参与测试,误差系数为" + wucha + "误差值为" + wuchaNum);

    ExecutorService executorService = Executors.newFixedThreadPool(20);
    final List<Long> results = new ArrayList<>();
    CompletionService<Long> cs = new ExecutorCompletionService<>(executorService);
    for (int i = 1; i < count; i++) {
        cs.submit(() -> {
            Thread.sleep(RandomUtils.nextInt(1, 2000));
            return IdWorker.getId();
        });
    }
    for (int i = 0; i < count; i++) {
        Future<Long> future = executorService.submit(IdWorker::getId);
        results.add(future.get());
    }
    executorService.shutdown();
    HashSet<Long> set = new HashSet<>(results);
    // 判断是否有重复
    Assert.assertEquals(count, set.size());
    int odd = 0;
    int even = 0;
    for (Long id : results) {
        if (id % 2 != 0) {
            odd++;
        } else {
            even++;
        }
    }
    System.err.println("奇数:" + odd);
    System.err.println("偶数:" + even);
    Assert.assertTrue(odd >= low && odd <= high);
    Assert.assertTrue(even >= low && even <= high);
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:41,代码来源:IdWorkerTest.java

示例6: testSqlInjector

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public void testSqlInjector() {
    Long id = IdWorker.getId();
    int rlt = baseMapper.insert(new User(id, "abc", 18, 1));
    System.err.println("插入ID:" + id + ", 执行结果:" + rlt);
    rlt = baseMapper.deleteLogicById(id);
    System.err.println("测试注入执行结果:" + rlt);
    System.err.println("逻辑修改:" + baseMapper.selectById(id).toString());
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:9,代码来源:UserServiceImpl.java

示例7: populateKeys

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
/**
 * <p>
 * 自定义元对象填充控制器
 * </p>
 *
 * @param metaObjectHandler 元数据填充处理器
 * @param tableInfo         数据库表反射信息
 * @param ms                MappedStatement
 * @param parameterObject   插入数据库对象
 * @return Object
 */
protected static Object populateKeys(MetaObjectHandler metaObjectHandler, TableInfo tableInfo,
                                     MappedStatement ms, Object parameterObject) {
    if (null == tableInfo || StringUtils.isEmpty(tableInfo.getKeyProperty()) || null == tableInfo.getIdType()) {
        /* 不处理 */
        return parameterObject;
    }
    /* 自定义元对象填充控制器 */
    MetaObject metaObject = ms.getConfiguration().newMetaObject(parameterObject);
    if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
        if (tableInfo.getIdType().getKey() >= 2) {
            Object idValue = metaObject.getValue(tableInfo.getKeyProperty());
            /* 自定义 ID */
            if (StringUtils.checkValNull(idValue)) {
                if (tableInfo.getIdType() == IdType.ID_WORKER) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getId());
                } else if (tableInfo.getIdType() == IdType.ID_WORKER_STR) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.getIdStr());
                } else if (tableInfo.getIdType() == IdType.UUID) {
                    metaObject.setValue(tableInfo.getKeyProperty(), IdWorker.get32UUID());
                }
            }
        }
        // 插入填充
        if (metaObjectHandler.openInsertFill()) {
            metaObjectHandler.insertFill(metaObject);
        }
    } else if (ms.getSqlCommandType() == SqlCommandType.UPDATE && metaObjectHandler.openUpdateFill()) {
        // 更新填充
        metaObjectHandler.updateFill(metaObject);
    }
    return metaObject.getOriginalObject();
}
 
开发者ID:baomidou,项目名称:mybatis-plus,代码行数:44,代码来源:MybatisDefaultParameterHandler.java

示例8: test

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public void test() throws Exception {
    int count = 1000;
    System.err.println("共有" + count + "个数参与测试");

    ExecutorService executorService = Executors.newFixedThreadPool(20);
    final List<Long> results = new ArrayList<>();
    CompletionService<Long> cs = new ExecutorCompletionService<>(executorService);
    for (int i = 1; i < count; i++) {
        cs.submit(new Callable<Long>() {
            public Long call() throws Exception {
                Thread.sleep(RandomUtils.nextInt(1, 2000));
                return IdWorker.getId();
            }
        });
    }
    for (int i = 0; i < count; i++) {
        Future<Long> future = executorService.submit(new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                return IdWorker.getId();
            }
        });
        results.add(future.get());
    }
    executorService.shutdown();
    HashSet<Long> set = new HashSet<>(results);
    // 判断是否有重复
    Assert.assertEquals(count, set.size());
    int odd = 0;
    int even = 0;
    for (Long id : results) {
        if (id % 2 != 0) {
            odd++;
        } else {
            even++;
        }
    }
    System.err.println("奇数:" + odd);
    System.err.println("偶数:" + even);
}
 
开发者ID:baomidou,项目名称:mybatis-plus,代码行数:41,代码来源:IdWorkerTest.java

示例9: test2

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public void test2() throws Exception {
    // 随机尾数
    for (int i = 0; i < 1000; i++) {
        Thread.sleep(10);
        System.out.println(IdWorker.getId());
    }
}
 
开发者ID:baomidou,项目名称:mybatis-plus,代码行数:8,代码来源:IdWorkerTest.java

示例10: run

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public void run() {
    try {
        long id = IdWorker.getId();
        System.err.println(id);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:baomidou,项目名称:mybatisplus-spring-mvc,代码行数:9,代码来源:IdWorkerTest.java

示例11: main

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public static void main(String[] args) {
    // 加载配置文件
    InputStream in = TestMapper.class.getClassLoader().getResourceAsStream("mysql-config.xml");
    MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
    SqlSessionFactory sqlSessionFactory = mf.build(in);
    TableInfoHelper.initSqlSessionFactory(sqlSessionFactory);
    sqlSessionFactory.openSession(false);
    // 保存一条记录
    Test t1 = new Test();
    t1.setType("test10");
    boolean rlt = t1.insert();
    print(" ar save=" + rlt + ", id=" + t1.getId());

    // 根据ID更新
    t1.setType("t1023");
    rlt = t1.updateById();
    print(" ar updateById:" + rlt);

    // 更新 SQL
    Test t11 = new Test();
    t11.setType("123");
    rlt = t11.update("id={0}", t1.getId());
    print("update sql=" + rlt);

    // 查询 SQL
    Test t10 = t1.selectOne("id={0}", t1.getId());
    print("selectOne=" + t10.getType());

    // 插入OR更新
    t1.setType("t1021");
    rlt = t1.insertOrUpdate();
    print(" ar saveOrUpdate:" + rlt);

    // 根据ID查询
    Test t2 = t1.selectById();
    print(" t2 = " + t2.toString());
    t2.setId(IdWorker.getId());
    t2.insert();

    // 查询所有
    List<Test> tl = t2.selectAll();
    for (Test t : tl) {
        print("selectAll=" + t.toString());
    }

    // 查询总记录数
    print(" count=" + t2.selectCount(null));

    // 翻页查询
    Page<Test> page = new Page<>(0, 10);
    page = t2.selectPage(page, null);
    print(page.toString());

    // 根据ID删除
    rlt = t2.deleteById();
    print("deleteById=" + rlt + ", id=" + t2.getId());

    // 根据ID查询
    Test t20 = t2.selectById();
    print("t2 删除后是否存在?" + (null != t20));

    // 删除 SQL
    rlt = t2.delete("type={0}", "t1021");
    System.err.println("delete sql=" + rlt);
}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:66,代码来源:ActiveRecordTest.java

示例12: main

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public static void main(String[] args) {
    // 加载配置文件
    InputStream in = UserMapperTest.class.getClassLoader().getResourceAsStream("mysql-config.xml");
    MybatisSessionFactoryBuilder mf = new MybatisSessionFactoryBuilder();
    mf.setGlobalConfig(new GlobalConfiguration(new MySqlInjector()));
    SqlSessionFactory sessionFactory = mf.build(in);
    SqlSession session = sessionFactory.openSession();

    UserMapper userMapper = session.getMapper(UserMapper.class);
    RoleMapper roleMapper = session.getMapper(RoleMapper.class);

    /**
     * sjy 测试@TableField的el属性, 级联resultMap
     */
    Role role = new Role();
    role.setId(IdWorker.getId());
    role.setName("admin");
    int rlt = roleMapper.insert(role);
    System.err.println("--------- insert role --------- " + rlt);

    PhoneNumber phone = new PhoneNumber("81", "0576", "82453832");

    User userA = new User();
    userA.setId(IdWorker.getId());
    userA.setName("junyu_shi");
    userA.setAge(15);
    userA.setTestType(1);
    userA.setRole(role);
    userA.setPhone(phone);
    rlt = userMapper.insert(userA);
    System.err.println("--------- insert user --------- " + rlt);

    User whereUser = userMapper.selectOne(userA);
    System.err.println("--------- select user --------- " + whereUser.toString());

    // 如果不使用el表达式, User类中就同时需要roleId用于对应User表中的字段,
    // 和Role属性用于保存resultmap的级联查询. 在插入时, 就需要写user.setRoleId(), 然后updateUser.
    role = new Role();
    role.setId(IdWorker.getId());
    role.setName("root");
    roleMapper.insert(role);
    userA.setRole(role);
    userMapper.updateById(userA);
    System.err.println("--------- upadte user's role --------- " + rlt);

    whereUser = userMapper.selectOne(userA);
    System.err.println("--------- select user --------- " + whereUser.toString());

    userMapper.delete(new EntityWrapper<>(userA));
    System.err.println("--------- delete user --------- " + rlt);

}
 
开发者ID:Caratacus,项目名称:mybatis-plus-mini,代码行数:53,代码来源:URPTest.java

示例13: pushOrder

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
/**
 * 下单
 * @param mch_id 商户号
 * @param appId APPID
 * @param partnerKey 安全密钥
 * @param trade_type 交易类型(APP/MWEB)
 * @param out_trade_no 商户订单号
 * @param body 商品描述
 * @param detail 商品详细描述
 * @param amount 交易金额
 * @param scene_info 场景信息
 * @param ip 客户端IP
 * @param callBack 回调地址
 * @return 支付参数
 */
public static Map<String, String> pushOrder(String mch_id, String appId, String partnerKey, String trade_type,
    String out_trade_no, String body, String detail, BigDecimal amount, String scene_info, String ip,
    String callBack, String openId) {
    String total_fee = amount.multiply(new BigDecimal("100")).setScale(0).toString();
    Map<String, String> params = WxPayment.buildUnifiedOrderParasMap(appId, null, mch_id, null, null, body, detail,
        null, out_trade_no, total_fee, ip, callBack, trade_type, partnerKey, null, scene_info, openId);
    logger.info("WeChart order parameter : " + JSON.toJSONString(params));
    String result = WxPay.pushOrder(params);
    logger.info("WeChart order result : " + result);
    Map<String, String> resultMap = WxPayment.xmlToMap(result);
    String return_code = resultMap.get("return_code");
    if (WxPayment.codeIsOK(return_code)) {
        String result_code = resultMap.get("result_code");
        if (WxPayment.codeIsOK(result_code)) {
            String sign = resultMap.get("sign");
            String mySign = WxPayment.createSign(resultMap, partnerKey);
            if (mySign.equals(sign)) {
                String prepay_id = resultMap.get("prepay_id");
                String mweb_url = resultMap.get("mweb_url");
                resultMap.clear();
                resultMap.put("prepayid", prepay_id);
                if ("APP".equals(trade_type)) {
                    resultMap.put("partnerId", mch_id);
                }
                if (DataUtil.isNotEmpty(mweb_url)) {
                    resultMap.put("mwebUrl", mweb_url);
                } else {
                    resultMap.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
                    resultMap.put("noncestr", IdWorker.get32UUID());
                    sign = WxPayment.buildOrderPaySign(appId, mch_id, prepay_id, trade_type,
                        resultMap.get("timestamp"), resultMap.get("noncestr"), partnerKey);
                    resultMap.put("sign", sign);
                }
                return resultMap;
            } else {
                throw new RuntimeException("微信返回数据异常.");
            }
        } else {
            throw new RuntimeException(resultMap.get("err_code_des"));
        }
    } else {
        throw new RuntimeException(resultMap.get("return_msg"));
    }
}
 
开发者ID:iBase4J,项目名称:iBase4J-Common,代码行数:60,代码来源:WeChatUtil.java

示例14: test1

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
public void test1() throws Exception {
    // 毫秒内并发
    for (int i = 0; i < 1000; i++) {
        System.out.println(IdWorker.getId());
    }
}
 
开发者ID:baomidou,项目名称:mybatis-plus,代码行数:7,代码来源:IdWorkerTest.java

示例15: test

import com.baomidou.mybatisplus.toolkit.IdWorker; //导入依赖的package包/类
@org.junit.Test
public void test() {
    TableInfoHelper.initSqlSessionFactory(this.sqlSessionFactory());
    // 保存一条记录
    Test t1 = new Test();
    t1.setType("test10");
    boolean rlt = t1.insert();
    print(" ar save=" + rlt + ", id=" + t1.getId());

    // 根据ID更新
    t1.setType("t1001");
    rlt = t1.updateAllColumnById();
    print(" ar updateAllColumnById:" + rlt);

    t1.setType("t1023");
    rlt = t1.updateById();
    print(" ar updateById:" + rlt);

    // 更新 SQL
    Test t11 = new Test();
    t11.setType("123");
    rlt = t11.update("id={0}", t1.getId());
    print("update sql=" + rlt);

    // 查询 SQL
    Test t10 = t1.selectOne("id={0}", t1.getId());
    print("selectOne=" + t10.getType());

    // 插入OR更新
    t1.setType("t1021");
    rlt = t1.insertOrUpdate();
    print(" ar saveOrUpdate:" + rlt);

    // 根据ID查询
    Test t2 = t1.selectById();
    print(" t2 = " + t2.toString());
    t2.setId(IdWorker.getId());
    t2.insert();

    // 查询所有
    List<Test> tl = t2.selectAll();
    for (Test t : tl) {
        print("selectAll=" + t.toString());
    }

    // 查询总记录数
    print(" count=" + t2.selectCount(null));

    // 翻页查询
    Page<Test> page = new Page<>(0, 10);
    page = t2.selectPage(page, null);
    print(page.toString());

    // 根据ID删除
    rlt = t2.deleteById();
    print("deleteById=" + rlt + ", id=" + t2.getId());

    // 执行 SQL 查询总数
    List<Map<String, Object>> ul = t2.sql().selectList(new SQL() {
        {
            SELECT("*");
            FROM("test");
            WHERE("type='t1021'");
        }
    }.toString());
    System.err.println("selectList SQL:");
    for (Map<String, Object> map : ul) {
        System.err.println(map);
    }

    // 根据ID查询
    Test t20 = t2.selectById();
    print("t2 删除后是否存在?" + (null != t20));

    // 删除 SQL
    rlt = t2.delete("type={0}", "t1021");
    System.err.println("delete sql=" + rlt);
}
 
开发者ID:baomidou,项目名称:mybatis-plus,代码行数:79,代码来源:ActiveRecordTest.java


注:本文中的com.baomidou.mybatisplus.toolkit.IdWorker类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。