1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <!DOCTYPE mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.jyusun.mapper.UserMapper">
    6. <!-- sql:里面可以写入一个共同的sql代码,用于提取重复的代码。
    7. 要使用该代码的时候就直接使用<include>标签
    8. id:为提取的sql代码,取一个id,起标识作用
    9. -->
    10. <sql id="select">
    11. select * from user
    12. </sql>
    13. <!-- public User findUserById(int id);
    14. id:填写在XxxMapper接口中的方法名
    15. parameterType:填写参数的类型
    16. resultType:填写方法中返回值的类型,不用写全路径,不区分大小写
    17. -->
    18. <select id="findUserById" parameterType="int" resultType="user">
    19. <!-- include:用于加载提取公共的sql语句,与<sql>标签对应
    20. refid:填写<sql>标签中的id属性
    21. -->
    22. <include refid="select"></include>
    23. where id = #{id}
    24. </select>
    25. <!-- resultMap属性:与resultMap标签一起使用,填写resultMap标签中定义的id属性 -->
    26. <select id="findAllOrders" resultMap="orders">
    27. select * from orders
    28. </select>
    29. <!-- resultMap标签:用于自定义封装结果
    30. type:最终结果还是封装到实体类中,type就是指定封装到哪一个类中
    31. id:与<select>标签中的resultMap中的属性一直,一定要唯一
    32. <id>:该标签是指定主键封装到实体类中的哪一个属性(可以省略)
    33. <result>:该标签是其他的列封装到实体类中,一般只需填写实体类中的属性与表中列不同的项即可
    34. property:填写实体类中的属性,column:填写表中的列名
    35. -->
    36. <resultMap type="Orders" id="orders">
    37. <id property="id" column="id"/>
    38. <result property="userId" column="user_id"/>
    39. </resultMap>
    40. <!-- public void addUser(User user);
    41. insert:用于执行添加语句;update:执行更新语句
    42. 同样 delete:执行删除语句
    43. -->
    44. <insert id="addUser" parameterType="user">
    45. <!-- selectKey配置主键信息的标签
    46. keyColumn:对应数据库表中的主键列
    47. keyProperty:对应实体类中的属性
    48. after:代表执行下面代码之前,先执行当前里面的代码
    49. -->
    50. <selectKey keyColumn="id" keyProperty="id" order="AFTER" resultType="int">
    51. select LAST_INSERT_ID()
    52. </selectKey>
    53. insert into user
    54. (username,sex,address)
    55. values(#{username},#{sex},#{address})
    56. </insert>
    57. <!-- public List<User> findUserBySexAndUsername(User user); -->
    58. <select id="findUserBySexAndUsername" parameterType="User" resultType="user">
    59. <!--select * from user where 1=1 -->
    60. <include refid="select"></include>
    61. <!-- where标签:一个where条件语句,通常和<if>标签混合使用 -->
    62. <where>
    63. <!-- if标签:执行一个判断语句,成立才会执行标签体内的sql语句
    64. test:写上条件判断语句
    65. 注意:这里每一个if前面都尽量加上and,如果你是第一个条件,框架会自动帮你把and截取,如果是第二个if就不能省略and
    66. -->
    67. <if test="sex != null and sex != ''">
    68. and sex = #{sex}
    69. </if>
    70. <if test="username != null and username != ''">
    71. and username like '%${username}%'
    72. </if>
    73. </where>
    74. </select>
    75. <!-- public List<User> findUserByIds(QueryVo vo); -->
    76. <!-- QueryVo:是一个实体包装类,通常用于封装实体类之外的一些属性-->
    77. <select id="findUserByIds" parameterType="QueryVo" resultType="user">
    78. <include refid="select"></include>
    79. <where>
    80. <!-- foreach:循环语句,通常多用于参数是集合时,需要对参数进行遍历出来,再进行赋值查询
    81. collection:参数类型中的集合、数组的名字,例:下面的ids就是QueryVo这个类中的list集合的名字
    82. item:为遍历该集合起一个变量名,遍历出来的每一个字,都赋值到这个item中
    83. open:在sql语句前面添加的sql片段
    84. close:在sql语句后面添加的sql片段
    85. separator:指定遍历元素之前用什么分隔符
    86. -->
    87. <foreach collection="ids" item="id" open="id in(" close=")" separator=",">
    88. #{id}
    89. </foreach>
    90. </where>
    91. </select>
    92. </mapper>