Ibatis SQL MAPS (3)

xiaoxiao2021-03-06  22

See how Ibatis SQL Maps queries data from the database according to One-to-Many relationship:

Resultmap ID = "get-autoinfo-result" class = "bo.autoinfo"

Resultmap is one of the important components in the Ibatis SQL Maps framework, you may remember Resultclass? The concept is basically consistent. ResultMap is a customized mapped statement to return objects. Customizable performance in: For example, I have a data table containing 10 fields, but my Pojo only defines 5 properties. At this time, just take out the field I care about. Similar to hibernate, ResultMap's Result element defines the mapping of the POJO property to the data table field. It should be noted that the Result element select property, which is similar to the loaded subquery and automatically loads. If this subquery contains multiple results, the result is automatically loaded into the list type. The Result element corresponds to the attribute in the public as "autoInfolist", and the people generated by Hibernate are the set type "autoinfoset". These two types cannot be directly mutually rotation, so I modified the Pojo property.

Select ID = "getPeople" ResultMap = "get-person-result" parameterclass = "bo.people"

This is the SELECT type mapped stat. Incoming an instance, returning to the people instance that is customized to contain "AutoInfolist". Parameters in the PEOPLE type parameter is to facilitate multi-field matching queries. Maybe I only need to use the people.id single field match to get the result, but the next new demand may be a joint.address and people.name combination!

Corresponding program code:

Package test; import java.io.reader; import java.util.list; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class automag {Private Reader reader; private people people = new people (); private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public people findPeople () throws Exception {reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader) : People.setid (New Integer ("1")); people = (person) SQLMap.QueryForObject ("getPeople"; Return people;}}

Package test; import java.io.reader; import java.util.list; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class automag {Private Reader reader; private people people = new people (); private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public people findPeople () throws Exception {reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader) People.setid (New Integer); people = (people) SQLMap.QueryForObject ("getPeople", people); returnnder;}} SQLMap.QueryForObject (String arg0, Object arg1)

This method is similar to Hibernate's session.low (...), incoming the mapped statement ID, and then incorporate an object instance containing the primary key. In addition to the SQLMap.QueryForObject (String Arg0, Object Arg1) listed above, there is an overload method:

People = (people) SQLMap.QueryForObject ("getPeople"; replacement into SQLMap.QueryForObject ("getPeople", people, people;

People = (people) SQLMap.QueryForObject ("getPeople"; replacement into SQLMap.QueryForObject ("getPeople", people, people;

The first "people" is an incoming parameter, the second "people" is the result of the return. To express the meaning, only the form of expression is changed.

Below I will talk about how to hold multiple people object instances with a collection class.

In vehicle management applications, people need to list one by one, select some of the details. Similar to such a need, IBATIS SQL MAPS is introduced into SQLMap.QueryforList (String Arg0, Object Arg1) to meet.

Remember how our mapping files wrote? By the way, I will enter the main key value and query!

However, no conditions are not required, directly listed people! Do you have to add new mapped statement to satisfy? Dynamic mappped statement can provide parameters and no differential queries without changing the mapping file:

Dynamic Prepend =" Where "

The essence of the SELECT type Mapped Statement has not changed, just the dynamic WHERE clause is defined via the Dynamic element. Dynamic Elements Properties prepend = "where" is valid when returning "True" at a time decision element isnotnull. ISNOTNULL Element Properties Property = "ID" is used to determine if the general is null, if the general instance that contains the primary key value, the Dynamic element will be ineffective, but also, this is also, this will reach the mapping file The purpose of dynamic SQL is achieved. For details on a dollar determination element and binary determination element, please refer to the official document, if you can properly use these judgment elements, dynamic SQL will be more flexible. Corresponding program code:

Package test; import java.io.reader; import java.util.list; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class automag {Private Reader reader; private people people = new people (); private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public List findPeople () throws Exception {reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader) List list = SQLMap.QueryforList ("getPeople", null; Return list;}}

Package test; import java.io.reader; import java.util.list; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class automag {Private Reader reader; private people people = new people (); private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public List findPeople () throws Exception {reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader) List list = SQLMap.QueryforList ("getPeople", null; Return list;}}

Passing into the mapped statement ID, then passing an object instance that does not contain the primary key, you can simply pass NULL. Overload method SQLMap.QueryforList (String Arg0, Object Arg1, Int Arg2, int Arg3) is used for paging, arg2, and arg3 represent page number and per page, respectively.

Ok, use dynamic mapped statement, try queryforObject (string arg0, object arg1)! (Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

转载请注明原文地址:https://www.9cbs.com/read-66060.html

New Post(0)