Translation Ormlite Document--Getting Started

Source: Internet
Author: User

Objective

This document is translated into the first learning Ormlite framework, please correct me if there is something wrong in it. If the translation is inconsistent with the original document, please refer to the original document. In principle, it is recommended to learn the original English documents.

----------------------------------------------------------------------------------------------

First, Getting Started 1.1 download Ormlite Jar

In order to get started with ormlite, you need to download the jar file. The Ormlite release package is a default library, but the associated jar files are also available from the Maven repository and Sourceforge.

Users who connect to a SQL database through JDBC need to download Ormlite-jdbc-4.48.jar and Ormlite-core-4.48.jar two files. For use in Android apps, you need to download Ormlite-android-4.48.jar and Ormlite-core-4.48.jar two files. You need ormlite-core to publish the package in JDBC, or Android, that has ormlite backend implementations. Ormlite does not have any external dependencies, even if there are other options that you might want to use. See external dependencies. The code applies to Java version 5 or later.

1.2 Configuring a POJO

Here is an example of a POJO that is persisted to data by using the Ormlite annotation configuration. @DatabaseTable Annotations Configure the Access class to a table with the database named accounts. @DatabaseField annotations map Each field in the account to a field of the same name in the database.

The Name field is configured as the primary key for the database table by using the Id=true annotation field. Also, it is worth mentioning that an argument-free constructor is necessary so that a query can return an object. For more information (JPA annotations and other methods to configure classes), see later in the Manual of the class installer information. See Set up your class.

@DatabaseTable (TableName= "Accounts") Public classAccount {@DatabaseField (id=true)    PrivateString name; @DatabaseFieldPrivateString password;  PublicAccount () {//Ormlite needs a no-arg constructor    }     PublicAccount (string name, string password) { This. Name =name;  This. Password =password; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}
1.3 Configuring a DAO

A typical Java pattern is the separation of database operations in a data Access object (DAO) class. Each DAO does not have a POJO that needs to be persisted provides the function of increment, delete, change and so on. A simple way to build a DAO is to use DaoManager  static methods on the class createDao  . for example, to create a DAO for the Accout class above, you can do this:

Dao<account, string> Accountdao =  Daomanager.createdao (Connectionsource, account. class );D ao<order, integer> Orderdao =  Daomanager.createdao (Connectionsource, Order. class);

more information about how to set up DAOs is available later in the manual. See section Daos settings.

1.4 Code Examples

This example uses a native Java H2 database to create an in-memory test database. If you need to run the following sample code, you need to download the H2 jar file to add to your ClassPath. Note:Android users See sample code for Android.

The code is performed as follows.

    1. Create a connection source to connect to the database.
    2. Instantiate a DAO for the account object.
    3. Create the Accounts table in the database. If the table has already been created, this step does not have to be performed.
 Public classAccountapp { Public Static voidMain (string[] args)throwsException {//This uses H2 by default and change to match your databaseString Databaseurl = "Jdbc:h2:mem:account"; //Create a connection source to our databaseConnectionsource Connectionsource =NewJdbcconnectionsource (Databaseurl); //instantiate the DAODao<account, string> Accountdao =Daomanager.createdao (Connectionsource, account. )class); //If you need to create the ' accounts ' table make this callTableutils.createtable (Connectionsource, account.)class); Once we have configured our database objects, we can use them to persist a account to the database and query forIt from the database by its ID://Create an instance of accountAccount Account =NewAccount (); Account.setname ("Jim Coakley"); //persist The account object to the databaseaccountdao.create (account); //Retrieve the account from the database by its ID field (name)Account Account2 = Accountdao.queryforid ("Jim Coakley"); System.out.println ("Account:" +account2.getname ()); //Close the connection sourceConnectionsource.close (); }}

With these points you should be able to start using Ormlite. To understand the ormlite more effective functions, continue to the next chapter.

Translation Ormlite Document--Getting Started

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.