Multi-parameter query and list query are implemented in different ways
This document describes how to implement Mybatis multi-parameter query and list query in different ways.
Download the sample project in this article
1. query all data and return List
Query the list, that is, return List. In this example, list is returned. To return data in this way, you need to configure the returned type resultMap in Employeer. xml. Note that this is not the resultType, And the resultMap should be configured by ourselves.
<! -- Define the ing between database fields and object --> <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>
Id and result are the simplest mappings. id is the primary key ing. The ing between other basic database table fields of result to object-class attributes.
The statement for querying the list is in Employeer. xml.
<! -- Returns the select statement of the list, note that the resultMap value is directed to the previously defined --> <select id = "findEmployeerByName" parameterType = "string" resultMap = "resultMap"> select * from 't_ employeer 'where employeer_name like # {employeer_name} </select>
Add the method: public List to the EmployeerMapper interface. FindEmployeerByName (String employeer_name );
/*** Be sure to match the method name of Employeer. xml */public List
FindEmployeerByName (String employeer_name );
Test:
/*** Query List */public static List
GetEmployeerList (String employeer_name) {SqlSession session = null; List
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
Employeers = getEmployeerList ("Zhang San"); for (Employeer employeer: employeers) {System. out. println (employeer );}}
Result:
All the data inserted before is listed in the list of Michael Jacob.
Ii. multi-parameter query
(1) method 1
Definition in EmployeerMapper
/*** For multi-parameter queries, be sure to correspond to the method name of Employeer. xml */public List
FindEmployeerByNameandDep (String employeer_name, String employeer_department );
Defined in Employeer. xml
<! -- Define the ing between database fields and object --> <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 query returns the select statement of the list, note that the resultMap value is directed to the previously defined --> <select id = "findEmployeerByNameandDep" resultMap = "resultMap"> select * from't _ employeer 'where employeer_name = # {0} and employeer_department =#{ 1} </select>
Because there are multiple parameters, parameterType cannot be used. The # {index} is the first index, and the index starts from 0.
Test code:
/*** Multi-parameter query List */public static List
GetEmployeerList (String employeer_name, String employeer_department) {SqlSession session = null; List
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 ("================================ use multi-single-parameter query ======== ============================ "); list
Employeers1 = getEmployeerList ("Zhang San", "product 2"); for (Employeer employeer1: employeers1) {System. out. println (employeer1 );}
Result:
Method 2:
Change resultMap = "resultMap" to resultType = "Employeer"
<! -- Multi-parameter query returns the select statement of the list, note that the resultMap value is directed to the previously defined --> <select id = "findEmployeerByNameandDep" resultType = "Employeer"> select * from't _ employeer 'where employeer_name = # {0} and employeer_department =#{ 1} </select>
Method 3: Map encapsulates multiple parameters
Add in EmployeerMapper. java
/*** For multi-parameter queries, be sure to correspond to the method name of Employeer. xml */public List
FindEmployeerByNameandDep1 (Map
Map );
Add:
<! -- Select statement of the list returned by multi-parameter search. Note that the resultMap value points to the previously defined one, note that key1 and key2 are the input map key values --> <select id = "findEmployeerByNameandDep1" parameterType = "map" resultType = "Employeer"> select * from 't_ employeer 'where employeer_name =#{ key1} and employeer_department =#{ key2} </select>
Here, map is used directly after mybatis is configured. In map, the key name is the one used in.
Test use:
/*** Multi-parameter query List, using map */public static List
GetEmployeerList (Map
Map) {SqlSession session = null; List
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 ("============================================ ============================== "); map
Map = new HashMap
(); Map. put ("key1", ""); map. put ("key2", "Finance Department"); List
Employeers2 = getEmployeerList (map); for (Employeer employeer2: employeers2) {System. out. println (employeer2 );}
Note the key:
Result: