ORMLite 数据库的使用--二次封装

Posted 童政通

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ORMLite 数据库的使用--二次封装相关的知识,希望对你有一定的参考价值。

ORMLite 数据库封装的使用

最近在做一个市政抄表机,录入用户水汽表数据到DB里面的,这个封装是同事封装的,封装的很好,文档也很详细,所以计入一下.方便下次使用.

推荐使用GreenDao3.2:https://mp.weixin.qq.com/s?__biz=MzAxMTI4MTkwNQ==&mid=2650821932&idx=1&sn=d26c09af7cbbfb1b0a95517bd78cc784&chksm=80b781b2b7c008a4a8dab45756e4d433b1c56e1c61762f98ee3b8a2b89a00756f82d6bb4d6b6&scene=0#rd

使用步骤:

##ORM使用



基础工作:
    1、下载 ORMLite Jar
    首先去ORMLite官网下载jar包,对于android为:ormlite-android-XXX.jar 和 ormlite-core-XXX.jar2、配置Bean类
    有了jar,我们在datamodule下把jar拷贝到libs下。然后新建一个包:com.sqlhelp专门用于存放项目中的Bean,首先新建一个Been.Java
    Been.java类作为所有数据结构的基类,涉及数据库相关的数据结构全部继承于此类

    首先在Been类上添加@DatabaseTable(tableName = "tb_been"),标明这是数据库中的一张表,表名为tb_been
    然后分别在属性上添加@DatabaseField(columnName = "name") ,columnName的值为该字段在数据中的列名
    @DatabaseField(generatedId = true) ,generatedId 表示id为主键且自动生成

    3、SQLiteOpenHelper
    原生的数据库操作,需要继承SQLiteOpenHelper,这里我们需要继承OrmLiteSqliteOpenHelper
    然后需要实现两个方法:

    onCreate(SQLiteDatabase database,ConnectionSource connectionSource)
    创建表,我们直接使用ormlite提供的TableUtils.createTable(connectionSource, User.class);进行创建~

    onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion)
    更新表,使用ormlite提供的TableUtils.dropTable(connectionSource, User.class, true);进行删除操作~
    删除完成后,别忘了,创建操作:onCreate(database, connectionSource);

    4、初始化:DatabaseHelper.getHelper(getApplicationContext());

    5、如果DatabaseHelper.getHelper(getApplicationContext())返回值为NULL,则表示数据库DB文件不存在
       或者路径配置不正确。正确的DB路径:内置SD卡下ncproject文件夹下的data文件夹readmachine.db

    6、如果DatabaseHelper.getHelper(getApplicationContext())返回值不为NULL,则表示初始化成功

假设你需要查找某张表,需要你自己去组织.详细的看如下:

第一步:以下三个方法每次执行数据库操作必须先初始化


    /**
     * 变量初始化
     * @param clazz 对应的been的.class文件(你需要查找的那张表你自己所映射出来的bean文件)
     */
    public Been(Context context,Class clazz)

    /**
     * 执行Dao变量的初始化方法
     */
    public Dao getDao()

    /**
     * 执行QueryBuilder变量的初始化方法
     */
    public void getQueryBuilder()

第二步:以下为SQL查询条件的一些设置(可选,多选,等等)

  /**
     * 设置是否排序
     * @param Coum    排序依据列
     * @param is_up_or_down  ture是升序,false降序
     */
    public void Set_Order_By (String Coum,boolean is_up_or_down)

    /**
     * 设置是否去重复
     */
    public void Set_Desc ()

    /**
     * 返回总条数
     * @return
     */
    public long getAllNums()

    /**
     *执行分页
     * @param limt 限制获取指定行数
     * @param offset 跳过指定的行数
     */
    public void setPage(long limt,long offset)

