mybatis基础知识(四)&输入映射与输出映射

mapper映射

1.1通过resource加载单个映射文件
<!--通过resource方法一次加载一个映射文件-->
<mapper resource="mapper/UserMapper.xml"/>

1.2通过mapper接口加载
<!-- 通过mapper接口加载映射文件
遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致并且在同一发个目录下
上边规范的前提是:使用mapper代理方法
-->
<mapper class="mybaties.mapperUserMapper"/>
如
    UserMapper.java
    UserMapper.xml
1.3批量加载mapper(推荐)
<!-- 批量加载mapper
指定mapper接口的包名,mybatis自动扫描包下的边所有的mapper接口进行加载
朱迅一些规范:需要将mapper接口和mapper.xml映射文件保持一致,且放在同一个目录中
上边的规范的前提是:使用是mapper代理的方法
-->
<package name="mybaties.mapper"/>

Mapper映射关系

2.输入映射

通过parameterType指定输入参数的类型,类型可以是简单类型,hashmap, 
2.1传递pojo的包装对象
2.1.1需求
完成用户信息的综合查询,需要传入的条件很复杂(可能包括用户信息,其他信息,比如商品,订单的)
2.1.2定义包装类类型pojo
针对上边需求,建议使用自定义的包装类型的pojo
在包装类型的pojo中将其复杂的查询条件包装进去
public class UserQueryVo【
    //在这里包装类所需要的查询条件
    //用户查询条件
    private UserCustom userCustom;
    public UserCustom getUserCustom(){
        return UserCustom;
    }
    public void setUserCustom(UserCustom userCustom){\
        this.userCustom=userCustom;
    }
}

2.1.3映射文件
mapperxml

<!--用户综合信息查询
#{userCustome。sex}取出pojo包装对象中的性别值
${userCustome.username}:取出pojo包装对象中的用户名称
-->
<select id="findUserList" parameterType="mybaties.po.UserQueryVo"
    resultType="mybaties.po.UserCustom">
    select * from user where user.sex=#{userCustom.sex} and user.username like "%${userCustom.username}%"
    </select>

2.1.4
    mapper.java
    //用户综合查询
    public List<UsreCustom> findUserList(UserQueryVo userQueryVo) throws Exception;
2.1.5测试
    public void testFindUserList() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //创建UserMapper对象,mybatis自动生成mapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        //创建包装对象,设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        userCustom.setSex("1");
        userCustom.setUsername("l李四");
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper的方法
        List<UserCustom> list-=userMapper.findUserList(userQueryVo);
        System.out.print(list);l

    2.2 hashMap类型,在使用hashMap时需要注意,查询的条件比如说ID和性别是作为key值放在hashMap中,value值是查询的具体条件,比如:

        <select id="findUserByHashMap" parameterType="hashMap" resultType="user"> 
            select * from user
            where user.sex = #{sex} AND user.username = #{username}
        </select>       

3.输出映射

3.1resultType
    使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,该列才可以映射成功
    如果查询出来的列名和pojo中的属性名全部不一致。没有创建pojo对象
    只要查询出来的列名和pojo中的属性有一个一致,就会创建pojo对象

    3.1.1 输出简单类型
        3.1.1.1需求
        用户信息的综合查询列表总数,通过查询总数和上边的用户综合查询列表才可以实现分页
        3.1..1..2mapper.xml
        <!--用户综合信息查询
            #{userCustome。sex}取出pojo包装对象中的性别值
            ${userCustome.username}:取出pojo包装对象中的用户名称
            -->
            <select id="findUserList" parameterType="mybaties.po.UserQueryVo"
                resultType="mybaties.po.UserCustom">
                select count(*) from user where user.sex=#{userCustom.sex} and user.username like "%${userCustom.username}%"
        </select>       
3.1.1.3mapper.java
    public int findUserCount(UserQueryVo userQueryVo) throws Exception;             
3.1.1.4测试
    public void testFindUserCount() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //创建UserMapper对象,mybatis自动生成mapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        //创建包装对象,设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        userCustom.setSex("1");
        userCustom.setUsername("l李四");
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper的方法
        int  count-=userMapper.findUserCount(userQueryVo);
        System.out.print(lcount;l

小结
查询出来的结果集只有一行且一列,可以使用简单类型进行输出映射

3.1.2输出pojo对象和pojo列表
    不管是输出pojo单个对象还是一个列表(list中包括pojo),在mapper。xml中resultType指定
    的类型是一样的
    在mapper.java指定的方法的返回值类型是不一样
    1.输出单个pojo对象。方法,返回值是单个对象类型
    public User findUserById(int id) throws Exception;
    2.输出pojo对象list,方法返回值是List<pojo>
    public List<User> findUserByName(String name) throws Exception;

    生成的动态代理对象中是根据mapper方法的返回值类型确定是调用selectOne(返回单个对象)还是selectList(
    返回集合对象调用)
3.2.1mapper.xml
    <mapper namespace="com.dy.mapper.UserMapper">
            <!-- 使用resultMap定义别名进行匹配查询 
            如果这个resultMap在其它的mapper文件中,前边需要加namespace
            -->
            <resultMap type="user" id="userResultMap">
            <!-- id 表示查询结果集中唯一标识
            column: 查询出来的列名
            property:type所指定的pojo中的属性名
            -->
            <id column="id_" property="id"/>
            <!-- result 对普通名映射定义
            column: 查询出来的列名
            property:type所指定的pojo中的属性名
            最终resultMap对column和property作一个映射关系
             -->
            <result column="username_" property="username"/>
            </resultMap>
    3.2.2四通resultMap作为statement的输出映射类型
    <!--使用resultMa进行输出映射
    resultMap 输出指定的resultMap 如果这个resultMap在其他的mapper文件中,前面需要加namespace
    -->
    <select id="" parameterType=’int“ returnlMap="userResultMap">
        select id id_,username username_  from user where id=#{vaule}
    </select>
    3.2.3mapper.java
        public User  findUserByResultMap(int id) throws Exception;
    3.2.3 测试
        public void testFindUserByResultMap() throws Exception{
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //创建UserMapper对象,mybatis自动生成mapper代理对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        User user-=userMapper.findUserByResuyltMap(1);
        System.out.print(luser);

小结

    使用resultType进行输出映射,只要查询出来的列名和pojo中的属性名一致,该列才可以映射成功
    如果查询出来的列名和pojo的属性名不一致,通过定义一个mapper对列名和pojo属性名之间做
    一个映射关系
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值