public class Builder {
    /**
     * 存放创建数据表的sql文件, 需要存储在assets目录
     */
    private final static String CREATE_SQL_FILE = "db/create.sql";
    /**
     * 存放清空数据表的sql文件, 需要存储在assets目录
     */
    private final static String CLEAN_SQL_FILE = "db/clean.sql";
    /**
     * 存放sql升级文件的目录 ( 里面会有多个版本的升级文件,文件名以数据库版本号命名 ), 需要存储在assets目录
     */
    private final static String SQL_PATH = "db/migrations";
    /**
     * Context , 用于访问assets目录下的文件
     */
    Context context;
    /**
     * 数据库名字
     */
    String dbName = "simple_database.db";
    /**
     * 数据库版本
     */
    int dbVersion = 1;
    /**
     * 创建数据库表的文件位置( 一般存在assets目录中 ),具体的文件名, 默认为 db/create.sql
     */
    String createSqlFile = CREATE_SQL_FILE;
    /**
     * 清空数据库表的文件位置( 一般存在assets目录中 ),具体的文件名, 默认为 db/clean.sql
     */
    String emptySqlFile = CLEAN_SQL_FILE;
    /**
     * 升级数据库的sql文件存储目录,默认为 db/migrations
     */
    String upgradePath = SQL_PATH;

    public Builder(Context context) {
        this.context = context;
    }

    public Builder setDbName(String dbName) {
        this.dbName = dbName;
        return this;
    }

    public Builder setDbVersion(int dbVersion) {
        this.dbVersion = dbVersion;
        return this;
    }

    /**
     * 设置创建数据库表的sql文件
     *
     * @param createSqlFile
     * @return
     */
    public Builder setCreateSqlFile(String createSqlFile) {
        this.createSqlFile = createSqlFile;
        return this;
    }

    /**
     * 设置清空数据库的sql文件
     *
     * @param createSqlFile
     * @return
     */
    public Builder setCleanSqlFile(String createSqlFile) {
        this.emptySqlFile = createSqlFile;
        return this;
    }

    /**
     * 设置更新路径
     *
     * @param upgradePath
     * @return
     */
    public Builder setUpgradePath(String upgradePath) {
        this.upgradePath = upgradePath;
        return this;
    }

    public Context getContext() {
        return context;
    }

    public String getDbName() {
        return dbName;
    }

    public int getDbVersion() {
        return dbVersion;
    }

    public String getCreateSqlFile() {
        return createSqlFile;
    }

    public String getCleanSqlFile() {
        return emptySqlFile;
    }

    public String getUpgradePath() {
        return upgradePath;
    }

    private void checkConfig() {
        if (TextUtils.isEmpty(dbName)) {
            throw new IllegalArgumentException("你没有设置数据库名称 !!! ");
        }
        if (TextUtils.isEmpty(createSqlFile)) {
            throw new IllegalArgumentException("你没有设置创建数据库表的文件位置");
        }
        if (TextUtils.isEmpty(upgradePath)) {
            throw new IllegalArgumentException("你没有设置创建数据库升级文件的目录");
        }
    }

    public DatabaseHelper create() {
        checkConfig();
        return new DatabaseHelper(this);
    }
} public interface DbListener<T> {
    void onComplete(T result);
} public class BookDao extends AbsDAO<Book> {

    public BookDao() {
        super("books");
    }

    @Override
    protected ContentValues convert(Book item) {
        ContentValues newValues = new ContentValues();
        newValues.put("id", item.id);
        newValues.put("name", item.name);
        return newValues;
    }


    @Override
    protected Book parseOneItem(Cursor cursor) {
        Book item = new Book();
        item.id = cursor.getString(0);
        item.name = cursor.getString(1) ;
        return item;
    }

} public class BorrowDao extends AbsDAO<BorrowRecord> {

    public BorrowDao() {
        super("borrow");
    }

    @Override
    protected ContentValues convert(BorrowRecord item) {
        ContentValues newValues = new ContentValues();
        newValues.put("user_id", item.userId);
        newValues.put("book_id", item.bookId);
        return newValues;
    }

    @Override
    protected BorrowRecord parseOneItem(Cursor cursor) {
        BorrowRecord item = new BorrowRecord();
        item.userId = cursor.getString(0);
        item.bookId = cursor.getString(1);
        return item;
    }

} public class UserDao extends AbsDAO<User> {

    private BorrowDao mBorrowDao = new BorrowDao();

    public UserDao() {
        super("users");
    }

    @Override
    public void insert(User item) {
        super.insert(item);
        // 插入借阅表
        insertBorrowedBooks(item);
    }

    private void insertBorrowedBooks(User item) {
        if (item.borrowedBooks != null) {
            BorrowRecord record = new BorrowRecord();
            record.userId = item.id;
            for (Book aBook : item.borrowedBooks) {
                record.bookId = aBook.id;
                mBorrowDao.insert(record);
            }
        }
    }

    @Override
    protected ContentValues convert(User item) {
        ContentValues newValues = new ContentValues();
        newValues.put("id", item.id);
        newValues.put("name", item.name);
        newValues.put("gender", item.gender);
        return newValues;
    }

    @Override
    protected User parseOneItem(Cursor cursor) {
        User user = new User();
        user.id = cursor.getString(0);
        user.name = cursor.getString(1);
        user.gender = cursor.getInt(2);
        // 查询该用户借阅的书
        queryBorrowedBooks(user);
        return user;
    }

    private void queryBorrowedBooks(User user) {
        List<BorrowRecord> records = mBorrowDao.query("user_id=?", new String[]{user.id});
        if (records.size() == 0) {
            return;
        }
        // 查询到该用户借阅的书籍
        user.borrowedBooks = new ArrayList<>();
        final BookDao bookDao = new BookDao();
        for (BorrowRecord aRecord : records) {
            Book aBook = bookDao.queryOne("id=?", new String[]{aRecord.bookId});
            user.borrowedBooks.add(aBook);
        }
    }
} public class Book {
    public String id ;
    public String name ;
} public class BorrowRecord {
    public String userId ;
    public String bookId ;
} public class User {
    public String id ;
    public String name ;
    public int gender ;
    public List<Book> borrowedBooks ;
} public static <T> InsertBuilder<T> insertInto(Class<? extends AbsDAO<T>> daoClass) {
    return new InsertBuilder<T>(daoClass);
} public static <T> QueryBuilder<T> selectFrom(Class<? extends AbsDAO<T>> daoClass) {
    return new QueryBuilder<>(daoClass);
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.simple.database.Builder;
import com.simple.database.listeners.DbListener;
import com.simple.simpledatabase.dao.BookDao;
import com.simple.simpledatabase.dao.BorrowDao;
import com.simple.simpledatabase.dao.UserDao;
import com.simple.simpledatabase.domain.Book;
import com.simple.simpledatabase.domain.BorrowRecord;
import com.simple.simpledatabase.domain.User;
import java.util.ArrayList;
import java.util.List;
import static com.simple.database.DatabaseHelper.insertInto;
import static com.simple.database.DatabaseHelper.selectFrom;
package com.simple.simpledatabase;





/**
 * 演示 数据的增删改查. 请查看 {@link com.simple.simpledatabase.model.BookModel} 类
 */
public class MainActivity extends AppCompatActivity {
    private List<User> mAllUsers;
    private List<Book> mAllBooks;
    private int mBorrowedIndex = 0;
    private TextView mLogTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // 初始化数据库
        initDatabase();
        initWidgets();
        mAllUsers = createUser();
        mAllBooks = createBooks();

        // 插入数据, 用户和数据
        insertInto(UserDao.class).withItems(mAllUsers).execute();