第三章 動態SQL

         MyBatis 的強大特性之一便是它的動態 SQL,使用動態SQL完成多條件查詢的功能

 

接口: IstudentMapper

package com.mapper;
 
import java.util.List;
import java.util.Map;

import com.model.Student;
 
/*
 *	數據庫方法接口----------->sql操作的映射文件
 */
public interface IStudentMapper {
	// 單條件查詢:按照名字name查詢,名字爲空則查詢全部,名字不爲空查詢具體信息:參數必須是key爲name的value
	// 多條件查詢:查詢name,sex的信息,指定name,sex的key
	public List<Student> findByName(Map<String,String> map);	
	
	// 多選擇查詢:當性別不爲空,查詢對應性別;性別爲空時查詢具體姓名
	public List<Student> findChoose(Map<String,String> map);
	
	// in查詢:查詢名字是.....等人的信息
	public List<Student> findStudents(List<String> list);
	
	// 模糊查詢:查詢名字中帶有o的學生信息
	public List<Student> likeName(Map<String,String> map);
	
	// 多條件查詢:根據輸入參數的學生對象,查詢具體信息
	public List<Student> findStudent(Student stu);
	
	public List<Student> findStudent2(Student stu);
}

接口配置文件:StudentMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  
 
<mapper namespace="com.mapper.IStudentMapper">
<!-- 接口對應的sql映射文件 :配置完接口映射文件之後需在mybatis配置文件進行註冊-->
 
	<!-- 執行查詢,將結果集數據存入集合中,需自定義返回結果集 -->
	<resultMap type="Student" id="StudentList">
		<id property="stuno" column="stuno" />
		<result property="sname" column="sname" />
		<result property="sex" column="sex" />
		<result property="bir" column="bir" />
		<result property="phone" column="phone" />
	</resultMap>
	
	<!-- 在各種標籤中的id屬性必須和接口中的方法名相同 , id屬性值必須是唯一的,不能夠重複使用 -->
	<!-- parameterType屬性指明查詢時使用的參數類型 -->
	<!-- resultType屬性指明查詢返回的結果值類型 -->
	<!--#{}中的內容,爲佔位符,當參數爲某個JavaBean時,表示放置該Bean對象的屬性值  -->
 
	<!-- 增,刪,改返回是影響的行數,不需要指定返回類型resultType-->
	<!-- if條件執行語句 -->
	<select id="findByName" parameterType="java.util.Map" resultMap="StudentList">
		select * from stu where 1=1
		<if test="name!=null and sex!=null">
			and sname=#{name} and sex=#{sex}
		</if>
		<if test="name!=null">
			and sname=#{name}
		</if>
	</select>
	
	<!-- choose選擇查詢 -->
	<select id="findChoose" parameterType="java.util.Map" resultMap="StudentList">
		select * from stu where
		<choose>
			<when test="sex==null">
				sname=#{name}
			</when>
			<otherwise>
				sex=#{sex}
			</otherwise>
		</choose>
	</select>
	
	<!-- foreach便利:in查詢 -->
	<select id="findStudents" parameterType="java.util.List" resultMap="StudentList">
		select * from stu where sname in
		<!-- 遍歷名字爲list的集合,將元素賦給name,index爲其下標,遍歷出來以'('開頭   中間以','分隔   以')'結尾-->
		<foreach item="name" index="index" collection="list" open="(" separator="," close=")">
			#{name}
		</foreach>
	</select>
	
	<!-- 模糊查詢:綁定% -->
	<select id="likeName" parameterType="java.lang.String" resultMap="StudentList">
		<!-- gex爲對象的屬性或Map的key: bind拼接 -->
		<bind name="like" value="'%'+gex+'%'"/>
		select * from stu where sname like #{like}
	</select>
	
	
	<!-- 條件查詢:trim截取不需要的部分 -->
	<select id="findStudent" parameterType="com.model.Student" resultMap="StudentList">
		select * from stu
		<trim prefix="where" prefixOverrides="and | or">
		<!-- if都不成立,則執行全部查詢,不加where;if中有一個成立,前置加where; -->
		<!-- 條件以and開頭,prefixOverrides會去除and-->
			<if test="sex!=null">
				and sex=#{sex}
			</if>
			<if test="bir!=null">
				and bir=#{bir}
			</if>
			<if test="phone!=null">
				and phone=#{phone}
			</if>
		</trim>
	</select>
	
	
	<!-- 條件查詢:trim截取不需要的部分 -->
	<select id="findStudent2" parameterType="com.model.Student" resultMap="StudentList">
		select * from stu
		<trim prefix="where" suffix=";" suffixOverrides="and | or">
		<!-- if都不成立,則執行全部查詢,不加where;if中有一個成立,前置加where; -->
		<!-- 當條件以and結尾,suffixOverrides會去除掉and;suffix在末尾添加分號 -->
			<if test="sex!=null">
				sex=#{sex} and
			</if>
			<if test="bir!=null">
				bir=#{bir} and
			</if>
			<if test="phone!=null">
				phone=#{phone} and
			</if>
		</trim>
	</select>
