• mybatis基础02


    一、Mybatis的获取参数的方式

    方式1:${} 字符串拼接

    方式2:#{} 占位符

    1.Mapper接口参数为单个参数

    Integer deleteById(Integer id);
    1. <delete id="deleteById" parameterType="int">
    2. delete from user where id = #{id}
    3. delete>

    2.Mapper接口参数是多个参数的获取

    User selectUserByUserNameAndPassword(String username,String password);
    1. <select id="selectUserByUserNameAndPassword" resultType="user">
    2. select * from user where user_name = #{arg0} and password = #{arg1}
    3. select>

    3.将参数设为map集合进行数据的传递

    User selectUserByMap(Map map);
    1. <select id="selectUserByMap" resultType="cn.kgc.mybatis.entity.User">
    2. select * from user where user_name = #{username} and password = #{password}
    3. select>

    4.通过注解@param("键名")设置参数的获取名字

    Integer addUser(@Param("username") String username,@Param("password") String password);
    
    1. <insert id="addUser" >
    2. insert into user(username,password)values(#{username},#{password})
    3. insert>

    5.将多个参数设置成对象进行数据的传递

    int insertUser(User user);
    1. <insert id="insertUser" parameterType="user" useGeneratedKeys="true" keyProperty="id" >
    2. insert into user values ( #{id},#{userName},#{password})
    3. insert>

    二、Mybatis中的模糊查询

    方式1:

    1. SELECT * FROM tableName WHERE name LIKE #{text};
    2. -- 传递的参数需要拼接 ‘%text%’

    方式2:

    1. SELECT * FROM tableName WHERE name LIKE '%${text}%';
    2. -- 使用$替代# 参数无需在拼接% 存在sql注入

    方式3

    1. SELECT * FROM tableName WHERE name LIKE CONCAT('%', #{text}), '%');
    2. -- 使用mysql的函数 实现拼接

    批量删除

    int deleteMore(String ids);
    1. <delete id="deleteMore">
    2. delete from user where id in(${ids})
    3. delete>

    mapper中自定义映射

    自定义映射的用法之一,解决表格查询的字段名和实体类中不一致的情况

    1. <resultMap id="userMap" type="user">
    2. <id column="id" property="id">id>
    3. <result column="user_name" property="userName">result>
    4. <result column="password" property="password">result>
    5. resultMap>

    mybatis对象的关联关系

    一对多关系处理(一方)

    1. <resultMap id="selectByEIdMap" type="cn.kgc.mybatis.entity.Emp">
    2. <result column="id" property="dept.id" >result>
    3. <result column="dname" property="dept.dName">result>
    4. resultMap>
    5. <resultMap id="selectByEIdMap2" type="cn.kgc.mybatis.entity.Emp">
    6. <id column="eid" property="eId">id>
    7. <result column="ename" property="EName">result>
    8. <result column="age" property="age">result>
    9. <result column="deptno" property="deptNo">result>
    10. <association property="dept" javaType="dept">
    11. <id column="id" property="id">id>
    12. <result column="dname" property="dName">result>
    13. association>
    14. resultMap>
    15. <resultMap id="selectByEIdMap3" type="cn.kgc.mybatis.entity.Emp">
    16. <id column="eid" property="eId">id>
    17. <result column="ename" property="EName">result>
    18. <result column="age" property="age">result>
    19. <result column="deptno" property="deptNo">result>
    20. <association property="dept" select="cn.kgc.mybatis.mapper.DeptMapper.selectById"
    21. column="deptno"
    22. fetchType="eager">
    23. association>
    24. resultMap>
    25. 注:延迟加载设置 :
    26. 1. <setting name="lazyLoadingEnabled" value="true"/>
    27. 2. <setting name="aggressiveLazyLoading" value="false"/> 3.4.1之前的版本需要设置
    28. <select id="selectByEId" resultMap="selectByEIdMap2">
    29. select * from emp left join dept on emp.deptno = dept.id where eid = #{eid}
    30. select>
    31. <select id="selectByEId2" resultMap="selectByEIdMap3">
    32. select * from emp where eid = #{eid}
    33. select>

    一对多关系处理(多方)

    1. <resultMap id="BaseResultMap" type="cn.kgc.mybatis.entity.Dept">
    2. <id property="id" column="id" />
    3. <result property="dName" column="dname" />
    4. <collection property="emps" ofType="emp">
    5. <id column="eid" property="eId">id>
    6. <result column="ename" property="EName">result>
    7. <result column="age" property="age">result>
    8. <result column="deptno" property="deptNo">result>
    9. collection>
    10. resultMap>
    11. <resultMap id="BaseResultMap2" type="cn.kgc.mybatis.entity.Dept">
    12. <id property="id" column="id" />
    13. <result property="dName" column="dname" />
    14. <collection property="emps" select="cn.kgc.mybatis.mapper.EmpMapper.selectByDeptId" column="id">collection>
    15. resultMap>
    16. <select id="selectById" resultType="dept">
    17. select * from dept where id = #{id}
    18. select>
    19. <select id="selectById2" resultMap="BaseResultMap">
    20. select emp.* ,dept.* from dept left join emp on dept.id = emp.deptno where id = #{id}
    21. select>
    22. <select id="selectById3" resultMap="BaseResultMap2">
    23. select dept.* from dept where id = #{id}
    24. select>

  • 相关阅读:
    深度解读《深度探索C++对象模型》之C++虚函数实现分析(一)
    【图像分割】基于回溯搜索优化算法实现图像聚类分割附matlab代码
    群晖Cloud Sync数据同步到百度云、另一台群晖、nextcloud教程
    【Redis学习笔记】第八章 Redis删除策略
    操作系统实验一模拟优先级调度算法(C语言实现附带详细注释)
    MySQL8.0优化 - 优化MySQL服务器、优化MySQL的参数、优化数据类型
    PHP+AJAX实现异步上传文件
    altium concord pro完整版下载的疑问
    一键式文件共享软件Jirafeau
    android 如何分析应用的内存(十五)——Visual Studio Code 调试Android应用
  • 原文地址:https://blog.csdn.net/lmdbhf/article/details/128062183