第三步:以上为SQL组成where语句查询条件(可选,多选,等等)


    /**
     *SQL where语句初始化
     */
    public Where<Object, Integer> getWhere()



    /**
     * 对应SQL:SELECT * FROM `xxxx` WHERE `id` = 2
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *             value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetBe_Equal_To(Map<String ,Object> map)



    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` < 2
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *             value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetLess_Than(Map<String ,Object> map)



    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` > 2
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *             value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetMore_Than(Map<String ,Object> map)


    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` >= 2
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *             value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetMore_Than_Or_Equal_To(Map<String ,Object> map)



    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` <= 2
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *             value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetLess_Than_Or_Equal_To(Map<String ,Object> map)




    /**
     * SELECT * FROM `t_person` WHERE `id` <> 2
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *             value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetNot_Equal_To(Map<String ,Object> map)



    /**
     *SELECT * FROM `t_person` WHERE `LastName` LIKE 'A%'
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *             value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetLike_To(Map<String ,Object> map)



    /**
     *获取指定范围内的结果
     * @param Colum  列名
     * @param low     范围开始
     * @param height  范围结束
     * @return
     */
    public Where<Object, Integer> SetBetween_To(String Colum,Object low,Object height)

第四步:查找用户

   /**
     *查找用户
     */
    public List<Object> Search()

其他


    /**
     * 增加一个记录
     */
     getDao().create(Been);


     /**
      * 删除一条记录
      */
    getDao().deleteById(id);

    /**
     * 更新一条记录
     */
     getDao().update(Been);

给个简单的使用案例

现在我对水汽表进行数据的查询

if (mDataList == null || mDataList.size() == 0) 
            Map<String, Object> map = new HashMap<>();
            //gibfid    气表查询字段     mibfid 水表查询字段
           //base_user_info_w  db文件水表所映射的bean文件
                //初始化3步骤 bean  dao  getQueryBuilder(必须做)
                Been been = new Been(mContext, base_user_info_w.class);
                been.getDao();
                been.getQueryBuilder();

                //设置是否排序    更具表中micbh 字段排序
                been.Set_Order_By("micbh", true);(可略)
                //一次取800条(不足的情况返回最大的数值)
                been.setPage(800, 0);   (可以不分页更具需求更爱)
                //初始化  where    (必须做)
                been.getWhere();
                //map  存查询条件     (必须做)
                map.put("mibfid", mContext);
                been.SetBe_Equal_To(map, "And");  (必须做)
                //开始搜索         (必须做)
                mDataList = been.Search();
             else 
                Been been = new Been(mContext, base_user_info_g.class);
                been.getDao();
                been.getQueryBuilder();
                been.Set_Order_By("gicbh", true);
                been.setPage(800, 0);
                been.getWhere();
                map.put("gibfid", mValue);
                been.SetBe_Equal_To(map, "And");
                mDataList = been.Search();
            
        

下面贴出用的的java类

Been

package com.sqlhelp;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/16 0016.
 */
public class Been 

    private QueryBuilder queryBuilder;
    Context context;
    private String Coum;
    private Class clazz;
    private Dao dao;
    private DatabaseHelper databaseHelper;

    public Been(Context context, Class clazz) 
        this.clazz = clazz;
        this.context = context;
    

    public DatabaseHelper InitDatabaseHelper() 
        if (databaseHelper == null) 
            databaseHelper = DatabaseHelper.getHelper(context);
        
        return databaseHelper;
    

    public Dao getDao() 

        if (dao == null) 
            try 
                this.dao = InitDatabaseHelper().getDao(clazz);
             catch (SQLException e) 
                e.printStackTrace();
            
        

        return dao;
    


    public QueryBuilder getQueryBuilder() 

        queryBuilder = getDao().queryBuilder();
        return queryBuilder;
    

    /**
     * 设置是否排序
     *
     * @param Coum          排序依据列
     * @param is_up_or_down ture是升序,false降序
     */
    public void Set_Order_By(String Coum, boolean is_up_or_down) 
        if (Coum != null) 
            this.Coum = Coum;
            queryBuilder.orderBy(Coum, is_up_or_down);
        
    

    /**
     * 设置是否去重复
     */
    public void Set_distinct(String distinct) 
        queryBuilder.selectColumns(distinct).distinct();
    

    public void Set_Desc(List<String> list) 
        for (int i = 0; i < list.size(); i++) 
            queryBuilder.selectColumns(list.get(i));
        
        queryBuilder.distinct();

    

    /**
     * 返回总条数
     *
     * @return
     */
    public long getAllNums() 
        long nums = 0;
        try 
            nums = queryBuilder.countOf();
         catch (SQLException e) 
            e.printStackTrace();
        
        return nums;
    

    /**
     * @param limt   限制获取指定行数
     * @param offset 跳过指定的行数
     */
    public void setPage(long limt, long offset) 
        if (limt > 0) 
            queryBuilder.limit(limt);
        
        if (offset > 0) 
            try 
                queryBuilder.offset(offset);
             catch (SQLException e) 
                e.printStackTrace();
            
        

    

    Where<Object, Integer> where;

    /**
     * 初始化Where,但是如果查询全部数据,不执行此步骤,直接执行Search()
     *
     * @return
     */
    public Where<Object, Integer> getWhere() 
        if (where == null) 
            where = queryBuilder.where();
