Mybatis映射文件——ParameterType

输入映射ParameterType

指定输入参数的java类型,可以使用别名或者类的全限定名。它可以接受简单类型、pojo对象、HashMap

传递简单类型

在这里插入图片描述

传入POJO类型

在这里插入图片描述

传入POJO包装对象:传递比较复杂的信息

传入map对象

1接口中声明方法:

   public List<User> findUserByMap(Map<String,Object> map);

2映射文件修改

<!--//通过Map查询-->
    <select id="findUserByMap" parameterType="hashmap" resultType="user">
        select * from user u where username like '%${username}%' and sex=#{sex};
    </select>

3测试

 /*
     * 多条件查询
     * */
    @Test
    public void test2() throws IOException {
        UserMapper userMapper=session.getMapper(UserMapper.class);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("username","张");
        map.put("sex","1");
        List<User> list=userMapper.findUserByMap(map);
        System.out.println(list);

    }

输出映射resultType/resultMap

resultType:使用resultType进行结果映射时,查询的列名和映射的pojo属性名完全一致,该列才能映射成功。如果查询的列名和映射的pojo属性名全部不一致,则不会创建pojo对象,如果查询的列名和映射的pojo属性有一个不一致,就会创建pojo对象。

输出简单类型

当输出结果只有一列时,可以使用ResultType指定简单类型作为输出结果类型。
1、UserMapper中创建一个借口,查询用户人数

 /**
     * 返回用户个数
     * @param vo
     * @return
     */
    public int findUserCount(UserQuerVO vo);

2、UserMapper.xml中设置方法

 <!--查找用户的个数-->
    <!-- 设置返回数据为基本数据类型-->
    <select id="findUserCount" parameterType="UserQuerVO" resultType="int">
        select  count(*) from user  where  sex=#{user.sex};
    </select>

3、测试

public class Demo6 {

    SqlSession session;
   @Before
    public void before() throws IOException {
       System.out.println("before......获取session");
//      *  a)	读取配置文件;
       InputStream is= Resources.getResourceAsStream("SqlMapConfig.xml");
//        *  b)	通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂。
        SqlSessionFactory factory= new SqlSessionFactoryBuilder().build(is);
        session=factory.openSession();
    }
    @After
    public void after(){
       session.close();
    }

    /**
     * 查询该性别的人数
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        UserMapper userMapper=session.getMapper(UserMapper.class);
       //通过模型的包装类来查询用户
        UserQuerVO query=new UserQuerVO();

        User user=new User();
        user.setSex("1");
        query.setUser(user);
        int count=userMapper.findUserCount(query);
        System.out.println("人数为"+count);
    }


}

ResultMap

如果查询出来的列名和属性名不一致,通过定义一个resultMap将列名和pojo属性名之间做一个映射关系
1、定义resultMap

/**
     * 用resultMap类型为返回值类型
     * @param UserId
     * @return
     */
    public User findUserByIdResultMap(int UserId);

2、使用resultMap作为statement的输出映射类型。

 <!--设置返回数据类型为resultMap-->
    <resultMap id="userResultMap" type="user">
        <id property="id" column="id_"></id>
        <result property="username" column="username_"></result>
        <result property="sex" column="sex_"></result>
        <result property="birthday" column="birthday_"></result>
        <result property="address" column="address_"></result>

    </resultMap>
    <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
      select  id ,username username_,
      sex sex_,birthday birthday_,
      address address_
      from user where id=#{id};
    </select>

3、测试

/**
     * 结果类型为resultMap
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        UserMapper userMapper=session.getMapper(UserMapper.class);

        User user=userMapper.findUserByIdResultMap(10);
        System.out.println("查询结果"+user);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值