OrmLite 轻量级ORM框架工具

前言

在项目中用到了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并且创建jdbcConnectionSource

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());
        }



    }
}

Channel.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;
    }
}



关于多条件查询的querybuilder 的用法,后续再完善

你可能感兴趣的:(Java)