xUtils DbUtils 关于实体类注解 汇总

RockyZhang 发布于 1年前,共有 0 条评论

先来官方demo

DbUtils db = DbUtils.create(this);
     User user = new User(); //这里需要注意的是User对象必须有id属性,或者有通过@ID注解的属性
     user.setEmail("wyouflf@qq.com");
     user.setName("wyouflf");
     db.save(user); // 使用saveBindingId保存实体时会为实体的id赋值      ...
     // 查找
     Parent entity = db.findById(Parent.class, parent.getId());
     List<Parent> list = db.findAll(Parent.class);//通过类型查找      Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","=","test"));      // IS NULL
     Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","=", null));
     // IS NOT NULL
     Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","!=", null));      // WHERE id<54 AND (age>20 OR age<30) ORDER BY id LIMIT pageSize OFFSET pageOffset
     List<Parent> list = db.findAll(Selector.from(Parent.class)
                                        .where("id" ,"<", 54)
                                        .and(WhereBuilder.b("age", ">", 20).or("age", " < ", 30))
                                        .orderBy("id")
                                        .limit(pageSize)
                                        .offset(pageSize * pageIndex));      // op为"in"时,最后一个参数必须是数组或Iterable的实现类(例如List等)
     Parent test = db.findFirst(Selector.from(Parent.class).where("id", "in", new int[]{1, 2, 3}));
     // op为"between"时,最后一个参数必须是数组或Iterable的实现类(例如List等)
     Parent test = db.findFirst(Selector.from(Parent.class).where("id", "between", new String[]{"1", "5"}));      DbModel dbModel = db.findDbModelAll(Selector.from(Parent.class).select("name"));//select("name")只取出name列
     List<DbModel> dbModels = db.findDbModelAll(Selector.from(Parent.class).groupBy("name").select("name", "count(name)"));
     ...      List<DbModel> dbModels = db.findDbModelAll(sql); // 自定义sql查询
     db.execNonQuery(sql) // 执行自定义sql
     ...

注解总结

1 .主键

    @Id // 如果主键没有命名名为id或_id的时,需要为主键添加此注解
    @NoAutoIncrement // int,long类型的id默认自增,不想使用自增时添加此注解
    private int id;

2. 忽略字段

  // Transient使这个列被忽略,不存入数据库
    @Transient
    public String willIgnore;
    /** ---------------------------------*/
    public static String staticFieldWillIgnore; // 静态字段也不会存入数据库

3.表名

@Table(name = "parent", execAfterTableCreated = "CREATE UNIQUE INDEX index_name ON parent(name,email)") 
//name即表名, 
//execAfterTableCreated  自定义表创建之后要执行的sql。为parent表创建(name,email)索引 -->在表上创建一个唯一的索引。唯一的索引意味着两个行不能拥有相同的索引值。

4.列名

@Column(column = "name") //为列名加上注解 可以针对命名不统一和防止混淆
public String name;

5.外键

  延迟加载

    @Finder(valueColumn = "id", targetColumn = "parentId")
    public FinderLazyLoader<Child> children; // 关联对象多时建议使用这种方式,延迟加载效率较高。
    
    @Foreign(column = "parentId", foreign = "id")
    public ForeignLazyLoader<Parent> parent;

非延迟加载

    @Finder(valueColumn = "id",targetColumn = "parentId")
    public Child children;
    
    @Foreign(column = "parentId", foreign = "isVIP")
    public List<Parent> parent;

1对多

    @Finder(valueColumn = "id", targetColumn = "parentId")
    private List<Child> children;
    
    @Foreign(column = "parentId", foreign = "id")
    public Parent parent;

代码

        Parent parent = new Parent();
        parent.name = "测试" + System.currentTimeMillis();
        parent.setAdmin(true);
        parent.setEmail("wyouflf@gmail.com");
        
        DbUtils db = DbUtils.create(this.getActivity());
        db.configAllowTransaction(true);
        db.configDebug(true);         Child child = new Child();
        child.name = "child' name";
        child.parent = parent;
        db.saveBindingId(child);//保存对象关联数据库生成的id

6.其他

    @NotNull  //不能为空
    @Check(value = "age>0") //age必须大于0  创建表时调用"
    @Unique    //唯一
    public int age;