</mapper>

測試

package com.test;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mapper.IStudentMapper;
import com.model.Student;
import com.util.DBTools;
/*
 * 	測試
 * */
public class Test {
	public static void main(String[] args) throws IOException {
		// 創建數據庫對象
		DBTools db = new DBTools();
		// 獲取接口
		IStudentMapper ism =(IStudentMapper)db.getInterfaceSqlSession(IStudentMapper.class);
		
		// 單條件查詢:按照名字name查詢,名字爲空則查詢全部,名字不爲空查詢具體信息:參數必須是key爲name的value		
		Map<String ,String> map = new HashMap<String, String>();
		map.put("name", "Myth");		
		List<Student> list = ism.findByName(map);
		System.out.println("單條件查詢:查詢名字爲Myth的學生信息");
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("\n\n");
		
		
		// 多條件查詢:查詢name,sex的信息,指定name,sex的key
		map.clear();
		list.clear();
		map.put("name", "Micro");
		map.put("sex", "man");
		list = ism.findByName(map);
		System.out.println("多條件查詢:查詢名字爲Micro,且性別爲man的學生信息");
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("\n\n");

		
		// 選擇查詢:當性別不爲空,查詢對應性別;性別爲空時查詢具體姓名
		map.clear();
		list.clear();
		map.put("sex", "woman");
		map.put("name", "Micro");
		list = ism.findChoose(map);
		System.out.println("選擇條件查詢:查詢性別爲女的學生信息");
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("\n\n");
		
		
		// in查詢:查詢名字爲....等人的信息的
		list.clear();
		List<String> name = new ArrayList<String>();
		name.add("Micro");
		name.add("Myth");
		name.add("Rose");
		System.out.println("in查詢:查詢名字Micro,Myth,Rose等學生信息");
		list = ism.findStudents(name);
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("\n\n");
		
		
		// 模糊查詢:查詢名字中帶有o的學生信息
		map.clear();
		list.clear();
		map.put("gex","o");
		list = ism.likeName(map);
		System.out.println("模糊查詢:查詢名字中帶有o的學生信息");
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("\n\n");
		
		// 多條件查詢1,根據輸入的學生信息查詢
		list.clear();
		Student stu = new Student(-1, null, "man", null, "17742328212");
		list = ism.findStudent(stu);
		System.out.println("多條件查詢,信息越詳細,查詢結果越精準");
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("\n\n");
		
		// 多條件查詢2,根據輸入的學生信息查詢
		list.clear();
		stu = new Student(-1, null, "man", null, "17742328212");
		list = ism.findStudent2(stu);
		System.out.println("多條件查詢,信息越詳細,查詢結果越精準");
		for (Student student : list) {
			System.out.println(student);
		}
		System.out.println("\n\n");
		
		// 提交事務和關閉
		db.commit();
		db.close();		
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章