安卓开源数据库ORM框架的使用
来源:互联网 发布:知乎ipad客户端没法装 编辑:程序博客网 时间:2024/05/16 18:36
前言
在我编写这个框架之前,我曾经深度反感对数据库操作的一系列重复无用功的前奏,只为向数据库输入一条SQL
语句和把返回值封装成一个JavaBean
对象。
自从我接触到了还在我在做一个菜鸟WEB
开发人员时候,MyBatis
是我接触到的最易入门上手使用的一个ORM
框架,在安卓端开发时候,
我发现市面上竟然没有一个对安卓端数据库Sqlite
一个很好地ORM框架
,于是我拜读了mybatis源码
,然后通过自己的理解,
自己编写一个安卓端的MyBatis
XAndrDB
安卓端开源数据库ORM操作框架。
使用说明:
在使用本框架之前首先
将本项目根目录下的本项目引入到你的项目中,然后。
在你的app的AndroidManifest.xml中配置:
<application android:name="com.xcode.xandrdb.Session.SessionApplication" …… >
一、新建数据库
package com.xcode.xandrdb.mapper;import com.xcode.xandrdb.annotation.Create;public interface test{ @Create("CREATE TABLE 'ChatItem' ('content' TEXT NOT NULL,'fromuser' TEXT,'type' TEXT,'touser' TEXT,'time' TEXT,'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)") public void createOnChat2();}
二、或者你这样新建数据库
1.新建一个数据库表的类
import com.xcode.xandrdb.annotation.Table;public class OnChat{ @Table.AUTOINCREMENT @Table.NOT_NULL @Table.PRIMARY_KEY private int id; private String User; private String lastmsg; private String time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser() { return User; } public void setUser(String user) { User = user; } public String getLastmsg() { return lastmsg; } public void setLastmsg(String lastmsg) { this.lastmsg = lastmsg; } public String getTime() { return time; } public void setTime(String time) { this.time = time; }}
2.新建数据库
package com.xcode.xandrdb.mapper;import com.xcode.xandrdb.annotation.Create;public interface test{ @Create.Auto(OnChat.class) public void createOnChat();}
这样的第二种建立方式很明显方便很多。
三、查询操作
优点就在于,你不用关心怎么把查询出来的数据封装成JavaBean对象,你只需要写好返回值类型。
package com.xcode.xandrdb.mapper;import com.xcode.xandrdb.User;import com.xcode.xandrdb.annotation.Create;import com.xcode.xandrdb.annotation.Delete;import com.xcode.xandrdb.annotation.Insert;import com.xcode.xandrdb.annotation.Select;import com.xcode.xandrdb.annotation.Update;import com.xcode.xandrdb.annotation.name;public interface test{ //查询操作 @Select("SELECT * FROM xiaolei WHERE _id = #{id}") public User[] select(@Param("id")int id);}
四、新增操作
package com.xcode.xandrdb.mapper;import com.xcode.xandrdb.User;import com.xcode.xandrdb.annotation.Create;import com.xcode.xandrdb.annotation.Delete;import com.xcode.xandrdb.annotation.Insert;import com.xcode.xandrdb.annotation.Select;import com.xcode.xandrdb.annotation.Update;import com.xcode.xandrdb.annotation.name;public interface test{ //插入的操作 @Insert("INSERT INTO xiaolei ('sname', 'snumber') VALUES ('xiaolei', 'xiaolei')") public String insert();}
五、删除操作
package com.xcode.xandrdb.mapper;import com.xcode.xandrdb.User;import com.xcode.xandrdb.annotation.Create;import com.xcode.xandrdb.annotation.Delete;import com.xcode.xandrdb.annotation.Insert;import com.xcode.xandrdb.annotation.Select;import com.xcode.xandrdb.annotation.Update;import com.xcode.xandrdb.annotation.name;public interface test{ //删除操作 @Delete("delete from xiaolei where id = #{id}") public String delete(@Param("id")int id);}
六、更新数据操作
package com.xcode.xandrdb.mapper;import com.xcode.xandrdb.User;import com.xcode.xandrdb.annotation.Create;import com.xcode.xandrdb.annotation.Delete;import com.xcode.xandrdb.annotation.Insert;import com.xcode.xandrdb.annotation.Select;import com.xcode.xandrdb.annotation.Update;import com.xcode.xandrdb.annotation.name;public interface test{ //更新操作 @Update("UPDATE xiaolei SET sname='xiaolei2', snumber='xiaolei2' WHERE (_id=#{id})") public String update(@Param("id")int id);}
这里只是定义了一系列的数据库操作action的Mapper。那我们看看怎么使用这些Mapper:
Session session = SessionFactory.getSession(new SessionConfig().setDBName("xiaolei"));test t1 = session.getMapper(test.class);User users[] = t1.select(1);System.out.println(users);
支持数据缓存
有时候,我需要在两秒内对查询到的数据进行缓存,不需要每次要数据就去数据库中查询。那么这个特性可以帮助你。
public interface test{ //查询操作 @Catch(500) @Select("SELECT * FROM xiaolei where sname=#{name}") public List<User> select(@Param("name")String name);}
这个@Catch(500)注解 参数是你自己设置的缓存的时间。不写参数默认为 2 * 1000 ms。
支持事务
有时候我需要批量向数据库中插入大量的数据,可是大量数据的写入会导致整个APP卡顿。那么为了解决这个问题 你可以这样:
public interface test{ @Transation @Insert("INSERT INTO xiaolei ('sname', 'snumber') VALUES ('xiaolei', 'xiaolei')") public String insert();}
当你需要进行大量数据的操作的时候,你可以使用 @Transation
注解来解决这个问题。
经测试 在模拟器中循环插入五十条数据,没有开启 Transation 的时候 耗时:220ms
开启了Transation的时候,耗时:7ms。并且内部使用Handler进行异步操作。绝对不会影响到APP的流畅性。
当然在你操作完成之后 别忘了调用 session.commit();
。否则是不会生效的。
是的,所有的数据库操作都必须使用Session
对象,去操作。
然后使用Session
对象拿到mapper,内部通过动态代理操作返回你一个mapper对象,然后你操作你的mapper里面定义的方法就是在操作数据库了。
这是一个完全面向切面,使用 自定义注解+反射+动态代理 结合在一起发挥强大功能的数据库框架。
GIT地址:http://git.oschina.net/xcode_xiao/XAndrDB
更多新特性正在添加中…
- 安卓开源数据库ORM框架的使用
- ORM数据库框架使用
- 使用ORM数据库框架greenDao
- ORM数据库简单的框架和黄油刀的使用
- android ORM数据库框架ActiveAndroid的简单使用
- 应用数据库管理ORM框架---GreenDao的使用
- Django ORM框架的使用
- android高效ORM数据库框架greenDao使用
- Android ORM 数据库的使用
- Android ORM 数据库的使用
- Android ORM数据库的使用
- Python的数据库ORM框架:SQLAlchemy
- Python的数据库ORM框架:SQLAlchemy
- Android 的 数据库 ORM框架 DBExecutor
- Python的数据库ORM框架:SQLAlchemy
- Python的数据库ORM框架:SQLAlchemy
- Android 数据库ORM框架
- Android-数据库-ORM框架
- http和https 区别和用法
- vim模板插件vim-template的使用
- git 创建,删除 branch 和 tag
- Spring MVC表单标签
- 机器学习面试材料
- 安卓开源数据库ORM框架的使用
- OpenGL中的坐标系和变换
- 看韦东山视频移植u-boot到mini2440支持烧写yaffs
- UIStackView 对UIView视图管理
- Caffe Solver
- 基于Deep Learning 的视频识别技术
- Android使用HttpClient时出现错误:java.lang.NoSuchFieldError: No static field INSTANCE
- Fiddler抓取HttpClient数据抓取不到的问题
- C#动态库调用方法