Android中使用Ormlite实现持久化(二)--持久化类的详细配置
openkk
13年前
<p><a href="http://www.open-open.com/lib/view/open1325323603780.html" target="_blank">上一篇</a>文章简单的使用了Ormlite一下,但是我觉得Ormlite出色之处就是对于需要字段,表等等的配置设计的比较好。</p> <p>下面来说说类的配置:</p> <p>如果我们开发的Android应用需要保持用户信息,那么现在新建一个类:UserAccount</p> <p>该类有六个变量:</p> <pre class="brush:java; toolbar: true; auto-links: false;"> private int id; private String username; private String password; private Date regTime; private String tellphone; private String email;</pre>用eclipse生成get和set方法: <pre class="brush:java; toolbar: true; auto-links: false;">public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getRegTime() { return regTime; } public void setRegTime(Date regTime) { this.regTime = regTime; } public String getTellphone() { return tellphone; } public void setTellphone(String tellphone) { this.tellphone = tellphone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }</pre> <p></p> <p>下面通过注解的方式配置该类的持久化参数:</p> <p>1.表名:</p> <div class="cnblogs_code"> <pre>@DatabaseTable(tableName="dataTableName")</pre> </div> <p>不指定的话表名就是类名。</p> <p>2.字段</p> <div class="cnblogs_code"> <pre>@DatabaseField</pre> </div> <p>这个可以配置的属性有点多。</p> <p>2.1 主键</p> <div class="cnblogs_Highlighter"> <pre class="brush:java;gutter:false;">@DatabaseField(id=true)</pre> </div> <p>2.2 列名</p> <div class="cnblogs_Highlighter"> <pre class="brush:java;gutter:false;">@DatabaseField(columnName="columnName")</pre> </div> <p>不指定的话就是和变量名一样的</p> <p>2.3 数据类型</p> <div class="cnblogs_Highlighter"> <pre class="brush:java;gutter:false;">@DatabaseField(dataType=DataType.INTEGER)</pre> </div> <p>这个一般情况下都不用指定,可以根据java 类获得</p> <p>2.4 默认值</p> <div class="cnblogs_Highlighter"> <pre class="brush:java;gutter:false;">@DatabaseField(defaultValue="http://www.www.open-open.com/")</pre> </div> <p>2.5 长度</p> <div class="cnblogs_Highlighter"> <pre class="brush:java;gutter:false;">@DatabaseField(width=13)</pre> </div> <p>一般用于String型</p> <p>2.6 能否为空</p> <div class="cnblogs_Highlighter"> <pre class="brush:java;gutter:false;">@DatabaseField(canBeNull=false)</pre> </div> <p>默认为True</p> <p>2.7 是否自增长</p> <div class="cnblogs_Highlighter"> <pre class="brush:java;gutter:false;">@DatabaseField(generatedId=true)</pre> </div> <p></p>