指定Mapper接口参数

  1. package com.chentianyu.mapper;
  2. import com.chentianyu.pojo.User;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Param;
  5. import java.util.List;
  6. /**
  7. * 底层提供的功能:
  8. * 要添加条件(判断有没有条件,有就加,没有就拉倒(不加),条件:姓名,性别(可以用实体类或者是下标,或者是map)),
  9. * 多条件分页查询
  10. * 添加
  11. * 删除
  12. * 更新
  13. * 底层干的活就是为前端提供数据的。所以前后端一般有接口文档
  14. */
  15. public interface UserMapper {
  16. /**
  17. * SSM后台提供的数据
  18. * url:
  19. * 参数:
  20. * userName:表单中的用户名称
  21. * userSex:表单中的用户性别
  22. * page:提交的页码(第一次访问为null)
  23. * 结果: 是一个JSON数组
  24. * 无数据时:
  25. * []
  26. */
  27. public List<User> selectUserPage(
  28. @Param("userName")
  29. String userName,
  30. @Param("userSex")
  31. String userSex,
  32. @Param("startRow")//算好的其实行的值
  33. int startRow);
  34. }
  1. package com.chentianyu.mapper;
  2. import com.chentianyu.pojo.User;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Param;
  5. import java.util.List;
  6. /**
  7. * 底层提供的功能:
  8. * 要添加条件(判断有没有条件,有就加,没有就拉倒(不加),条件:姓名,性别(可以用实体类或者是下标,或者是map)),
  9. * 多条件分页查询
  10. * 添加
  11. * 删除
  12. * 更新
  13. * 底层干的活就是为前端提供数据的。所以前后端一般有接口文档
  14. */
  15. public interface UserMapper {
  16. /**
  17. * SSM后台提供的数据
  18. * url: /user/selectUserPage?userName=?&userSex=?
  19. * 参数:
  20. * userName:表单中的用户名称
  21. * userSex:表单中的用户性别
  22. * page:提交的页码(第一次访问为null)
  23. * 结果: 是一个JSON数组
  24. * 无数据时:
  25. * []
  26. *
  27. * select * from user limit (当前页码-1)*每页条数,每页条数;
  28. */
  29. public List<User> selectUserPage(
  30. @Param("userName")
  31. String userName,
  32. @Param("userSex")
  33. String userSex,
  34. @Param("startRow")//算好的其实行的值
  35. int startRow);
  36. /**
  37. * 增加(添加)
  38. * url: /user/createUser
  39. * 参数:
  40. * cardType
  41. * cardNo
  42. * userName
  43. * userSex
  44. * userAge
  45. * userRole
  46. * 结果:
  47. * 添加成功:
  48. * 1
  49. * 添加失败:
  50. * 0
  51. */
  52. int createUser(User user);
  53. /**
  54. * 删除
  55. * url: /user/deleteUserById?userId= ?
  56. * 参数
  57. * userId:删除用户的id
  58. * 结果:
  59. * 删除成功:
  60. * 1
  61. * 删除失败:
  62. * 0
  63. */
  64. int deleteUserById(String userId);
  65. /**
  66. * 获取总行数
  67. * url: /user/getRowCount?userName=?&userSex=?
  68. * 参数:
  69. * userName: 表单中用户的名称
  70. * userSex:表单中用户的性别
  71. * 结果:
  72. * 有数据时:
  73. * 12
  74. * 无数据时:
  75. * 0
  76. */
  77. int getRowCount(
  78. @Param("userName")
  79. String userName,
  80. @Param("userSex")
  81. String userSex);
  82. }

到此为止,接口层开发完毕,接口按照接口文档开发。接口文档就是用来前后端统一传输请求的规范

开发要用动态代理的方式做:

创建XXXMapper.xml模板(参考spring-sm整合-MyBatis模板)

分页查询&初始化配置
  1. <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
  2. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.chentianyu.mapper.UserMapper">
  4. <!--
  5. 列名和实体类成员变量名称不一致
  6. private String userId;
  7. private String cardType;
  8. private String cardNo;
  9. private String userName;
  10. private String userSex;
  11. private String userAge;
  12. private String userRole;
  13. 所以干所有活之前,先resultMap。
  14. 完成实体类与列名的映射
  15. -->
  16. <resultMap id="usermap" type="user">
  17. <id property="userId" column="user_id"></id>
  18. <result property="cardType" column="card_type"></result>
  19. <result property="cardNo" column="card_no"></result>
  20. <result property="userName" column="user_name"></result>
  21. <result property="userSex" column="user_sex"></result>
  22. <result property="userAge" column="user_age"></result>
  23. <result property="userRole" column="user_role"></result>
  24. </resultMap>
  25. <!--定义全部列名-->
  26. <sql id="allColumns">
  27. user_id,card_type,card_no,user_name,user_sex,user_age,user_role
  28. </sql>
  29. <!--
  30. 实现分页查询
  31. public List<User> selectUserPage(
  32. @Param("userName")
  33. String userName,
  34. @Param("userSex")
  35. String userSex,
  36. @Param("startRow")//算好的其实行的值
  37. int startRow);
  38. -->
  39. <select id="selectUserPage" resultMap="usermap">
  40. select <include refid="allColumns"></include>
  41. from user <where>
  42. <if test="userName!=null and userName!=''">
  43. and user_name like concat('%',#{userName},'%')
  44. </if>
  45. <if test="userSex!=null and userSex!=''">
  46. and user_sex = #{userSex}
  47. </if>
  48. </where>
  49. limit #{startRow},5
  50. </select>
  51. </mapper>

整个分页查询的功能就开发完了

添加
<!--
int createUser(User user);
-->
<insert id="createUser" parameterType="user">
    insert into user values(#{userId},#{cardType},#{cardNo},#{userName},#{userSex},#{userAge},#{userRole})
</insert>

删除
<!--
int deleteUserById(String userId);
-->
<delete id="deleteUserById" parameterType="string">
    delete from user where user_id=#{userId}
</delete>

计算总行数
<!--
计算总行数
int getRowCount(
        @Param("userName")
        String userName,
        @Param("userSex")
        String userSex);
-->
<select id="getRowCount" resultType="int">
    select count(*) from user <where>
        <if test="userName!=null and userName!=''">
            and user_name like concat('%',#{userName},'%')
        </if>
        <if test="userSex!=null and userSex!=''">
            and user_sex = #{userSex}
        </if>
    </where>
</select>

这样我们mapper开发就结束了