0
点赞
收藏
分享

微信扫一扫

Mybaitis案例二:员工信息查询系统(动态SQL)

哈哈我是你爹呀 2022-02-25 阅读 54

在数据库建立一个employee表,实现以下功能:(基于案例一)

1.实现多条件查询,根据员工的姓名和年龄筛选
(1)当用户输入姓名不为空,年龄为空时,则只根据姓名查询;
(2)当用户输入年龄不为空,姓名为空时,则只根据年龄查询;
(3)当用户输入年龄和姓名都不为空时,根据年龄姓名同时查询;

(4)当用户输入年龄和姓名都为空时,则查询出所有职位不为空的的员工信息;
2.查询id=1,2,5,6的员工信息

3.单条件查询出所有id值小于5的员工的信息;

4.查询出年龄为26且职位为员工的员工信息;

代码实现:

使用了MyBatis动态SQL中的常用元素<if>、<choose>、<when>、<otherwise>、<foreach>

1.EmployeeMapper.xml文件


    <!--根据年龄和姓名查询-->
    <select id="findByNameOrAge" parameterType="org.example.pojo.Employee" resultType="org.example.pojo.Employee">
        select * from employee where 1 = 1
        <!--1.(3)当年龄姓名都不为空时,根据年龄姓名同时查询-->
        <if test="name != null and name != '' and age != null and age != ''">
            <!--当写成“and name = #{name} or age = #{age}”时,只能查询到姓名符合的信息,调换位置后才能同时查询出符合信息-->
            and age = #{age} or name = #{name}
        </if>
        <!--1.多条件查询-->
        <choose>
            <when test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </when>
            <when test="age != null and age != ''">
                and age = #{age}
            </when>
            <otherwise>
                and position is not null
            </otherwise>
        </choose>
    </select>
    <!--2.查询id=1,2,5,6的员工信息-->
    <select id="findByArray" parameterType="java.util.Arrays" resultType="org.example.pojo.Employee">
        select * from employee where id in
        <foreach collection="array" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <!--3.查询id小于5的员工信息-->
    <select id="findByList" parameterType="java.util.List" resultType="org.example.pojo.Employee">
        select * from employee where id in
        <foreach collection="list" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <!--4.查询出年龄为26且职位为员工的员工信息-->
    <select id="findByMap" parameterType="java.util.Map" resultType="org.example.pojo.Employee">
        select * from employee where age = #{age} and position = #{position} and id in
        <foreach collection="id" item="roleMap" index="index" open="(" separator="," close=")">
            #{roleMap}
        </foreach>
    </select>

2.测试类findTest

public class findTest {
    SqlSession sqlSession = MyBatisUtils.getSession();
    @Test
    public void findByNameOrAge(){
        Employee employee = new Employee();
        employee.setName("张三");
        employee.setAge(22);
        List<Employee> list = sqlSession.selectList("findByNameOrAge",employee);
        for (Employee employee1:list){
            System.out.println(employee1);
        }
        sqlSession.close();
    }
    @Test
    public void findByArray(){
        Integer[] ids = {1,2,5,6};
        List<Employee> list = sqlSession.selectList("findByArray",ids);
        for (Employee employee:list){
            System.out.println(employee);
        }
        //找不到的时候呢?
        sqlSession.close();
    }
    @Test
    public void findByList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0;i < 5;i++){
            list.add(i);
        }
        List<Employee> employees = sqlSession.selectList("findByList",list);
        for (Employee employee:employees){
            System.out.println(employee);
        }
        sqlSession.close();
    }
    @Test
    public void findByMap(){
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(5);
        list.add(6);
        Map<String ,Object> map = new HashMap<>();
        map.put("id",list);
        map.put("age","26");
        map.put("position","员工");
        List<Employee> employees = sqlSession.selectList("findByMap",map);
        for (Employee employee:employees){
            System.out.println(employee);
        }
        sqlSession.close();
    }
}
举报

相关推荐

0 条评论