安卓开发之数据库orm---greendao

安卓初学者在前期编程时,想要连接数据库的好工具

程序员最不喜欢的就是废话!!废话不多说,直接搞配置!

1 在项目的根节点的build.gradle(app)中:(注意层级关系!)

buildscript { 
      repositories {
             mavenCentral()
      }
      dependencies {
              classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' 
       }
 }

 apply plugin: 'org.greenrobot.greendao'

 dependencies {
         compile 'org.greenrobot:greendao:3.2.0'
 }
 greendao {   
        schemaVersion 1//数据库版本号    
        daoPackage 'com.com.sky.downloader.greendao'//设置DaoMaster、DaoSession、Dao自动生成的位置    
        targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录   
        //targetGenDirTest:设置生成单元测试目录    
       //generateTests:设置自动生成单元测试用例
}

如下即为成功!
在这里插入图片描述
2. ☆创建一个实体类User在上述三个文件的***共同***目录下,copy如下代码:

@Entity(
        // Flag to make an entity "active": Active entities have update,
        // delete, and refresh methods.
        // 一般情况下不要这个标记
        active = false,
        // Specifies the name of the table in the database.
        // By default, the name is based on the entities class name.
        // 默认table为类名,这个默认可以不用,自己需要自定义名字时候添加
        nameInDb = "AWESOME_USERS",
        // Define indexes spanning multiple columns here.
        //定义索引,可以加快搜索,这里unique标记意思表示name值需要唯一,否则出错
//        indexes = {
//               @Index(value = "name DESC", unique = true)
//        },

        // Flag if the DAO should create the database table (default is true).
        // Set this to false, if you have multiple entities mapping to one table,
        // or the table creation is done outside of greenDAO.
        // 默认true,是需要的。
        createInDb = true,

        // Whether an all properties constructor should be generated.
        // A no-args constructor is always required.
        // 是否生成构造函数,所有的属性作为参数
        generateConstructors = true
)
public class User {
    // 自增的Id值 ,这里只能是long类型
    @Id(autoincrement = true)
    private Long id;
    // name在数据库里面的列表名字
    @Property(nameInDb = "Nam")
    private String name;
    //标注这个意思tmp不需要存储在数据库里面
    @Transient
    private int tmp;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Generated(hash = 873297011)
    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    @Generated(hash = 586692638)
    public User() {
    }

}

3.相关注解说明:

实体@Entity注解

schema:告知GreenDao当前实体属于哪个schema
active:标记一个实体处于活跃状态,活动实体有更新、删除和刷新方法
nameInDb:在数据库中使用的别名,默认使用的是实体的类名
indexes:定义索引,可以跨越多个列
createInDb:标记创建数据库表

基础属性注解

@Id:主键 Long 型,可以通过@Id(autoincrement = true)设置自增长
@Property:设置一个非默认关系映射所对应的列名,默认是使用字段名,例如:@Property(nameInDb = "name")
@NotNull:设置数据库表当前列不能为空
@Transient:添加此标记后不会生成数据库表的列

索引注解

@Index:使用@Index作为一个属性来创建一个索引,通过name设置索引别名,也可以通过unique给索引添加约束
@Unique:向数据库添加了一个唯一的约束

关系注解

@ToOne:定义与另一个实体(一个实体对象)的关系
@ToMany:定义与多个实体对象的关系

当我们编写好实体类并添加自己需要的注解之后,点击Make Project或者Make Module ‘app’,就会项目的build目录下或者自己设定的目录下看到生成的三个类文件:
DaoMaster
DaoSession
UserDao
后面的数据库操作需要借助这三个类来进行,同时在我们的实体类中自动生成了各个属性的get、set方法。

5.主函数中

public class MainActivity extends AppCompatActivity {


    private UserDao userdao;
    private List<User> users;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //拿到dao
        userdao = DaoMaster.newDevSession(this, "use.db").getUserDao();
        //构造数据
        User user = new User();
        user.setName("code_gg");
        //插入数据
        userdao.insert(user);
        //获取全部数据
        users = userdao.loadAll();

        //删除
        userdao.delete(user);

        // 插入或者替换
        userdao.insertOrReplace(user);

        // 用查询语句
        users=userdao.queryBuilder().where(UserDao.Properties.Name.eq("code_gg")).build().list();

         // 想使用rx,可以使用如下方式
        //userdao.rx().
    }
}
发布了47 篇原创文章 · 获赞 51 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_41976613/article/details/94844753