JAVAEE实践系列
1. Spring入门程序2. 依赖注入实现方法
3. Bean基于注解方式的装配
4. Mybatis入门程序-根据id查询数据
5. Mybatis入门程序-添加,根据id更新和删除客户信息
6. Mybatis入门程序-根据客户名模糊查询客户信息
6. Mybatis动态sql-根据姓名和职业动态查询客户信息
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();
}
}