Quantcast
Channel: OSCHINA 社区最新新闻
Viewing all articles
Browse latest Browse all 44787

FastQuery v1.0.8 发布,数据持久层框架

$
0
0

FastQuery v1.0.8 发布了,FastQuery基于Java语言.他的使命是:简化Java操作数据层.
做为一个开发者, 仅仅只需要设计DAO接口即可,其内部采用ASM动态生成实现,执行快. 因此,代码简洁而优雅.从而,大幅度提升开发效率.
遵循非侵入式原则设计,松耦合,很容易与其它容器或框架集成.
提供了一组简单的Annotation.消费者只用关心注解的含义.这就使得框架的核心便于重构,便于持续良性发展.
截至发布1.0版,发现做了如下改进:
1.0.1 增强拦截器作用范围合法校验.
1.0.2 支持批量update,支持事务
1.0.3 新增自定义类加载器
1.0.4 #{#table} , #{#id} 合法使用检测
1.0.5 Repository异常处理
1.0.6 新增分页功能
1.0.7 增强fastquery.json

1.0.8 控制分页区间的位置,数据源优先级,新增@NotCount

部分例子

// 将三条改操作纳入到一个事务中.
@Transactional
@Modifying
@Query("update `userinfo` set `name`=?1 where id=?3")
@Query("update `userinfo` set `age`=?2 where id=?3")
// 把主键id修改为1,目前主键id=1是存在的.这行会报错.那么前两行所做的操作全部失效.
@Query("update `userinfo` set `id`=1 where `id`=?3")
int updateBatch(String name,Integer age,Integer id);
// 注意: 
// 1).返回值如果是int类型,表示这个事务成功提交后所有改操作所影响的行数总和.
// 2).返回值如果是int[]类型,表示这个事务成功提交后,每个最小修改单元所影响行数的集合.
//    举例说明: 若有个事务T,它里面有3条改操作,分别叫U1,U2,U3. 
//    T成功提交后,U1,U2,U3所影响的数据行数分别为N1,N2,N3.
//    则: 返回值为: new int[]{N1,N2,N3}

分页

public interface UserInfoDBService extends QueryRepository {
    // countField : 明确指定求和字段count(countField),默认值是"id"
    @Query(value="select id,name,age from `userinfo` where 1",countField="id")
    Page<Map<String, Object>> findAll(Pageable pageable);

    // 如果没有指定求和语句,那么由fastquery分析出最优的求和语句
    @Query("select id,name,age from `userinfo` #{#where}")
    @Condition(l="age",o=Operator.GT,r="?1")                // age > ?1
    @Condition(c=COperator.AND,l="id",o=Operator.LT,r="?2") // id < ?2
    Page<UserInfo> find(Integer age,Integer id,Pageable pageable);

    // countQuery : 指定自定义求和语句
    @Query(value = "select id,name,age from `userinfo` #{#where}", 
           countQuery = "select count(id) from `userinfo` #{#where}")
    @Condition(l = "age", o = Operator.GT, r = "?1")        // age > ?1
    @Condition(c=COperator.AND,l="id",o=Operator.LT,r="?2") // id < ?2
    Page<UserInfo> findSome(Integer age,Integer id,Pageable pageable);
}

// Page是分页的抽象.通过它可以获取分页中的各种属性. 并且开发者不用去实现.

Integer age = 10;
Integer id = 50;
int p = 1;    // 指定访问的是第几页
int size = 3; // 设定每一页最多显示几条记录
Pageable pageable = new PageableImpl(p, size);
Page<UserInfo> page  = userInfoDBService.findSome(10, 50,pageable);
List<UserInfo> userInfos = page.getContent(); // 获取这页的数据
Slice slice = page.getNextPageable();         // 下一页
int number = page.getNumber();                // 当前页数(当前是第几页)
// 更多 page.? 就不赘述了.

她转换成JSON后的结构如下:

{
    "content":[                  // 这页的数据
        {
            "name":"查尔斯·巴贝奇","id":2,"year":1792
        },
        {
            "name":"约翰·冯·诺依曼","id":3,"year":1903
        },
        {                     
            "name":"阿兰·麦席森·图灵","id":1,"year":1912
        },
        {
            "name":"约翰·麦卡锡","id":4,"year":1927
        },
        {
            "name":"丹尼斯·里奇","id":5,"year":1941
        },
        {
            "name":"蒂姆·伯纳斯·李","id":6,"year":1955
        }
    ],
    "first": true,              // 是否是第一页
    "hasContent": true,         // 这页是否有数据
    "hasNext": true,            // 是否有下一页
    "hasPrevious": false,       // 是否有上一页
    "last": false,              // 是否是最后一页
    "nextPageable": {           // 下一页的基本属性
        "number": 1,            // 定位的页码
        "size": 15              // 每页多少条数据
    },
    "number": 1,                // 当前页码,从1开始
    "numberOfElements": 6,      // 当前页的真实记录行数
    "previousPageable": {       // 上一页的基本属性
        "number": 0,            // 定位的页码
        "size": 15              // 每页多少条数据
    },
    "size": 15,                 // 每页行数
    "totalElements": 188,       // 总行数
    "totalPages": 13            // 总页码
}

如果在分页函数上标识@NotCount,表示在分页中不统计总行数.
那么分页对象中的totalElements的值为-1L,totalPages为-1.其他属性都有效且真实.
如果明确指定不统计行数,那么设置countField和nativeQuery就会变得无意义.
通常分页的区间控制默认放在SQL语句的末尾. 
在符合SQL语法的前提下,通过#{#limit}可以把分页区间放在SQL里的任何地方.

详情: http://git.oschina.net/xixifeng.com/fastquery


Viewing all articles
Browse latest Browse all 44787

Trending Articles