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="top.jztice5.mapper.PruductMapper">
    6. <!--
    7. 注:标签不是select
    8. values后面写的是Brand类中的属性名字
    9. useGeneratedKeys 获取mysql自动生成的主键,自动封装到id属性值中
    10. keyProperty 实体类中主键的属性名 brandid
    11. keyColumn tb_brand表中主键字段名字 id
    12. -->
    13. <!-- 映射的接口方法 id后面的方法名 ;框架创建代理对象,当调用映射的接口方法后,代理对象就会找到并执行对应idsql语句,再将结果集或需要的数据返回到实体类中-->
    14. <!-- 在创建代理对象的时候就会加载映射文件 mapper.xml)-->
    15. <!-- 添加-->
    16. <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
    17. insert into tb_brand
    18. values (null, #{brandName}, #{companyName}, #{ordered}, #{description}, #{status})
    19. </insert>
    20. <!-- 修改-->
    21. <update id="update">
    22. update tb_brand
    23. <set>
    24. <if test="brandName !=null and brandName !=''">
    25. brand_name =#{brandName},
    26. </if>
    27. <if test="companyName != null and companyName !=''">
    28. company_name =#{companyName},
    29. </if>
    30. <if test="ordered != null">
    31. ordered=#{ordered},
    32. </if>
    33. <if test="description!=null and description!=''">
    34. description=#{description},
    35. </if>
    36. <if test="status!=null">
    37. status = #{status}
    38. </if>
    39. </set>
    40. <where>
    41. id = #{id}
    42. </where>
    43. </update>
    44. <!-- 删除-->
    45. <delete id="delete">
    46. delete
    47. from tb_brand
    48. where id = #{id}
    49. </delete>
    50. <!-- 批量删除: 数组-->
    51. <delete id="deletes">
    52. delete from tb_brand
    53. where id in
    54. -- 因为数组有别名:ids,如果没有别名,这里是数组的话,默认写 array
    55. <foreach collection="ids" open="(" separator="," close=")" item="id">
    56. #{id}
    57. </foreach>
    58. </delete>
    59. <!-- 批量删除 集合-->
    60. <delete id="deletesList">
    61. delete from tb_brand
    62. where id in
    63. <foreach collection="collection" open="(" separator="," close=")" item="id">
    64. #{id}
    65. </foreach>
    66. </delete>
    67. <!-- 查询-->
    68. <!--只有查询才需要定义结果集的返回值类型-->
    69. <select id="selectOne" resultType="top.jztice5.pojo.Pruduct">
    70. select *
    71. from tb_brand
    72. where id = #{id};
    73. -- #:的底层是preparedStatement
    74. -- $:的底层是Statement,会有sql注入异常;(两个不能同时存在)
    75. </select>
    76. //多条件查询:
    77. <select id="selectAll" resultType="top.jztice5.pojo.Pruduct">
    78. select * from tb_brand
    79. <where>
    80. <if test="status != null">
    81. `status`= #{status}
    82. </if>
    83. <if test="brandName != null and brandName !='' ">
    84. AND brand_Name like "%"#{brandName}"%"
    85. </if>
    86. <if test="companyName != null and companyName !='' ">
    87. AND company_Name like "%"#{companyName}"%"
    88. </if>
    89. </where>
    90. </select>
    91. <select id="selects" resultType="top.jztice5.pojo.Pruduct">
    92. select *
    93. from tb_brand;
    94. </select>
    95. <select id="selectList" resultType="top.jztice5.pojo.Pruduct">
    96. select *
    97. from tb_brand
    98. <where>
    99. <if test="status != null">
    100. status = 1
    101. </if>
    102. <if test="companyName != null and companyName !=''">
    103. and company_name = #{companyName}
    104. </if>
    105. <if test="brandName != null and brandName !=''">
    106. and brand_Name = #{brandName}
    107. </if>
    108. </where>
    109. </select>
    110. </mapper>