第三章 动态SQL

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Mythology_px/article/details/82870971

         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();		
	}
}

猜你喜欢

转载自blog.csdn.net/Mythology_px/article/details/82870971