//            try 
//                where.ge(Coum,0);
//             catch (SQLException e) 
//                e.printStackTrace();
//            
        
        return where;
    


    public Where<Object, Integer> setAnd_Or(String Type) 
        if (Type.equals("And")) 
            where.and();
         else if (Type.equals("Or")) 
            where.or();
        
        return where;
    


    /**
     * 对应SQL:SELECT * FROM `xxxx` WHERE `id` = 2
     *
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *            value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetBe_Equal_To(Map<String, Object> map, String Type) 

        int i = 0;
        try 
            for (String key : map.keySet()) 
                if (i > 0) 
                    setAnd_Or(Type);
                
                i++;
                where.eq(key, map.get(key));

            
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    

    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` < 2
     *
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *            value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetLess_Than(Map<String, Object> map, String Type) 
        int i = 0;
        try 
            for (String key : map.keySet()) 
                if (i > 0) 
                    setAnd_Or(Type);
                
                i++;
                where.lt(key, map.get(key));
            
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    

    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` > 2
     *
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *            value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetMore_Than(Map<String, Object> map, String Type) 
        int i = 0;
        try 
            for (String key : map.keySet()) 
                if (i > 0) 
                    setAnd_Or(Type);
                
                i++;
                where.gt(key, map.get(key));
            
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    

    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` >= 2
     *
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *            value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetMore_Than_Or_Equal_To(Map<String, Object> map, String Type) 
        int i = 0;
        try 
            for (String key : map.keySet()) 
                if (i > 0) 
                    setAnd_Or(Type);
                
                i++;
                where.ge(key, map.get(key));
            
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    

    /**
     * 对应SQL:SELECT * FROM `t_person` WHERE `id` <= 2
     *
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *            value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetLess_Than_Or_Equal_To(Map<String, Object> map, String Type) 
        int i = 0;
        try 
            for (String key : map.keySet()) 
                if (i > 0) 
                    setAnd_Or(Type);
                
                i++;
                where.le(key, map.get(key));
            
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    

    /**
     * SELECT * FROM `t_person` WHERE `id` <> 2
     *
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *            value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetNot_Equal_To(Map<String, Object> map, String Type) 
        int i = 0;
        try 
            for (String key : map.keySet()) 
                if (i > 0) 
                    setAnd_Or(Type);
                
                i++;
                where.ne(key, map.get(key));
            
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    

    /**
     * SELECT * FROM `t_person` WHERE `LastName` LIKE 'A%'
     *
     * @param map 里面存放的键值对key值必须和对应的Been里面的成员变量名保持一致,即要操作ciid,对应的KEY为"ciid"
     *            value值则余对应的变量类型保持一致
     * @return
     */
    public Where<Object, Integer> SetLike_To(Map<String, Object> map, String Type) 
        int i = 0;
        try 
            for (String key : map.keySet()) 
                if (i > 0) 
                    setAnd_Or(Type);
                
                i++;
                where.like(key, map.get(key) + "%");
            
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    


    /**
     * 获取指定范围内的结果
     *
     * @param Colum  列名
     * @param low    范围开始
     * @param height 范围结束
     * @return
     */
    public Where<Object, Integer> SetBetween_To(String Colum, Object low, Object height) 

        try 
            where.between(Colum, low, height);
         catch (SQLException e) 
            e.printStackTrace();
        
        return where;
    

    /**
     * 查找用户
     */
    public List<Object> Search() 

        List<Object> getlist = new ArrayList<>();
        try 
            if (where == null) 
                getlist = queryBuilder.query();
             else 
                getlist = where.query();
            
//            Log.e("Object","总列表长度:"+getlist.size());
//            Log.e("Object","总列表长度:"+getlist.toString());

         catch (SQLException e) 
            e.printStackTrace();
        

        return getlist;
    


DatabaseHelper

