MyBatis_mapper代理和高级查询

5.mapper代理

前面使用mybatis 的JavaAPI完成的相关CRUD操作,下面看看开发中,在DAO层如何使用

  1. 编写mapper接口

  2. mapper.xml遵循如下约定:

    2.1. mapper.xml中namespace指定为mapper接口的全限定名

    2.2. mapper.xml中statement的id就是DAO接口中方法名

    2.3. mapper.xml中statement的parameterType和DAO接口中方法输入参数类型一致

    2.4. mapper.xml中statement的resultType和DAO接口中方法返回值类型一致.

  3. 访问mapper接口中的方法

5.2. 编写mapper接口

在这里插入图片描述

5.3. 修改UserMapper.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必须是接口全限定名-->
<mapper namespace="com.czxy.mybatis.mapper.UserMapper">

    <!--参数类型必须和接口方法参数类型一致,可以省略不写-->
    <!--返回值类型必须和接口方法参数类型一致-->
    <select id="listUser" parameterType="map" resultType="user">
        SELECT
          uid,
          username,
          birthday,
          phone,
          sex,
          address
        FROM `user`
        WHERE sex = #{sex}
        AND phone like #{phone}
    </select>
</mapper>

5.4. 测试

public class MybatisDaoTest {

    @Test
    public void testMapper(){
        //使用工具类获取sqlSession
        SqlSession sqlSession = MyBatisUtils.getSession();

        //查询参数
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("sex","1");
        map.put("phone","%"+6+"%");

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.listUser(map);

        for (User user : users) {
            System.out.println(user);
        }
        //关闭会话
        MyBatisUtils.closeSession();
    }
}

6. 高级查询

6.1. 模糊查询

  • 需求:查询姓名包含“张”的用户信息
  • mapper接口:UserMapper.java
public interface UserMapper {

    public List<User> listUser(Map<String,Object> map);
    //模糊查询
    public List<User> selectByPhone(String username);
}
  • UserMapper.xml
<!-- - 使用SQL元素定义SQL片段: 使用<include refid="片段ID"></include>引用-->

<sql id="baseColumn">
    uid,username,birthday,phone,sex,address
</sql>

<!--
  id:sql片段的唯一标识,同一个mapper文件中不能重复
  parameterType:参数类型
  resultType:返回值类型
  -->
  
<!--模糊查询-->
<select id="selectByPhone" resultType="user">
    select <include refid="baseColumn"></include>
    from `user`
    where username like #{name}
</select>
  • 测试
/**
     * 模糊查询
     */
@Test
public void selectByPhone(){
    //使用工具类获取sqlSession
    SqlSession sqlSession = MyBatisUtils.getSession();

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    String name="%张%";
    List<User> users =mapper.selectByPhone(name);

    for (User user : users) {
        System.out.println(user);
    }
    //关闭会话
    MyBatisUtils.closeSession();
}

6.2. 分组查询

需求: 统计男女各多少人

  • mapper接口
public interface UserMapper {
    //分组查询
    public List<Map<String,Integer>> selectCount(String columnName);
}
  • UserMapper.xml
<!--分组查询,注意取值使用的是${}-->
<select id="selectCount" resultType="map">
    select sex,count(sex) num from `user` group by ${column_name }
</select>
  • 测试
/**
  * 分组查询
  */
@Test
public void selectCount(){
    //使用工具类获取sqlSession
    SqlSession sqlSession = MyBatisUtils.getSession();

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List<Map<String, Integer>> listMap = (List<Map<String, Integer>>) mapper.selectCount("sex");
    //便利查询结果
    for (Map<String, Integer> map : listMap) {
        System.out.println(map);
    }
    //关闭会话
    MyBatisUtils.closeSession();
}

小技巧:自定义VO类,接收返回的数据

  • UserVO
public class UserVO {
    private String sex;
    private Long num;
    //get set toString()
}
  • UserMapper.xml
<!--分组查询,注意取值使用的是${}-->
<select id="selectCount2" resultType="com.czxy.mybatis.vo.UserVO">
    select sex,count(sex) num from `user` group by ${column_name }
</select>
  • UserMapper.java
public List<UserVO> selectCount2(String columnName);
  • 测试
/**
  * 分组查询:使用自定义vo类接收返回参数
  */
@Test
public void selectCount2(){
    //使用工具类获取sqlSession
    SqlSession sqlSession = MyBatisUtils.getSession();

    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List<UserVO> list = (List<UserVO>) mapper.selectCount2("sex");
    //便利查询结果
    for (UserVO vo : list) {
        System.out.println(vo);
    }
    //关闭会话
    MyBatisUtils.closeSession();
}

猜你喜欢

转载自blog.csdn.net/qq_44509920/article/details/107583468