オームライト級ORMフレームワークツール

21752 ワード

前言
プロジェクトでsqliteというデータベースを使いました.第一の考えはORMフレームを探して操作して、OrmLiteという軽量フレームを見つけました.
使ってみて、とても便利な感じがします.このフレームをここに記録します.
プロジェクトに統合する
普通のプロジェクトはjarをダウンロードしてlibの中に入ればいいです.
jarダウンロードアドレス:http://ormlite.com/releases/
maven項目はこの依存を追加すればいいです.

    com.j256.ormlite
    ormlite-jdbc
    4.48
私たちはsqliteを操作していますので、sqlite jdbcを追加する必要があります.
<dependency>
    org.xerial
    sqlite-jdbc
    3.7.2
dependency>
使い方
1、持久類の配置、例えばChannel
package com.mingjie1212.ormlite.entity;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Created by jeiao on 2016/9/11.
 */
@DatabaseTable(tableName = "Channel")
public class Channel {


    @DatabaseField(generatedId = true, allowGeneratedIdInsert = true)
    private int id;
    @DatabaseField(columnName = "name",columnDefinition = "VARCHAR(32)")
    private String name;
    @DatabaseField(columnName = "url", columnDefinition = "VARCHAR(255)")
    private String url;
    @DatabaseField(columnName = "icon_url", columnDefinition = "VARCHAR(255)")
    private String iconUrl;
    @DatabaseField(columnName = "intro", columnDefinition = "VARCHAR(255)")
    private String intro;

    @DatabaseField(columnName = "status",defaultValue ="0",canBeNull = false)
    private int status;
    @DatabaseField(columnName = "is_delete", defaultValue = "0", canBeNull = false)
    private int isDelete;





    public Channel() {
    }

    public Channel(String name) {
        this.name = name;
    }

    public Channel(int id, String name, String url, String iconUrl, String intro) {
        this.id = id;
        this.name = name;
        this.url = url;
        this.iconUrl = iconUrl;
        this.intro = intro;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIconUrl() {
        return iconUrl;
    }

    public void setIconUrl(String iconUrl) {
        this.iconUrl = iconUrl;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(int isDelete) {
        this.isDelete = isDelete;
    }
}
2,JDBCを登録する
static {
    try {
        //    
        Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
3,databaseURLを接続してjdbcConnection Sourceを作成するように配置します.
String databaseUrl = "jdbc:sqlite:tv.db";

//    JDBC  
ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
4、テーブルの削除とテーブルの作成
//         
TableUtils.dropTable(connectionSource, Channel.class, true);

//  Table
TableUtils.createTable(connectionSource, Channel.class);
5,データを操作する
まずDaoを作成します.
//     DAO,        
Dao, Integer> dao = DaoManager.createDao(connectionSource, Channel.class);
a.データの追加
//       
Channel channel1 = new Channel("CCTV1");

dao.create(channel1);

Channel channel2 = new Channel("CCTV2");

dao.create(channel2);
b.データを検索する
//      

Channel channel = dao.queryForId(1);
logger.info(channel.getName());
c.複数の条件に分けてデータを照会する
//                    QueryBuilder
QueryBuilder, Integer> queryBuilder = dao.queryBuilder();

queryBuilder.where().eq("is_delete", 0).and().eq("status", 0);
queryBuilder.orderBy("id", false);
queryBuilder.limit(10);

List channels = dao.query(queryBuilder.prepare());

for (Channel channel3 : channels) {
    logger.info(channel3.getName());
}
d.データを更新する
//      ;
channel.setIconUrl("http://sssss");
dao.update(channel);
e.データを削除する
//      
dao.deleteById(2);
dao.delete(channel);
すべてのコードを添付
Main.java
package com.mingjie1212.ormlite;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.logger.LoggerFactory;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.mingjie1212.ormlite.entity.Channel;

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

/**
 * Created by jeiao on 2016/9/11.
 */
public class Main {


    private static final Logger logger = LoggerFactory.getLogger(Main.class);


    static {
        try {
            //    
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws SQLException {

        String databaseUrl = "jdbc:sqlite:tv.db";

        //    JDBC  
        ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);

        //         
        TableUtils.dropTable(connectionSource, Channel.class, true);

        //  Table
        TableUtils.createTable(connectionSource, Channel.class);


        //     DAO,        
        Dao dao = DaoManager.createDao(connectionSource, Channel.class);


        //       
        Channel channel1 = new Channel("CCTV1");

        dao.create(channel1);

        Channel channel2 = new Channel("CCTV2");

        dao.create(channel2);


        //      

        Channel channel = dao.queryForId(1);
        logger.info(channel.getName());


//        //      
//        dao.deleteById(2);
//        dao.delete(channel);


        //      ;
        channel.setIconUrl("http://sssss");
        dao.update(channel);

        //                    QueryBuilder
        QueryBuilder queryBuilder = dao.queryBuilder();

        queryBuilder.where().eq("is_delete", 0).and().eq("status", 0);
        queryBuilder.orderBy("id", false);
        queryBuilder.limit(10);

        List channels = dao.query(queryBuilder.prepare());

        for (Channel channel3 : channels) {
            logger.info(channel3.getName());
        }



    }
}
Chanel.java
package com.mingjie1212.ormlite.entity;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Created by jeiao on 2016/9/11.
 */
@DatabaseTable(tableName = "Channel")
public class Channel {


    @DatabaseField(generatedId = true, allowGeneratedIdInsert = true)
    private int id;
    @DatabaseField(columnName = "name",columnDefinition = "VARCHAR(32)")
    private String name;
    @DatabaseField(columnName = "url", columnDefinition = "VARCHAR(255)")
    private String url;
    @DatabaseField(columnName = "icon_url", columnDefinition = "VARCHAR(255)")
    private String iconUrl;
    @DatabaseField(columnName = "intro", columnDefinition = "VARCHAR(255)")
    private String intro;

    @DatabaseField(columnName = "status",defaultValue ="0",canBeNull = false)
    private int status;
    @DatabaseField(columnName = "is_delete", defaultValue = "0", canBeNull = false)
    private int isDelete;





    public Channel() {
    }

    public Channel(String name) {
        this.name = name;
    }

    public Channel(int id, String name, String url, String iconUrl, String intro) {
        this.id = id;
        this.name = name;
        this.url = url;
        this.iconUrl = iconUrl;
        this.intro = intro;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getIconUrl() {
        return iconUrl;
    }

    public void setIconUrl(String iconUrl) {
        this.iconUrl = iconUrl;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }


    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(int isDelete) {
        this.isDelete = isDelete;
    }
}
多条件クエリのquerybilderの使い方については、後で改善します.