package com.sqlhelp;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.zte.utils.GetDeviceInfo;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiongchen on 2017/5/11.
 */

public class DatabaseHelper extends OrmLiteSqliteOpenHelper 

    private static final String TABLE_NAME = GetDeviceInfo.getSDPath() + "/ncproject/data/" + "readmachine.db";
//    private static final String TABLE_NAME = getSDPath() + "/ncproject/data/" + "readmachine.db";

    private Map<String, Dao> daos = new HashMap<String, Dao>();

    private DatabaseHelper(Context context) 
        super(context, TABLE_NAME, null, 4);
//        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(getSDPath()+"/ncproject/data"+TABLE_NAME, null);
//        // ORMLite的android.jar封装SQLiteDatabase
//        ConnectionSource aconnectionSource = new AndroidConnectionSource(db);
//        Log.e("DatabaseHelper","SD:"+getSDPath());
//        db.close();
    

    private ArrayList<String> list = new ArrayList<>();
    Object mObject = new Object[10];

    //
    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) 

        Log.e("111", "DB onCreate");
//        try
//
//            TableUtils.createTable(connectionSource, base_user_info_g.class);
//            Log.e("111","base_user_info_g 建表");
//            TableUtils.createTable(connectionSource, base_user_info_w.class);
//            Log.e("111","base_user_info_w 建表");
//            TableUtils.createTable(connectionSource, cb_his.class);
//            Log.e("111","cb_his 建表");
//            TableUtils.createTable(connectionSource, datadesign.class);
//            Log.e("111","datadesign 建表");
//            TableUtils.createTable(connectionSource, fee_detail_g.class);
//            Log.e("111","fee_detail_g 建表");
//            TableUtils.createTable(connectionSource, fee_detail_w.class);
//            Log.e("111","fee_detail_w 建表");
//            TableUtils.createTable(connectionSource, garbage_detail.class);
//            Log.e("111","garbage_detail 建表");
//            TableUtils.createTable(connectionSource, garbage_list.class);
//            Log.e("111","garbage_list 建表");
//            TableUtils.createTable(connectionSource, meter_status.class);
//            Log.e("111","meter_status 建表");
//            TableUtils.createTable(connectionSource, mix_info.class);
//            Log.e("111","mix_info 建表");
//            TableUtils.createTable(connectionSource, personschange.class);
//            Log.e("111","personschange 建表");
//            TableUtils.createTable(connectionSource, photo.class);
//            Log.e("111","photo 建表");
//            TableUtils.createTable(connectionSource, price_rate_g.class);
//            Log.e("111","price_rate_g 建表");
//            TableUtils.createTable(connectionSource, price_rate_w.class);
//            Log.e("111","price_rate_w 建表");
//            TableUtils.createTable(connectionSource, work_log.class);
//            Log.e("111","work_log 建表");
//         catch (SQLException e)
//            e.printStackTrace();
//        

    

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) 
        Log.e("111", "DB onUpgrade");