android Xutils dbutils 注解的更多相关文章

  1. 【转】Android 最火框架XUtils之注解机制详解

    原文:http://blog.csdn.net/rain_butterfly/article/details/37931031 在上一篇文章Android 最火的快速开发框架XUtils中简单介绍了x ...

  2. Android 最火框架XUtils之注解机制具体解释

    在上一篇文章Android 最火的高速开发框架XUtils中简介了xUtils的基本用法,这篇文章说一下xUtils里面的注解原理. 先来看一下xUtils里面demo的代码: @ViewInject ...

  3. Android Xutils 框架(转)

    Android Xutils 框架 (转) 目录(?)[-] xUtils简介 目前xUtils主要有四大模块 使用xUtils快速开发框架需要有以下权限 混淆时注意事项 DbUtils使用方法 Vi ...

  4. Android Xutils 框架

    XUtils是git上比较活跃 功能比较完善的一个框架,是基于afinal开发的,比afinal稳定性提高了不少,下面是介绍: 鉴于大家的热情,我又写了一篇Android 最火框架XUtils之注解机 ...

  5. Android中通过注解代替findViewById方法

    转自:http://www.2cto.com/kf/201405/302998.html 这篇文章主要讲解注解实现findViewById的功能,首先我们来熟悉一下在java中怎么定义一个注解和解析一 ...

  6. android xUtils get post

    使用android xUtils框架,进行http的get和post验证. 参考链接: https://github.com/wyouflf/xUtils3 http://blog.csdn.net/ ...

  7. 2.2、Android Studio通过注解提升代码检测

    使用像Lint这样的代码检测工具可以帮助你发现问题和提升代码,但是代码检测在有些地方很难应用.例如,Android的资源ID,使用一个int类型来表示字符.图像.颜色或者其他资源类型所以代码检测工具不 ...

  8. 利用APT实现Android编译时注解

    摘要: 一.APT概述 我们在前面的java注解详解一文中已经讲过,可以在运行时利用反射机制运行处理注解.其实,我们还可以在编译时处理注解,这就是不得不说官方为我们提供的注解处理工具APT (Anno ...

  9. 理解Android中的注解与反射

    反射 Java反射(Reflection)定义 Java反射机制是指在运行状态中 对于任意一个类,都能知道这个类的所有属性和方法:对于任何一个对象,都能够调用它的任何一个方法和属性: 这样动态获取新的 ...

随机推荐

  1. hdu1907(anti-sg入门)

    改变了下规则,现在变成了最后拿的人输. 如果对于单纯的nim的话,只需要判断每堆都是1个石子的特殊情况. 因为如果存在有大于1个石子的堆话,类似于nim的取法,处于必胜状态的一方只需要在 对方取完后只 ...

  2. Hello,Akka

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/dreamhead-logs/235916459.html 只要稍微了解过一些Scala, ...

  3. VB6 GDI+ 入门教程[4] 文字绘制

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[4] 文字绘制 2009 年 6 月 18 日 7条评论 ...

  4. mac app icon 设置

    mac app icon 设置 1:目前 mac app 所需要的icon 图标尺寸 icon_16x16.png 16px icon_16x16@2x.png 32px icon_32x32.png ...

  5. 30-Razor语法基础

    以往开发ASP.NET Web Form时,在ASPX页面上都会出现许多夹杂C#/VB.NET与HTML的情况,而先前使用<%...%>这种传统圆角括号的表示法会让HTML标签与ASP.N ...

  6. wordpress内存不足问题“Fatal error:out of memoryin etc...”

    2016年3月22日wordpress用户名及密码登录,出现: “ Fatal error: Out of memory (allocated 19136512) (tried to allocate ...

  7. Linux内核零碎知识

    UNIX系统:内核.shell外壳.文件系统.工具或应用程序. 操作系统功能:进程与处理机管理.存储管理.设备管理.作业管理.文件管理. 内存是磁盘的缓存,cache是内存的缓存. 可把内核看作是不断 ...

  8. Javascript的"预编译"思考

    今天工作需要,搜索下JS面试题,看到一个题目,大约是这样的 <script> var x = 1, y = z = 0; function add(n) { n = n+1;  } y = ...

  9. 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票

    121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...

  10. iOS开发 iOS10推送必看

    iOS10更新之后,推送也是做了一些小小的修改,下面我就给大家仔细说说.希望看完我的这篇文章,对大家有所帮助. 一.简单入门篇---看完就可以简单适配完了 相对简单的推送证书以及环境的问题,我就不在这 ...