UserMapper.xml 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.lc.mapper.UserMapper">
  6. <!--
  7. 动态sql if 标签
  8. test="" OGNL 表达式
  9. select * from t_user where and password = ? sql 问题
  10. -->
  11. <select id="getUserByNameAndPass" resultType="user" >
  12. select * from t_user
  13. <where>
  14. <if test="name != null and name != '' ">
  15. username = #{name}
  16. </if>
  17. <if test="pass != null and pass != '' ">
  18. and password = #{pass}
  19. </if>
  20. </where>
  21. </select>
  22. <!--
  23. set 解决 逗号问题
  24. -->
  25. <update id="updateUserById">
  26. UPDATE `db2`.`t_user`
  27. <set>
  28. <if test=" username != null and username != '' ">
  29. `username` = #{username},
  30. </if>
  31. <if test=" password != null and password != '' ">
  32. `password` = #{password},
  33. </if>
  34. <if test=" gender != null and gender != '' ">
  35. `gender` = #{gender},
  36. </if>
  37. <if test=" address != null and address != '' ">
  38. `address` = #{address},
  39. </if>
  40. <if test=" deptId != null and deptId != '' ">
  41. `dept_id` = #{deptId}
  42. </if>
  43. </set>
  44. <where>
  45. <if test="id != null and id !='' ">
  46. `id` = #{id} ;
  47. </if>
  48. </where>
  49. </update>
  50. <!--
  51. trim
  52. 属性
  53. prefix 前缀
  54. prefixOverrides 覆盖前缀
  55. suffix 后缀
  56. suffixOverrides 覆盖后缀
  57. -->
  58. <select id="getUserByNameAndPassTrim" resultType="user" >
  59. select * from t_user
  60. <trim prefix="where" prefixOverrides="and" >
  61. <if test="name != null and name != '' ">
  62. and username = #{name}
  63. </if>
  64. <if test="pass != null and pass != '' ">
  65. and password = #{pass}
  66. </if>
  67. </trim>
  68. </select>
  69. <!--
  70. foreach
  71. collection 集合
  72. open 开始符号
  73. close 结尾
  74. separator 中间符号
  75. index 索引
  76. item 每一项
  77. -->
  78. <select id="getUserByIds" resultType="user" >
  79. select
  80. <include refid="userSql"></include>
  81. from t_user where id in
  82. <foreach collection="ids" open="(" item="id" separator="," close=")">
  83. #{id}
  84. </foreach>
  85. </select>
  86. <!--
  87. sql 片段
  88. -->
  89. <sql id="userSql">
  90. username , password , gender , address , dept_id
  91. </sql>
  92. </mapper>