Search in sources :

Example 1 with StringGenerator

use of org.nutz.lang.random.StringGenerator in project nutz by nutzam.

the class DaoUpTest method test_pojo.

/**
     * 2. 带Pojo的基本操作,单表无操作
     */
@Test
public void test_pojo() {
    // 首先,得到Dao实例, 同理, 已经初始化好了的
    // 因为
    Dao dao = DaoUp.me().dao();
    // 关于SimplePojo, 请务必打开看一眼哦
    // 这个类同时标注了@Id和@Name,自动建表时以@Id作为主键,@Name作为唯一键
    // 强制建表
    // 真实代码可别写true,被删表了别找我!!!
    dao.create(SimplePojo.class, true);
    // 先生成个随机字符串帮助实例, R是nutz内部的随机数相关的帮助类,有UUID, UU32, UU64等很多东西哦
    // 代表一个字符串长度为10的随机字符串生成器
    StringGenerator sg = R.sg(10);
    // 插入几条记录
    for (int i = 0; i < 100; i++) {
        // 如果用的是Oracle数据库死在这里了,请加上druid,使其用上连接池.
        dao.insert(new SimplePojo(sg.next(), "http://www." + sg.next() + ".cn", R.random(10, 100)));
    }
    // 统计一下,应该是100条
    assertEquals(100, dao.count(SimplePojo.class));
    // 批量插入1000条吧
    List<SimplePojo> list = new ArrayList<SimplePojo>();
    for (int i = 0; i < 100; i++) {
        list.add(new SimplePojo(sg.next(), "http://www." + sg.next() + ".cn", R.random(10, 100)));
    }
    // 注意, list里面的对象必须是同一个类型哦
    dao.fastInsert(list);
    // fastInsert的特点是快,但不会执行@Prev和@Next, 也不会取回生成的主键哦,有得有失嘛
    // 看看大于45岁的有多少人,虽然理论上是一半一半,事实上经常不是这样...
    int re = dao.count(SimplePojo.class, Cnd.where("age", ">", 45));
    log.infof("older than 45y : %d", re);
    // 分页查询,跟无Pojo时的操作差不多
    List<SimplePojo> pojos = dao.query(SimplePojo.class, Cnd.where("age", ">", 45), dao.createPager(2, 10));
    // 肯定小于等于10
    log.infof("size=%d", pojos.size());
    // 更新操作
    // 不加参数的话,就是取出第一条记录了
    SimplePojo pojo = dao.fetch(SimplePojo.class);
    // 肯定不为null啦
    assertNotNull(pojo);
    // nutzbook是一本很值得看的nutz书哦
    pojo.setWebsite("http://nutzbook.wendal.net");
    // 只需要更新website, 如果全部属性都更新就 dao.update(pojo, null)
    dao.update(pojo, "website");
    // 检查一下是不是真的变成nutzbook的网址了
    assertEquals("http://nutzbook.wendal.net", dao.fetch(SimplePojo.class, pojo.getId()).getWebsite());
    // 现在, 随便删掉一条记录
    // 传入的是数值,所以SimplePojo必须有个@Id的属性
    dao.delete(SimplePojo.class, 20);
    // 肯定删了,或者根本没有,呵呵
    assertNull(dao.fetch(SimplePojo.class, 20));
    // 再根据name删掉一条
    // 传入的是字符串,所以SimplePojo必须有个@Name
    dao.delete(SimplePojo.class, pojo.getName());
    assertNull(dao.fetch(SimplePojo.class, pojo.getName()));
    // delete方法时删除单条记录,而批量呢? clear方法
    // 开始大开杀戒
    // 删掉所有id小于20的记录,哈哈
    dao.clear(SimplePojo.class, Cnd.where("id", "<", 20));
    // 统计一下20以内的记录,应该是0哦
    assertEquals(0, dao.count(SimplePojo.class, Cnd.where("id", "<", 20)));
    // 现在,让id大于50的记录的website通通变成nutzbook,哈哈哈
    int count = dao.update(SimplePojo.class, Chain.make("website", "http://nutzbook.wendal.net").add("createTime", new Date()), Cnd.where("id", ">", 50));
    assertEquals(count, dao.count(SimplePojo.class, Cnd.where("id", ">", 50).and("website", "=", "http://nutzbook.wendal.net")));
    // 请留意一下Cnd和Chain的链式写法
    // 按某人的建议, 查询一下id大于30或者website为nutzbook且name包含a的记录
    // 翻译成伪sql就是  where id > 300 or (website like "%nutzbook%" and name like "%a%")
    int size = dao.count(SimplePojo.class, Cnd.where("id", ">", 300).or(Cnd.exps("website", "like", "%nutzbook%").and("name", "like", "%a%")));
    assertTrue(size > 0);
    // sql 输出类似于
    // SELECT COUNT(*) FROM t_test_simple_pojo  WHERE id>300 OR (website LIKE '%nutzbook%' AND name LIKE '%a%')
    // 关于日志中的sql, 特别说明一下, nutz真正执行的sql是
    /**
         * SELECT COUNT(*) FROM t_test_simple_pojo  WHERE id>? OR (website LIKE ? AND name LIKE ?)
         */
    // 即PreparedStatement, 参数都是以安全的方式传输.
    // 而日志中带上参数的以"For example"提示的sql是用于显示的,并非真正执行的sql
    // 好了, happy完了,全杀
    dao.clear(SimplePojo.class, null);
}
Also used : Dao(org.nutz.dao.Dao) StringGenerator(org.nutz.lang.random.StringGenerator) ArrayList(java.util.ArrayList) SimplePojo(org.nutz.dao.util.meta.SimplePojo) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Test (org.junit.Test)1 Dao (org.nutz.dao.Dao)1 SimplePojo (org.nutz.dao.util.meta.SimplePojo)1 StringGenerator (org.nutz.lang.random.StringGenerator)1