Mybatis动态sql-根据姓名和职业动态查询客户信息

JAVAEE实践系列

1. Spring入门程序
2. 依赖注入实现方法
3. Bean基于注解方式的装配
4. Mybatis入门程序-根据id查询数据
5. Mybatis入门程序-添加,根据id更新和删除客户信息
6. Mybatis入门程序-根据客户名模糊查询客户信息
6. Mybatis动态sql-根据姓名和职业动态查询客户信息


概括

本文包括以下内容:
按照查询的代码对映射文件和测试文件进行修改,实现 动态sql-根据姓名和职业动态查询客户信息


说明

需要5个文件,这里只需修改映射文件和测试文件的代码,其余的代码在本系列之前的博客:Mybatis入门程序中


一、步骤及截图

①. 修改映射文件中的内容

方法一:where 1=1

在这里插入图片描述

方法二:where元素替代

在这里插入图片描述

方法三:trim元素替代

在这里插入图片描述

②. 修改测试文件中的内容

在这里插入图片描述

四、输出结果

数据库:
在这里插入图片描述
查询结果:

在这里插入图片描述

五、整体代码

目录结构

在这里插入图片描述

1. 映射文件:Mapper包下CustomerMapper.xml

方法一:where 1=1

<?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.itheima.mapper.CustomerMapper">
	<select id="findCustomerByNameAndJobs" parameterType="com.itheima.po.Customer" resultType="com.itheima.po.Customer">
 		select * from t_costomer where 1=1
 		<if test="username !=null and username !=''">
 			and username like '%${username}%'
 		</if>
 		<if test="jobs !=null and jobs !=''">
 			and jobs like '%${username}%'
 		</if>
	</select>
</mapper>

方法二:where元素替代

<?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.itheima.mapper.CustomerMapper">
	<select id="findCustomerByNameAndJobs" parameterType="com.itheima.po.Customer" resultType="com.itheima.po.Customer">
 		select * from t_costomer 
 		<where>
 			<if test="username !=null and username !=''">
 				and username like '%${username}%'
 			</if>
 			<if test="jobs !=null and jobs !=''">
 				and jobs like '%${jobs}%'
 			</if>
 		</where> 
	</select>
</mapper>

方法三:trim元素替代

<?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.itheima.mapper.CustomerMapper">
	<select id="findCustomerByNameAndJobs" parameterType="com.itheima.po.Customer" resultType="com.itheima.po.Customer">
 		select * from t_costomer 
 		<!-- trim元素作用是去除一些特殊的字符串 -->
 		<!-- prefix代表语句前缀 prefixOverrides代表需要去除的特殊字符串 -->
 		<trim prefix="where" prefixOverrides="and">
 			<if test="username !=null and username !=''">
 				and username like '%${username}%'
 			</if>
 			<if test="jobs !=null and jobs !=''">
 				and jobs like '%${jobs}%'
 			</if>
 		</trim>
	</select>
</mapper>

2. 测试类:Test包下MyBatisTest.java

package com.ithiema.test;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.itheima.po.Customer;

public class MyBatisTest {
	/*
	 * 根据客户名字模糊查询客户信息
	 * */
	@Test
	public void findCustomerByNameAndJobsTest() throws Exception{
		//1. 读取核心配置文件
		String resource = "mybatis-config.xml";
		//通过输入流读取配置文件
		InputStream inputStream = Resources.getResourceAsStream(resource);
		//2. 根据配置文件构建sqlSessionFactory;
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		//3. 通过sqlSessionFactory创建sqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//4. 创建customer对象,封装需要组合查询的条件
		Customer customer = new Customer();
		customer.setUsername("高瘦子");
		customer.setJobs("student");
		//5. sqlSession执行映射文件中定义的sql,并返回映射结果
		List<Customer> customers = sqlSession.selectList("com.itheima.mapper"+".CustomerMapper.findCustomerByNameAndJobs",customer);
		//打印输出结果
		for(Customer customer2 : customers) {
			System.out.println(customer2);
		}
		//6. 关闭sqlSession 
		sqlSession.close();
	}
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值