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 int mileave21;
    @DatabaseField(columnName = "codesource")
    private String codesource;
    @DatabaseField(columnName = "alias")
    private String alias;
    @DatabaseField(columnName = "isstep")
    private String isstep;
    @DatabaseField(columnName = "mino")
    private String mino;
    @DatabaseField(columnName = "miavgsl")
    private String miavgsl;

    //    @DatabaseField(columnName = "MGarbageFlag")
//    private String MGarbageFlag;
//    @DatabaseField(columnName = "MGarbageID")
//    private String MGarbageID;
//    @DatabaseField(columnName = "GarbageNum")       //垃圾费户数
//    private int GarbageNum;
//    @DatabaseField(columnName = "stepBeginMouth")
//    private String stepBeginMouth;
//    @DatabaseField(columnName = "mans")
//    private int mans;
//    @DatabaseField(columnName = "miyeartotal")      //年累计水量
//    private int miyeartotal;


    //蓝牙相关
    private boolean isSelect;

    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean isSelect) {
        this.isSelect = isSelect;
    }

    private String mipfidName;//用水性质中文

    public String getMipfidName() {
        return mipfidName;
    }

    public void setMipfidName(String mipfidName) {
        this.mipfidName = mipfidName;
    }

    private int totalYealSL;//年累计水量

    public int getTotalYealSL() {
        return totalYealSL;
    }

    public void setTotalYealSL(int totalYealSL) {
        this.totalYealSL = totalYealSL;
    }

    private String stepBeginTime;//阶梯起始日期

    public String getStepBeginTime() {
        return stepBeginTime;
    }

    public void setStepBeginTime(String stepBeginTime) {
        this.stepBeginTime = stepBeginTime;
    }

    private int garbageNum; //垃圾费人口数

    public int getGarbageNum() {
        return garbageNum;
    }

    public void setGarbageNum(int garbageNum) {
        this.garbageNum = garbageNum;
    }

    private int miPersons;//变更的人口数

    public int getMiPersons() {
        return miPersons;
    }

    public void setMiPersons(int miPersons) {
        this.miPersons = miPersons;
    }

    private int mileave111;

    public int getMileave111() {
        return mileave111;
    }

    public void setMileave111(int mileave111) {
        this.mileave111 = mileave111;
    }

    private int mileave222;

    public int getMileave222() {
        return mileave222;
    }

    public void setMileave222(int mileave222) {
        this.mileave222 = mileave222;
    }


    public String getMifaceName() {
        return miface;
    }

    public void setMifaceName(String miface) {
        this.miface = miface;
    }


    private int flag;        //气表标识  2代表气

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    private List<BeanGLH> beanGLH;    //水气关联号

    public List<BeanGLH> getBeanGLH() {
        return beanGLH;
    }

    public void setBeanGLH(List<BeanGLH> beanGLH) {
        this.beanGLH = beanGLH;
    }


    //所以用户界面相关
    private boolean selectored = false;

    public boolean getSelectored() {
        return selectored;
    }

    public void setSelectored(boolean selectored) {
        this.selectored = selectored;
    }

    private boolean isVisible = true;

    public boolean getVisible() {
        return isVisible;
    }

    public void setVisible(boolean visible) {
        isVisible = visible;
    }


    public String getCiid() {
        return ciid;
    }

    public void setCiid(String ciid) {
        this.ciid = ciid;
    }

    public String getMiid() {
        return miid;
    }

    public void setMiid(String miid) {
        this.miid = miid;
    }

    public String getMiname() {
        return miname;
    }

    public void setMiname(String miname) {
        this.miname = miname;
    }

    public String getMiadr() {
        return miadr;
    }

    public void setMiadr(String miadr) {
        this.miadr = miadr;
    }

    public String getMilinkman() {
        return milinkman;
    }

    public void setMilinkman(String milinkman) {
        this.milinkman = milinkman;
    }

    public String getMiconnectphone() {
        return miconnectphone;
    }

    public void setMiconnectphone(String miconnectphone) {
        this.miconnectphone = miconnectphone;
    }

    public String getMiconnectmtel() {
        return miconnectmtel;
    }

    public void setMiconnectmtel(String miconnectmtel) {
        this.miconnectmtel = miconnectmtel;
    }

    public String getMibfid() {
        return mibfid;
    }

    public void setMibfid(String mibfid) {
        this.mibfid = mibfid;
    }

    public String getMicbh() {
        return micbh;
    }

    public void setMicbh(String micbh) {
        this.micbh = micbh;
    }

    public String getMipfid() {
        return mipfid;
    }

    public void setMipfid(String mipfid) {
        this.mipfid = mipfid;
    }

    public String getMicaliber() {
        return micaliber;
    }

    public void setMicaliber(String micaliber) {
        this.micaliber = micaliber;
    }

    public int getHbcode() {
        return hbcode;
    }

    public void setHbcode(int hbcode) {
        this.hbcode = hbcode;
    }

    public String getHbper() {
        return hbper;
    }

    public void setHbper(String hbper) {
        this.hbper = hbper;
    }

    public String getHbstatus() {
        return hbstatus;
    }

    public void setHbstatus(String hbstatus) {
        this.hbstatus = hbstatus;
    }

    public int getMilastcode() {
        return milastcode;
    }

    public void setMilastcode(int milastcode) {
        this.milastcode = milastcode;
    }

    public int getMilastsl() {
        return milastsl;
    }

    public void setMilastsl(int milastsl) {
        this.milastsl = milastsl;
    }

    public int getMiyeartotalsl() {
        return miyeartotalsl;
    }

    public void setMiyeartotalsl(int miyeartotalsl) {
        this.miyeartotalsl = miyeartotalsl;
    }

    public int getMileave1() {
        return mileave1;
    }

    public void setMileave1(int mileave1) {
        this.mileave1 = mileave1;
    }

    public int getMileave2() {
        return mileave2;
    }

    public void setMileave2(int mileave2) {
        this.mileave2 = mileave2;
    }

    public int getMisaving() {
        return misaving;
    }

    public void setMisaving(int misaving) {
        this.misaving = misaving;
    }

    public int getMiqfmoney() {
        return miqfmoney;
    }

    public void setMiqfmoney(int miqfmoney) {
        this.miqfmoney = miqfmoney;
    }

    public int getMiscode() {
        return miscode;
    }

    public void setMiscode(int miscode) {
        this.miscode = miscode;
    }

    public String getMiecode() {
        return miecode;
    }

    public void setMiecode(String miecode) {
        this.miecode = miecode;
    }

    public int getMisl() {
        return misl;
    }

    public void setMisl(int misl) {
        this.misl = misl;
    }

    public float getMitotalmoney() {
        return mitotalmoney;
    }

    public void setMitotalmoney(float mitotalmoney) {
        this.mitotalmoney = mitotalmoney;
    }

    public void setMiassesssl(int miassesssl) {
        this.miassesssl = miassesssl;
    }

    public int getMiassesssl() {
        return miassesssl;
    }

    public String getMiface() {
        return miface;
    }

    public void setMiface(String miface) {
        this.miface = miface;
    }

    public String getMimemo() {
        return mimemo;
    }

    public void setMimemo(String mimemo) {
        this.mimemo = mimemo;
    }

    public String getMicbmemo() {
        return micbmemo;
    }

    public void setMicbmemo(String micbmemo) {
        this.micbmemo = micbmemo;
    }

    public String getMideptno() {
        return mideptno;
    }

    public void setMideptno(String mideptno) {
        this.mideptno = mideptno;
    }

    public String getMideptname() {
        return mideptname;
    }

    public void setMideptname(String mideptname) {
        this.mideptname = mideptname;
    }

    public String getCopyerid() {
        return copyerid;
    }

    public void setCopyerid(String copyerid) {
        this.copyerid = copyerid;
    }

    public String getCopyername() {
        return copyername;
    }

    public void setCopyername(String copyername) {
        this.copyername = copyername;
    }

    public String getCopytel() {
        return copytel;
    }

    public void setCopytel(String copytel) {
        this.copytel = copytel;
    }

    public int getMiplussl() {
        return miplussl;
    }

    public void setMiplussl(int miplussl) {
        this.miplussl = miplussl;
    }

    public String getMiclientno() {
        return miclientno;
    }

    public void setMiclientno(String miclientno) {
        this.miclientno = miclientno;
    }

    public String getWgcount() {
        return wgcount;
    }

    public void setWgcount(String wgcount) {
        this.wgcount = wgcount;
    }

    public String getIsupload() {
        return isupload;
    }

    public void setIsupload(String isupload) {
        this.isupload = isupload;
    }

    public String getIsdownload() {
        return isdownload;
    }

    public void setIsdownload(String isdownload) {
        this.isdownload = isdownload;
    }

    public String getIscopy() {
        return iscopy;
    }

    public void setIscopy(String iscopy) {
        this.iscopy = iscopy;
    }

    public String getIsprint() {
        return isprint;
    }

    public void setIsprint(String isprint) {
        this.isprint = isprint;
    }

    public String getMiepiposition() {
        return miepiposition;
    }

    public void setMiepiposition(String miepiposition) {
        this.miepiposition = miepiposition;
    }

    public String getRlrmonth() {
        return rlrmonth;
    }

    public void setRlrmonth(String rlrmonth) {
        this.rlrmonth = rlrmonth;
    }

    public int getMimaxsl() {
        return mimaxsl;
    }

    public void setMimaxsl(int mimaxsl) {
        this.mimaxsl = mimaxsl;
    }

    public String getMiemotion() {
        return miemotion;
    }

    public void setMiemotion(String miemotion) {
        this.miemotion = miemotion;
    }

    public int getMileave11() {
        return mileave11;
    }

    public void setMileave11(int mileave11) {
        this.mileave11 = mileave11;
    }

    public int getMileave21() {
        return mileave21;
    }

    public void setMileave21(int mileave21) {
        this.mileave21 = mileave21;
    }

    public String getCodesource() {
        return codesource;
    }

    public void setCodesource(String codesource) {
        this.codesource = codesource;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getIsstep() {
        return isstep;
    }

    public void setIsstep(String isstep) {
        this.isstep = isstep;
    }

    public String getMino() {
        return mino;
    }

    public void setMino(String mino) {
        this.mino = mino;
    }

    public String getMiavgsl() {
        return miavgsl;
    }

    public void setMiavgsl(String miavgsl) {
        this.miavgsl = miavgsl;
    }

//    public String getMGarbageFlag() {
//        return MGarbageFlag;
//    }
//
//    public void setMGarbageFlag(String MGarbageFlag) {
//        this.MGarbageFlag = MGarbageFlag;
//    }
//
//    public String getMGarbageID() {
//        return MGarbageID;
//    }
//
//    public void setMGarbageID(String MGarbageID) {
//        this.MGarbageID = MGarbageID;
//    }
//
//    public int getGarbageNum() {
//        return GarbageNum;
//    }
//
//    public void setGarbageNum(int garbageNum) {
//        GarbageNum = garbageNum;
//    }
//
//    public int getMans() {
//        return mans;
//    }
//
//    public void setMans(int mans) {
//        this.mans = mans;
//    }
//
//    public int getMiyeartotal() {
//        return miyeartotal;
//    }
//
//    public void setMiyeartotal(int miyeartotal) {
//        this.miyeartotal = miyeartotal;
//    }

    public String getMiopenaccount() {
        return miopenaccount;
    }

    public void setMiopenaccount(String miopenaccount) {
        this.miopenaccount = miopenaccount;
    }

    public String getHbdate() {
        return hbdate;
    }

    public void setHbdate(String hbdate) {
        this.hbdate = hbdate;
    }

    public String getMilastreaddate() {
        return milastreaddate;
    }

    public void setMilastreaddate(String milastreaddate) {
        this.milastreaddate = milastreaddate;
    }

    public String getCopydate() {
        return copydate;
    }

    public void setCopydate(String copydate) {
        this.copydate = copydate;
    }

//    public String getStepBeginMouth() {
//        return stepBeginMouth;
//    }
//
//    public void setStepBeginMouth(String stepBeginMouth) {
//        this.stepBeginMouth = stepBeginMouth;
//    }


    /**
     * 熊忠良 2017年6月26日 上午11:39
     */
    //一阶余量
    private int Mileave1_1;
    //二阶余量
    private int Mileave2_1;
    //一阶水费
    private double firstMoney;
    //二阶水费
    private double secondMoney;
    //净水费
    private double jsfprice;
    //污水费
    private double wsfprice;
    //加压费
    private double jyfprice;
    //垃圾费
    private double ljfprice;

    public int getMileave1_1() {
        return Mileave1_1;
    }

    public void setMileave1_1(int mileave1_1) {
        Mileave1_1 = mileave1_1;
    }

    public int getMileave2_1() {
        return Mileave2_1;
    }

    public void setMileave2_1(int mileave2_1) {
        Mileave2_1 = mileave2_1;
    }

    public double getFirstMoney() {
        return firstMoney;
    }

    public void setFirstMoney(double firstMoney) {
        this.firstMoney = firstMoney;
    }

    public double getSecondMoney() {
        return secondMoney;
    }

    public void setSecondMoney(double secondMoney) {
        this.secondMoney = secondMoney;
    }

    public double getJsfprice() {
        return jsfprice;
    }

    public void setJsfprice(double jsfprice) {
        this.jsfprice = jsfprice;
    }

    public double getWsfprice() {
        return wsfprice;
    }

    public void setWsfprice(double wsfprice) {
        this.wsfprice = wsfprice;
    }

    public double getJyfprice() {
        return jyfprice;
    }

    public void setJyfprice(double jyfprice) {
        this.jyfprice = jyfprice;
    }

    public double getLjfprice() {
        return ljfprice;
    }

    public void setLjfprice(double ljfprice) {
        this.ljfprice = ljfprice;
    }
}
阅读更多

没有更多推荐了,返回首页