Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
This article is to realize mybatis multi-parameter query and list query different ways to implement, with an example to illustrate.
This sample project download
One, query all data, return to list
Query out the list, that is, to return to list, in our example is list<employeer>, this way to return the data, you need to configure Employeer.xml inside the returned type Resultmap, note is not resulttype, And this resultmap is supposed to be our own configuration.
<!--define the mapping of database fields to entity objects--
<resultmap type= "Employeer" id= "Resultmap" >
<id property= " employeer_id "column=" employeer_id "/>
<result property=" Employeer_name "column=" Employeer_name "/>
<result property= "employeer_age" column= "Employeer_age"/> <result
property= "Employeer_ Department "column=" employeer_department "/>
<result property=" Employeer_worktype "column=" Employeer_ Worktype "/>
</resultMap>
The ID, result is the simplest mapping, the ID is the primary key mapping, and the result is the mapping of other basic database table fields to entity class properties.
The statement of the query list is in Employeer.xml
<!--Returns the SELECT statement for list, note that the value of Resultmap is pointed to the previously defined--
<select id= "Findemployeerbyname" parametertype= " String "resultmap=" Resultmap ">
select * from ' T_employeer ' where Employeer_name like #{employeer_name}
< /select>
Add method in Employeermapper interface: public list<employeer> findemployeerbyname (String employeer_name);
/**
* Note the method name corresponding to the employeer.xml */public
list<employeer> findemployeerbyname (String employeer_ name);
Test:
/**
* Query List
*
/public static list<employeer> getemployeerlist (String employeer_name) {
sqlsession session = NULL;
List<employeer> Employeers=null;
try {
session = Sqlsessionfactory.opensession ();
Employeermapper Employeermapper=session.getmapper (employeermapper.class);
Employeers = Employeermapper.findemployeerbyname (employeer_name);
Session.commit ();
} finally {
session.close ();
}
return employeers;
}
public static void Main (string[] args) {
list<employeer> employeers=getemployeerlist ("Zhang San");
for (Employeer employeer:employeers) {
System.out.println (employeer);
}
}
Results:
The list of data that was inserted before, now to find Zhang San, is all listed.
Two, multi-parameter query
(1) Method one
Defined in Employeermapper
/**
* Multi-parameter query, note to the Employeer.xml method name corresponds to */public
list<employeer> FINDEMPLOYEERBYNAMEANDDEP (String Employeer_name,string employeer_department);
Defined in Employeer.xml
<!--define the mapping of database fields to entity objects--
<resultmap type= "Employeer" id= "Resultmap" >
<id property= " employeer_id "column=" employeer_id "/>
<result property=" Employeer_name "column=" Employeer_name "/>
<result property= "employeer_age" column= "Employeer_age"/> <result
property= "Employeer_ Department "column=" employeer_department "/>
<result property=" Employeer_worktype "column=" Employeer_ Worktype "/>
</resultMap>
<!--multi-parameter lookup returns the SELECT statement for list, note that the value of Resultmap is pointed to the previously defined--
<select id= "FINDEMPLOYEERBYNAMEANDDEP" resultmap= "Resultmap" >
select* from ' T_employeer ' where employeer_name=#{0} and Employeer_department=#{1}
because it is a multi-parameter then you can not use ParameterType, #{index} is the first number of the index, the index starting from 0
Test code:
/**
* Multi-parameter query list *
/public static list<employeer> getemployeerlist (String employeer_name,string employeer_department) {
sqlsession session = NULL;
List<employeer> Employeers=null;
try {
session = Sqlsessionfactory.opensession ();
Employeermapper Employeermapper=session.getmapper (employeermapper.class);
Employeers = EMPLOYEERMAPPER.FINDEMPLOYEERBYNAMEANDDEP (Employeer_name, employeer_department);
Session.commit ();
} finally {
session.close ();
}
return employeers;
}
public static void Main (string[] args) {
System.out.println ("========================= using multiple single parameter queries ================ ===========");
List<employeer> employeers1=getemployeerlist ("Zhang San", "Product Two");
for (Employeer employeer1:employeers1) {
System.out.println (employeer1);
}
Results:
Method Two:
Change resultmap= "Resultmap" to Resulttype= "Employeer"
<!--multi-parameter lookup returns the SELECT statement for list, note that the value of Resultmap is pointed to the previously defined--
<select id= "FINDEMPLOYEERBYNAMEANDDEP" Resulttype= "Employeer" >
select* from ' T_employeer ' where employeer_name=#{0} and Employeer_department=#{1}
Method Three: Map package multi-parameter
Add in Employeermapper.java
/**
* Multi-parameter query, note to and Employeer.xml method name corresponds to */public
list<employeer> FindEmployeerByNameandDep1 (Map <String,String> map);
Added in Employeer.xml:
<!--multi-parameter lookup returns the SELECT statement for list, note that the value of Resultmap is pointed to the previous definition, and note that Key1 and Key2 are key values for the incoming map--
<select id= " FindEmployeerByNameandDep1 "parametertype=" map "resulttype=" Employeer ">
select* from ' T_employeer ' where Employeer_name=#{key1} and Employeer_department=#{key2}
Where map is MyBatis's own configuration of direct use on the line. The name of key in map is the one that is used in #{}.
Test using:
/**
* Multi-parameter query list, using MAP
*
/public static list<employeer> getemployeerlist (map<string, string> Map) {
sqlsession session = NULL;
List<employeer> Employeers=null;
try {
session = Sqlsessionfactory.opensession ();
Employeermapper Employeermapper=session.getmapper (employeermapper.class);
Employeers = EMPLOYEERMAPPER.FINDEMPLOYEERBYNAMEANDDEP1 (map);
Session.commit ();
} finally {
session.close ();
}
return employeers;
}
System.out.println ("========================= using multiple single parameter map mode query ===========================");
map<string, string> map = new hashmap<string, string> ();
Map.put ("Key1", "Ming");
Map.put ("Key2", "accounting department");
List<employeer> employeers2=getemployeerlist (map);
for (Employeer employeer2:employeers2) {
System.out.println (employeer2);
}
Be sure to note the key:
Results:
This sample project download
Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka