xUtils DbUtils 关于实体类注解 汇总
来源:互联网 发布:apache tomcat 区别 编辑:程序博客网 时间:2024/06/07 02:23
先来官方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 .主键
1
2
3
@Id
// 如果主键没有命名名为id或_id的时,需要为主键添加此注解
@NoAutoIncrement
// int,long类型的id默认自增,不想使用自增时添加此注解
private
int
id;
2. 忽略字段
1
2
3
4
5
// Transient使这个列被忽略,不存入数据库
@Transient
public
String willIgnore;
/** ---------------------------------*/
public
static
String staticFieldWillIgnore;
// 静态字段也不会存入数据库
3.表名
1
2
3
@Table
(name =
"parent"
, execAfterTableCreated =
"CREATE UNIQUE INDEX index_name ON parent(name,email)"
)
//name即表名,
//execAfterTableCreated 自定义表创建之后要执行的sql。为parent表创建(name,email)索引 -->在表上创建一个唯一的索引。唯一的索引意味着两个行不能拥有相同的索引值。
4.列名
1
2
@Column
(column =
"name"
)
//为列名加上注解 可以针对命名不统一和防止混淆
public
String name;
5.外键
延迟加载
1
2
3
4
5
@Finder
(valueColumn =
"id"
, targetColumn =
"parentId"
)
public
FinderLazyLoader<Child> children;
// 关联对象多时建议使用这种方式,延迟加载效率较高。
@Foreign
(column =
"parentId"
, foreign =
"id"
)
public
ForeignLazyLoader<Parent> parent;
非延迟加载
1
2
3
4
5
@Finder
(valueColumn =
"id"
,targetColumn =
"parentId"
)
public
Child children;
@Foreign
(column =
"parentId"
, foreign =
"isVIP"
)
public
List<Parent> parent;
1对多
1
2
3
4
5
@Finder
(valueColumn =
"id"
, targetColumn =
"parentId"
)
private
List<Child> children;
@Foreign
(column =
"parentId"
, foreign =
"id"
)
public
Parent parent;
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
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.其他
1
2
3
4
@NotNull
//不能为空
@Check
(value =
"age>0"
)
//age必须大于0 创建表时调用"
@Unique
//唯一
public
int
age;
1
<br>
0 0
- xUtils DbUtils 关于实体类注解 汇总
- xUtils系列之DbUtils-Id,NoAutoIncrement注解
- xUtils系列之DbUtils-Check注解
- xUtils系列之DbUtils-Column注解
- xUtils系列之DbUtils-Table注解
- xUtils系列之DbUtils-Transient注解
- xUtils系列之DbUtils-Unique注解
- xUtils系列之DbUtils-NotNull注解
- xUtils系列之DbUtils-Foreign注解
- xUtils系列之DbUtils-Finder注解
- Xutils框架中关于DbUtils的使用
- 关于XUtils的DBUtils使用遇到的错误
- xUtils框架—DbUtils
- xUtils中DbUtils用法
- xUtils DbUtils数据库
- xutils DbUtils框架
- XUtils中的dbUtils用法
- xUtils框架之DbUtils
- rtp与rtcp协议详解
- 元素的 height、clientHeight和offsetHeight之间的区别
- Beaglebone启动过程
- 爬山算法
- Matrix
- xUtils DbUtils 关于实体类注解 汇总
- requestWindowFeature各属性详解
- 软键盘的隐藏与显示
- em和px的区别一次彻底搞清楚!
- 同步/异步 阻塞/非阻塞
- Arale aspect切面编程源码解析
- Ext4文件系统架构分析(二)
- Head Pose Estimation Using AAM and POSIT
- ViewPager使用FragmentPagerAdapter卡顿问题解决