The implementation of ORM has two main purposes: Generic and reflection.

Source: Internet
Author: User

The implementation of ORM has two main purposes: Generic and reflection.

The monks believe that the two essentials of ORM implementation are generic and reflection. Next let's take a look at how to add bricks and tiles to the DAO layer.

First, expand the DBAccessORM interface on the basis of DBAccess to form an ORM-based data call.

/*** Data Access Object ** @ author Frank Cheung **/public interface DBAccessORM extends DBAccess {/*** query a single record, returns the object ** @ param SQL * native SQL statement * @ param clazz * POJO class * @ return the object class containing the value */<T> T queryOne (String SQL, class <T> clazz);/*** query a single record, return object ** @ param qr * patchwork SQL * @ param clazz * POJO class * @ return object class containing values */<T> T queryOne (QueryRunner qr, class <T> clazz ); /*** query multi-row records ** @ param SQL * native SQL statement * @ return returns null if no record is found */<T> T [] queryList (String SQL, class <T> clazz ); /*** query multi-row records ** @ param qr * piece together SQL * @ return if no record is found, return null */<T> T [] queryList (QueryRunner qr, class <T> clazz ); /*** query multiple rows of records and paging ** @ param qr * piece together SQL * @ param start * start row * @ param limit * read row * @ return if no record is found returns null */<T> T [] queryList (QueryRunner qr, int start, int limit, Class <T> clazz ); /*** insert record ** @ param tablename * Table name * @ param data * Map structure data * @ return Result information object * // Result <CreateAction> insert (String tablename, IRecord data ); /***** update record ** @ param tablename * Table name * @ param data * Map structure data * @ param uid * UUID * @ return Result information object * // Result <updateAction> update (String tablename, IRecord data, String uid);/*** specifies the table name and id, delete a record ** @ param tablename * Table name * @ param uid * UUID * @ return result information object * // boolean delete (String tablename, String uid );}

Currently, the read operation is completed first, and the write operation will be completed later.

Usage:

Public static class News {private String name; public String getName () {return name;} public void setName (String name) {this. name = name ;}/// query a record DBAccessORM dao = new DBAccessImpl_ORM (conn); News news = dao. queryOne ("SELECT * FROM news WHERE uid = '2ccccd21-b89c-416b-a511-59103fd0b1cc '", News. class );
It can be seen that the object Class only needs to pass in the target class. Class to define the specific type of the generic, without the need for forced type conversion.

It is easier to use generic data. Next we will talk about the use of reflection.

For example, a single row of data returns an object.

Public <T> T queryOne (String SQL, Class <T> clazz) {Result <Record> result = queryOne (SQL); // query result if (Result! = Null) {T obj = Reflect. newInstance (clazz); // create a POJO instance for (String name: result. result. keySet () Reflect. setProperty (obj, name, result. result. get (name); return obj;} else return null ;}

Reflect. newInstance (clazz); is an instance that creates POJO through reflection, that is, Bean. It is useless to leave a Bean empty instance alone, and data needs to be inserted into it. We use the Reflect. setProperty () of the reflection package to call the setter plug data. Of course, the premise is that the Map key is matched with the XXX in the Bean's setXXX.

For the use of Java Reflection, see my previous article Reflection memo.

Now, you can create an ORM to call data through generics and reflection.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.