1. 程式人生 > >第三章 動態SQL

第三章 動態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();		
	}
}