//        try
//            TableUtils.dropTable(connectionSource, base_user_info_g.class, true);
//            TableUtils.dropTable(connectionSource, base_user_info_w.class, true);
//            TableUtils.dropTable(connectionSource, cb_his.class, true);
//            TableUtils.dropTable(connectionSource, datadesign.class, true);
//            TableUtils.dropTable(connectionSource, fee_detail_g.class, true);
//            TableUtils.dropTable(connectionSource, fee_detail_w.class, true);
//            TableUtils.dropTable(connectionSource, garbage_detail.class, true);
//            TableUtils.dropTable(connectionSource, garbage_list.class, true);
//            TableUtils.dropTable(connectionSource, meter_status.class, true);
//            TableUtils.dropTable(connectionSource, mix_info.class, true);
//            TableUtils.dropTable(connectionSource, personschange.class, true);
//            TableUtils.dropTable(connectionSource, photo.class, true);
//            TableUtils.dropTable(connectionSource, price_rate_g.class, true);
//            TableUtils.dropTable(connectionSource, price_rate_w.class, true);
//            TableUtils.dropTable(connectionSource, work_log.class, true);
//            onCreate(database, connectionSource);
//         catch (SQLException e)
//            e.printStackTrace();
//        

    


    private static DatabaseHelper instance;

    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized DatabaseHelper getHelper(Context context) 


        if (!GetDeviceInfo.fileIsExists(GetDeviceInfo.getSDPath() + "/ncproject/"))
            File file = new File(GetDeviceInfo.getSDPath() + "/ncproject/");
            file.mkdirs();
        
        if (!GetDeviceInfo.fileIsExists(GetDeviceInfo.getSDPath() + "/ncproject/data/"))
            File file = new File(GetDeviceInfo.getSDPath() + "/ncproject/data/");
            file.mkdirs();
        

        if (!GetDeviceInfo.fileIsExists(GetDeviceInfo.getSDPath() + "/ncproject/backup/"))
            File file = new File(GetDeviceInfo.getSDPath() + "/ncproject/backup/");
            file.mkdirs();
        

        if (!GetDeviceInfo.fileIsExists(TABLE_NAME)) 
            Log.e("TAG", "11111111111");
            //DB文件不存在弹出提示框,提示DB不存在无法进入APP
            return null;
        

        context = context.getApplicationContext();
        if (instance == null) 
            synchronized (DatabaseHelper.class) 
                if (instance == null)
                    instance = new DatabaseHelper(context);
            
        

        return instance;
    

    public synchronized Dao getDao(Class clazz) throws SQLException 
        Dao dao = null;
        String className = clazz.getSimpleName();

        if (daos.containsKey(className)) 
            dao = daos.get(className);
        
        if (dao == null) 
            dao = super.getDao(clazz);
            daos.put(className, dao);
        
        return dao;
    

    /**
     * 释放资源
     */
    @Override
    public void close() 
        super.close();

        for (String key : daos.keySet()) 
            Dao dao = daos.get(key);
            dao = null;
        
    



UserDao

package com.sqlhelp;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.Where;
import com.sqlhelp.been.datadesign;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by xiongchen on 2017/5/12.
 */

public class UserDao 
    private Context context;
    private Dao<Been, Integer> userDaoOpe;
    private DatabaseHelper helper;
    public UserDao(Context context,Class been)
    
        this.context = context;

        Log.e("UserDao","been:"+been);
        try
        
            helper = DatabaseHelper.getHelper(context);
            userDaoOpe = helper.getDao(been);
         catch (SQLException e)
        
            e.printStackTrace();
        
    

    /**
     * 增加一个记录
     * @param been
     */
    public void add(Been been)
    
        try
        
            userDaoOpe.create(been);
         catch (SQLException e)
        
            e.printStackTrace();
        

    //...other operations


    /**
     * 删除一条记录
     * @param id
     */
    public  void remove(int id)
        try 
            userDaoOpe.deleteById(id);
         catch (SQLException e) 
            e.printStackTrace();
        
    


    /**
     * 更新一条记录
     * @param user
     */
    public  void Update(Been user)
        try 
            userDaoOpe.update(user);
         catch (SQLException e) 
            e.printStackTrace();
        
    


    /**
     * 查找用户
     * @param map
     */
    public List<Been> Serch_Date(List<Been> item , Map<String, Object> map)
        //Been item = new Been();
        try 
            item = userDaoOpe.queryForFieldValues(map);
         catch (SQLException e) 
            e.printStackTrace();
        
        return  item;
    


    public void Search(Map<String ,Object> map,Class clazz )

        try 
            Where<datadesign, Integer> where = DatabaseHelper.getHelper(context).getDao(clazz)
                    .queryBuilder().where();
            int i= 0;
            for (String key : map.keySet()) 

                where.eq(key,map.get(key));
                if (map.keySet().size() != i+1)
                    where.and();
                
                i++;
            
            Log.e("Search","查询条件添加完毕");
            List<datadesign> list = where.query();
            Log.e("Search","list:"+list.size());
         catch (SQLException e) 
            e.printStackTrace();
        
    

这个是数据库db表中映射的bean (水表)

package com.sqlhelp.been;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.sqlhelp.been.bluebean.BeanGLH;

import java.io.Serializable;
import java.util.List;

/**
 * Created by xiongchen on 2017/5/22.
 */
