In my humble opinion, the two thrusts of implementing ORM are generics and reflection. Let's start by looking at how to contribute to the DAO layer.
First, the Dbaccessorm interface is extended on the basis of DBAccess to form an ORM-based data call.
/** * Data Access Object * * @author Frank Cheung * */public interface Dbaccessorm extends DBAccess {/** * Query single record, return entity * * @param s QL * Native SQL statement * @param clazz * POJO class * @return The entity class containing the value */<t> T queryone (String SQL, class< ; T> clazz)/** * Query single record, return entity * * @param QR * Patchwork SQL * @param clazz * POJO class * @return entity class containing values */& Lt T> T queryone (queryrunner qr, class<t> clazz);/** * Query multi-line record * * @param SQL * Native SQL statement * @return if not found To record return null */<t> t[] querylist (String sql, class<t> clazz);/** * Query Multiline record * * @param QR * Patchwork SQL * @ Return if no record is found returns null */<t> t[] querylist (queryrunner qr, class<t> clazz);/** * Querying multiple lines of records and paging * * @param QR * Patchwork SQL * @param start * start * number of rows * @param limit * Number of rows read * @return If no record 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 * * @p Aram TableName * Table name * @param data * MAP structure data * @param UID * UUID * @return Result information Object *///Re Sult<updateaction> Update (String tablename, irecord data, String uid),/** * Specify table name and ID, delete a record * * @param tablename * Table name * @param UID * UUID * @return Result Information Object *///Boolean Delete (String tablename, String uid);}
The current read operation is completed, and the write operation is left to be completed.
How to use:
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 entity class only needs to pass in the target Class.class to define the specific type of the generic, without the strongly-typed conversion.
Can understand the generic type, it is more convenient to use. Next, let's talk about the use of reflection.
To give an example, query single-line data to return entities.
Public <T> T Queryone (String sql, class<t> clazz) {result<record> result = Queryone (SQL);//Query result if (resu Lt! = null) {T obj = reflect.newinstance (clazz);//Create POJO instance by reflection for (String Name:result.result.keySet ()) REFLECT.SETPR Operty (obj, name, Result.result.get (name)); return obj;} else return null;}
Reflect.newinstance (Clazz); is to create an instance of POJO through reflection, the Bean. It is useless to have a Bean empty instance, and to plug the data into it. We use the Reflect.setproperty () of the reflection package to invoke the setter plug data. Of course, the prerequisite is that the key of the MAP is the setxxx of the Bean with the XXX can be the number.
For the use of Java reflection, you can look at my previous article "Reflection (Reflection) Memo".
Now, with generics and reflection, you can simply create an ORM method to invoke the data.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Two key thrusts of implementing ORM: Generics and reflection