ResultMap 动态sql语句

学习目标

  • ResultMap
    查询数据封装的原理
    字段名和成员名不一致
    ResultMap标签
  • 动态sql
    动态sql-if标签
    动态sql-foreach标签
  • 关联查询
    关联查询-一对一
    关联查询-一对多
    关联查询-综合案例
  • (1)字段名和成员名称不一致,为什么id属性能被Mybatis赋值?
    》底层使用id生成setId
    》使用反射调用setId方法,setId(10) 方法内完成赋值 id = 10
  • (2)为什么user_id属性能被Mybatis赋值?
    》底层使用user_id生成setUser_id
    》使用反射调用setUser_id方法,类中只有setUserId
  • (3)所以 为什么强调 表与类的对应关系?
    字段名和成员名要一致

ResultMap

  • ResultMap的作用
    1)建立查询字段与实体类成员变量的映射关系
    查询字段是user_id但是Mybatis去赋值userId
    2)字段名与变量名不一致,可以赋值
    3)实现一对多,多对多的查询
  • ResultMap标签映射关系,result Map使用

动态标签

  • 动态标签是什么
    由Mybatis将sql和Java代码分离(sql写在xml文件中)
    if标签 where标签 forEach标签
  • 动态标签的作用
    用来根据数据的不同来生成对应的sql语句
  • 应用场景
    高级搜索功能
    搜索有多个条件时,并不是每个条件输入框都有值,所以需要用where语句来生成条件

动态sql-if标签与where标签

  • if标签
    可以判断传入的参数是否为空,如果不为空则拼接sql
  • where标签
    写了where标签之后
    可以不用在初始sql后面写where 1=1
    不用在第一个拼接的sql前写and,也可以手动写
    UserDao.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="com.wxx.dao.UserDao">

    <select id="findByUser" resultType="user">
        select * from `user`
        <where>
            <if test="username != '' and username != null">
                and username like '${username}'
            </if>
            <if test="sex != '' and sex != null">
                and sex = #{sex}
            </if>
            <if test="address != '' and address != null">
                and address = #{address}
            </if>
        </where>
    </select>

SqlMapConfig.xml文件配置

<mapper resource="UserDao.xml"/>

测试文件

 @Test
    public void test02(){
    
    
        SqlSession session = MySessionUtils.getSession();
        UserDao dao = session.getMapper(UserDao.class);
        User user =new User();
        List<User> list = dao.findByUser(user);
        System.out.println(list);
        session.commit();
        session.close();
    }

动态sql-forEach标签

  • 作用 像sql传递数组或List,mybatis使用foreach标签
  • 如何使用
    》》collection:表示方法传入的集合对象的名字 collection=“xxx”
    》》tem:遍历集合时,会将集合中的元素赋值给item
    》》open表示你要拼接的sql以什么开始
    》》close:表示你拼接的sql以什么结束
    》》separator:表示拼接的分隔符
    》》接口中的变量名不能被标签识别,必须在参数的前边加注解@Param(“xxx”)
  • 练习
    UserDao.xml文件
 <select id="queryUsersByIds" resultType="user">
        select * from `user`
        <where>
            <foreach collection="ids" item="id" open="and id in(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </select>

UserDao实现接口

 List<User> queryUsersByIds(@Param("ids") List<Integer> ids);

测试

public void test03(){
    
    
        SqlSession session = MySessionUtils.getSession();
        UserDao dao = session.getMapper(UserDao.class);
        List<Integer> list =new ArrayList<>();
        list.add(1);
        list.add(3);
        list.add(5);
        List<User> users = dao.queryUsersByIds(list);
        System.out.println(users);
        session.commit();
        session.close();
    }

猜你喜欢

转载自blog.csdn.net/xinxin_____/article/details/108807662