@DatabaseTable(tableName = "base_user_info_w")//计划表 水
public class base_user_info_w implements Serializable 
    @DatabaseField(columnName = "ciid", id = true)//单位号
    private String ciid;
    @DatabaseField(columnName = "miid")//用户编号base_user_info_w
    private String miid;
    @DatabaseField(columnName = "miname")//用户名称
    private String miname;
    @DatabaseField(columnName = "miadr")//用户地址
    private String miadr;
    @DatabaseField(columnName = "milinkman")//联系人
    private String milinkman;
    @DatabaseField(columnName = "miconnectphone")//固定电话
    private String miconnectphone;
    @DatabaseField(columnName = "miconnectmtel")//手机号码
    private String miconnectmtel;
    @DatabaseField(columnName = "mibfid")//表册编号       //表册编号
    private String mibfid;
    @DatabaseField(columnName = "micbh")//册页号
    private String micbh;
    @DatabaseField(columnName = "mipfid")//用水性质
    private String mipfid;
    @DatabaseField(columnName = "micaliber")//水表口径
    private String micaliber;
    @DatabaseField(columnName = "miopenaccount")//立户日期
    private String miopenaccount;
    @DatabaseField(columnName = "hbdate")//换表日期
    private String hbdate;
    @DatabaseField(columnName = "hbcode")//换表表底
    private int hbcode;
    @DatabaseField(columnName = "hbper")//换表人
    private String hbper;
    @DatabaseField(columnName = "hbstatus")//换表状态
    private String hbstatus;
    @DatabaseField(columnName = "milastreaddate")//上次抄表日期
    private String milastreaddate;
    @DatabaseField(columnName = "milastcode")//上次抄表示数
    private int milastcode;
    @DatabaseField(columnName = "milastsl")//上次抄表水量
    private int milastsl;
    @DatabaseField(columnName = "miyeartotalsl")//年累计水量
    private int miyeartotalsl;
    @DatabaseField(columnName = "mileave1")//一阶余量
    private int mileave1;
    @DatabaseField(columnName = "mileave2")//二阶余量
    private int mileave2;
    @DatabaseField(columnName = "misaving")//预存金额
    private int misaving;
    @DatabaseField(columnName = "miqfmoney")//历史欠费
    private int miqfmoney;
    @DatabaseField(columnName = "miscode")//起码
    private int miscode;
    @DatabaseField(columnName = "miecode")//止码
    private String miecode;
    @DatabaseField(columnName = "misl")//本期总水量
    private int misl;
    @DatabaseField(columnName = "mitotalmoney")//本期总金额
    private float mitotalmoney;
    @DatabaseField(columnName = "miassesssl")//本期抄表估量
    private int miassesssl;
    @DatabaseField(columnName = "miface")//抄表表况
    private String miface;
    @DatabaseField(columnName = "mimemo")//用户备注
    private String mimemo;
    @DatabaseField(columnName = "micbmemo")//抄表备注
    private String micbmemo;
    @DatabaseField(columnName = "mideptno")
    private String mideptno;
    @DatabaseField(columnName = "mideptname")
    private String mideptname;
    @DatabaseField(columnName = "copyerid")
    private String copyerid;
    @DatabaseField(columnName = "copyername")
    private String copyername;
    @DatabaseField(columnName = "copydate")
    private String copydate;
    @DatabaseField(columnName = "copytel")
    private String copytel;
    @DatabaseField(columnName = "miplussl")     //截止底数
    private int miplussl;
    @DatabaseField(columnName = "miclientno")
    private String miclientno;
    @DatabaseField(columnName = "wgcount")
    private String wgcount;
    @DatabaseField(columnName = "isupload")   //是否上传
    private String isupload;
    @DatabaseField(columnName = "isdownload")   //是否下载
    private String isdownload;
    @DatabaseField(columnName = "iscopy")       //是否抄表
    private String iscopy;
    @DatabaseField(columnName = "isprint")      //是否打印
    private String isprint;
    @DatabaseField(columnName = "miepiposition")
    private String miepiposition;
    @DatabaseField(columnName = "rlrmonth")
    private String rlrmonth;
    @DatabaseField(columnName = "mimaxsl")
    private int mimaxsl;
    @DatabaseField(columnName = "miemotion")
    private String miemotion;
    @DatabaseField(columnName = "mileave11")
    private int mileave11;
    @DatabaseField(columnName = "mileave21")
    private Android数据库进阶-从SQLite到ORMLite封装

Android数据库框架-----ORMLite 的基本用法

实战基于OpenCV的水表字符识别(OCR)

arm总线带宽的余量如何设计大于30

[ 转]Android快速开发–使用ORMLite操作数据库

android OrmLite 入门