Ormlite Lightweight ORM Framework Tool

Source: Internet
Author: User
Tags sqlite
Preface

A database of SQLite is used in the project. The first idea was to find an ORM framework to operate and find the lightweight framework for Ormlite.

Use it, it's very convenient, just record this frame here. Consolidate to Project

General project download jar put into lib on the line.

Jar Download Address: http://ormlite.com/releases/

Maven Project Add this dependency on the line

<dependency>
    <groupId>com.j256.ormlite</groupId>
    <artifactid>ormlite-jdbc</ artifactid>
    <version>4.48</version>
</dependency>
Because we're working with SQLite, we need to add sqlite jdbc

<dependency>
    <groupId>org.xerial</groupId>
    <artifactid>sqlite-jdbc</ artifactid>
    <version>3.7.2</version>
</dependency>

How to use 1, configure the persistence class, such as 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, Allowgeneratedidi
    Nsert = true) private int id;
    @DatabaseField (columnName = "Name", ColumnDefinition = "VARCHAR") 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;
    The 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;
    The public void setisdelete (int isdelete) {this.isdelete = Isdelete; }
}

2, register JDBC

static {
    try {
        //Register driver
        class.forname ("Org.sqlite.JDBC");
    } catch (ClassNotFoundException e) {
        E.printstacktrace ();
    }

3, configure connection Databaseurl and create Jdbcconnectionsource

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

Create a JDBC connection
connectionsource Connectionsource = new Jdbcconnectionsource (Databaseurl);

4, delete the table and create the table

Deletes the table while ignoring the error
tableutils.droptable (Connectionsource, Channel.class, true);

CREATE TABLE
tableutils.createtable (Connectionsource, Channel.class);

5, operation of the data

First you need to create a DAO

Instantiate a DAO, perform data manipulation on the table
dao<channel, integer> dao = Daomanager.createdao (Connectionsource, Channel.class);

A. Adding data

Add two piece of data
Channel Channel1 = new Channel ("CCTV1");

Dao.create (CHANNEL1);

Channel Channel2 = new Channel ("CCTV2");

Dao.create (CHANNEL2);

B. Finding a piece of data

Query a data

Channel Channel = Dao.queryforid (1);
Logger.info (Channel.getname ());

C. Paging multiple criteria query data

Query multiple records According to the conditions and pagination and reverse order here used to QueryBuilder
querybuilder<channel, integer> querybuilder = Dao.querybuilder ();

Querybuilder.where (). EQ ("Is_delete", 0). and ()-eq ("status", 0);
Querybuilder.orderby ("id", false);
Querybuilder.limit (ten);

list<channel> channels = Dao.query (Querybuilder.prepare ());

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

D. Updating a piece of data

Change a record;
Channel.seticonurl ("Http://sssss");
Dao.update (channel);

E. Delete a piece of data

Delete a record
Dao.deletebyid (2);
Dao.delete (channel);

Attach All code

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 {//Register driver Class.forName ("Org.sqlite.JDBC");
        catch (ClassNotFoundException e) {e.printstacktrace (); } public static void Main (string[] args) throws SQLException {String Databaseurl = "Jdbc:sqlite:tv.d

        B "; Create a JDBC connection connectionsource connectionsource = new JdBcconnectionsource (Databaseurl);

        Deletes the table while ignoring the error tableutils.droptable (Connectionsource, Channel.class, true);


        CREATE TABLE Tableutils.createtable (Connectionsource, Channel.class);


        Instantiate a DAO, perform data manipulation on the table dao<channel, integer> dao = Daomanager.createdao (Connectionsource, Channel.class);

        Add two piece of data Channel Channel1 = new Channel ("CCTV1");

        Dao.create (CHANNEL1);

        Channel Channel2 = new Channel ("CCTV2");


        Dao.create (CHANNEL2);
        Query a data Channel Channel = Dao.queryforid (1);


Logger.info (Channel.getname ());
Delete a record//Dao.deletebyid (2);


        Dao.delete (channel);
        Change a record;
        Channel.seticonurl ("Http://sssss");

        Dao.update (channel);

        Query multiple records According to the conditions and pagination and reverse order here used to QueryBuilder querybuilder<channel, integer> querybuilder = Dao.querybuilder ();
        Querybuilder.where (). EQ ("Is_delete", 0). and ()-eq ("status", 0); QuErybuilder.orderby ("id", false);

        Querybuilder.limit (10);

        list<channel> 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, Allowgeneratedidi
    Nsert = true) private int id;
    @DatabaseField (columnName = "Name", ColumnDefinition = "VARCHAR") 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;
    The 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;
    The public void setisdelete (int isdelete) {this.isdelete = Isdelete;
 }
}



On the usage of QueryBuilder in the multi-condition query and further perfecting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.