20191231-- output mapping

resultType

Use resulttype output mapping, consistent only check out the column names and attribute names pojo in this column can successfully map

If you check out the column names and attributes in all pojo inconsistent, then there is no pojo create objects
only check out of a consistent, it will create objects out of a pojo

You can also print a simple type of
demand:
the total number of list of user information integrated query before they can implement paging by the total number of queries and a list of the top queries users.

mapper.xml given .java

@Test
    public void testFindUserCount() throws  Exception{
        //创建usermappper对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建mapper对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用usermapper方法
        UserQueryVo userQueryVo =  new UserQueryVo();
        UserCustom userCustom = new UserCustom();
        userCustom.setSex("1");
        userCustom.setUsername("张三丰");
        userQueryVo.setUserCustom(userCustom);
        int count  = userMapper.findUserCount(userQueryVo);
        System.out.println(count);

    }
 <select id="findUserCount" parameterType="cn.itcast.mybatis.pojo.UserQueryVo" resultType="int">
        select count(*)  from user where user.sex = #{userCustom.sex} and user.username like '%${userCustom.username}%'
    </select>

Summary
check out the results of only one row or one can use a simple type

Pojo and output objects list pojo
whether pojo single object or multiple objects output a list (list including pojo), resultType mapper.xml specified in the same type of
the type specified mapper.java return value is not the same

If a single object, the return value is a single object
if a plurality of object list pojo, is returned List, which is pojo

resultmap
the mybatis use resultmap meet the advanced output mapping

If we say check out the column name and attribute name pojo inconsistent, you can define a resultmap do a mapping relation between the column name and attribute names

Defined resultmap
used as the output mapping type resultmap

Demand
following sql mapping is done using UserCustom

 <resultMap type="user" id="userResultMap">
        <id column="id_" property="id"></id>
        <result column="username_" property="username"></result>
    </resultMap>
    <select id="findUserByResultMap" parameterType="int" resultMap="userResultMap">
        select id id_,username username_ from user where id = #{value}
    </select>

test

public void testFindUseByResultMap() throws  Exception{
        //创建usermappper对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建mapper对象
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用usermapper方法
        User user = userMapper.findUserByResultMap(1);
        System.out.println(user);
    }
Published 658 original articles · won praise 39 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/103776248