MyBatis 动态SQL查询语句 (10)

MyBatis 动态SQL查询语句:根据传输参数进行判断是否作为查询条件

1、mapper.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.mybatis03.mapper.PersonMapper">
 <!-- 动态SQL语句 -->
    <select id="queryPersonByAgeAndNameWithSQLTag" parameterType="com.mybatis03.bean.Person" resultType="com.mybatis03.bean.Person">
        select
        id,name,age,sex
        from
        t_person_01
        where
        <if test="name !=null and name!='' ">
          name = #{name}
        </if>
        <if test="id !=null and id!='' ">
            and id = #{id}
        </if>
    </select>

</mapper>

2、测试类

 @Test
    public void queryPersonByAgeAndNameWithSQLTag() throws Exception{
        Reader reader = Resources.getResourceAsReader("mybatis-03.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

        SqlSession session = sessionFactory.openSession();
        Person person = new Person();
        person.setName("zhangsan");
        // 动态代理
        PersonMapper personMapper = session.getMapper(PersonMapper.class);
        List<Person> personList = personMapper.queryPersonByAgeAndNameWithSQLTag(person);
        System.out.println("动态SQL查询结果为:"+personList);
        session.close();
    }

3、mapper j接口类

/**
 * @author :jack.zhao
 * @Describe: 操作mybatis接口
 * @date :2021-10-16 22:55
 */
public interface PersonMapper {   
 // 动态SQL语句
    List<Person> queryPersonByAgeAndNameWithSQLTag(Person person);
}

4、实体类

package com.mybatis03.bean;

/**
 * @author :jack.zhao
 * @Describe: 实体类
 * @date :2021-10-16 22:55
 */
public class  Person {

    private int id;
    private String name;
    private int age;
    private Boolean sex;
    private Address address;
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public Person() {
    }
    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Person(int id, String name, int age, Boolean sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    public Boolean getSex() {
        return sex;
    }
    public void setSex(Boolean sex) {
        this.sex = sex;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", address=" + address +
                '}';
    }
}

5、pom文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis01"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- java.zhao 加载映射文件 -->
        <mapper resource="com/mybatis03/mapper/personMapper.xml"/>
    </mappers>
</configuration>

【测试数据】

 

 【测试结果】:

 

posted @ 2021-10-18 20:22  东北大亨  阅读(313)  评论(0)    收藏  举报