21、SSM框架-Mybatis多引數查詢與列表查詢不同方式實現(3)
阿新 • • 發佈:2019-02-13
本文要實現Mybatis多引數查詢與列表查詢不同方式實現,以一個例項來說明。
一、查詢所有資料,返回List
查詢出列表,也就是返回list, 在我們這個例子中也就是 List<Employeer> , 這種方式返回資料,需要在Employeer.xml 裡面配置返回的型別 resultMap, 注意不是 resultType, 而這個resultMap 所對應的應該是我們自己配置的
- <!-- 定義資料庫欄位與實體物件的對映關係 -->
- <resultMap type="Employeer"
- <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、result是最簡單的對映,id為主鍵對映;result其他基本資料庫表字段到實體類屬性的對映。<!-- 定義資料庫欄位與實體物件的對映關係 --> <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>
查詢列表的語句在 Employeer.xml 中
- <!-- 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的 -->
- <select id="findEmployeerByName" parameterType="string" resultMap="resultMap">
- select * from `t_employeer` where employeer_name like #{employeer_name}
- </select>
<!-- 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的 -->
<select id="findEmployeerByName" parameterType="string" resultMap="resultMap">
select * from `t_employeer` where employeer_name like #{employeer_name}
</select>
在EmployeerMapper 介面中增加方法:public List<Employeer> findEmployeerByName(String employeer_name);
[java] view plain copy print?
- /**
- * 注意要和Employeer.xml的方法名對應
- */
- public List<Employeer> findEmployeerByName(String employeer_name);
/**
* 注意要和Employeer.xml的方法名對應
*/
public List<Employeer> findEmployeerByName(String employeer_name);
測試: [java] view plain copy print?
- /**
- * 查詢列表
- */
- 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("張三");
- for(Employeer employeer:employeers){
- System.out.println(employeer);
- }
- }
/**
* 查詢列表
*/
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("張三");
for(Employeer employeer:employeers){
System.out.println(employeer);
}
}
結果:
其中的資料的之前插入的,現在要查詢張三的列表全部都列出來了。
二、多引數查詢
(1)方法一
EmployeerMapper中定義
- /**
- * 多引數查詢,注意要和Employeer.xml的方法名對應
- */
- public List<Employeer> findEmployeerByNameandDep(String employeer_name,String employeer_department);
/**
* 多引數查詢,注意要和Employeer.xml的方法名對應
*/
public List<Employeer> findEmployeerByNameandDep(String employeer_name,String employeer_department);
Employeer.xml中定義
[html]
view plain
copy
print?
- <!-- 定義資料庫欄位與實體物件的對映關係 -->
- <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>
<!-- 定義資料庫欄位與實體物件的對映關係 -->
<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>
[java]
view plain
copy
print?
- <!--多引數 查詢 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的 -->
- <select id="findEmployeerByNameandDep" resultMap="resultMap">
- select* from `t_employeer` where employeer_name=#{0} and employeer_department=#{1}
- </select>
<!--多引數 查詢 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的 -->
<select id="findEmployeerByNameandDep" resultMap="resultMap">
select* from `t_employeer` where employeer_name=#{0} and employeer_department=#{1}
</select>
由於是多引數那麼就不能使用parameterType, 改用#{index}是第幾個就用第幾個的索引,索引從0開始
測試程式碼:
- /**
- * 多引數查詢列表
- */
- 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 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;
}
[java] view plain copy print?
- public static void main(String[] args) {
- System.out.println("=========================使用多單引數查詢===========================");
- List<Employeer> employeers1=getEmployeerList("張三","產品二部");
- for(Employeer employeer1:employeers1){
- System.out.println(employeer1);
- }
public static void main(String[] args) {
System.out.println("=========================使用多單引數查詢===========================");
List<Employeer> employeers1=getEmployeerList("張三","產品二部");
for(Employeer employeer1:employeers1){
System.out.println(employeer1);
}
結果:
方法二:
將 resultMap="resultMap" 改成 resultType="Employeer"
- <!--多引數 查詢 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的 -->
- <select id="findEmployeerByNameandDep" resultType="Employeer">
- select* from `t_employeer` where employeer_name=#{0} and employeer_department=#{1}
- </select>
<!--多引數 查詢 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的 -->
<select id="findEmployeerByNameandDep" resultType="Employeer">
select* from `t_employeer` where employeer_name=#{0} and employeer_department=#{1}
</select>
方法三:Map封裝多引數EmployeerMapper.java中新增
[java] view plain copy print?
- /**
- * 多引數查詢,注意要和Employeer.xml的方法名對應
- */
- public List<Employeer> findEmployeerByNameandDep1(Map<String,String> map);
/**
* 多引數查詢,注意要和Employeer.xml的方法名對應
*/
public List<Employeer> findEmployeerByNameandDep1(Map<String,String> map);
Employeer.xml中新增:
[html]
view plain
copy
print?
- <!--多引數 查詢 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的,注意key1和key2為傳入的map的鍵值 -->
- <select id="findEmployeerByNameandDep1" parameterType="map" resultType="Employeer">
- select* from `t_employeer` where employeer_name=#{key1} and employeer_department=#{key2}
- </select>
<!--多引數 查詢 返回list 的select 語句,注意 resultMap 的值是指向前面定義好的,注意key1和key2為傳入的map的鍵值 -->
<select id="findEmployeerByNameandDep1" parameterType="map" resultType="Employeer">
select* from `t_employeer` where employeer_name=#{key1} and employeer_department=#{key2}
</select>
其中map是mybatis自己配置好的直接使用就行。map中key的名字是那個就在#{}使用那個。
測試使用:
[java] view plain copy print?- /**
- * 多引數查詢列表,使用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;
- }
/**
* 多引數查詢列表,使用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;
}
[java] view plain copy print?
- System.out.println("=========================使用多單引數map方式查詢===========================");
- Map<String, String> map = new HashMap<String, String>();
- map.put("key1", "明明");
- map.put("key2", "財會部");
- List<Employeer> employeers2=getEmployeerList(map);
- for(Employeer employeer2:employeers2){
- System.out.println(employeer2);
- }
System.out.println("=========================使用多單引數map方式查詢===========================");
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "明明");
map.put("key2", "財會部");
List<Employeer> employeers2=getEmployeerList(map);
for(Employeer employeer2:employeers2){
System.out.println(employeer2);
}
一定要注意key:
結果: