spring boot数据操作Mapper.xml映射操作教程

[复制链接]
admin 发表于 2025-9-14 16:00:58 | 显示全部楼层 |阅读模式
spring boot数据操作Mapper.xml映射操作教程

1,配置文件设置

2.jpg


代码如下:
  1. mybatis:
  2.   #mapper配置文件
  3.   mapper-locations: classpath:mapper/*.xml
  4.   type-aliases-package: com.sky.entity
  5.   configuration:
  6.     #开启驼峰命名
  7.     map-underscore-to-camel-case: true
复制代码
xml操作数据库映射代码参考:
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.sky.mapper.AddressBookMapper">

  4.     <select id="list" parameterType="AddressBook" resultType="AddressBook">
  5.         select * from address_book
  6.         <where>
  7.             <if test="userId != null">
  8.                 and user_id = #{userId}
  9.             </if>
  10.             <if test="phone != null">
  11.                 and phone = #{phone}
  12.             </if>
  13.             <if test="isDefault != null">
  14.                 and is_default = #{isDefault}
  15.             </if>
  16.         </where>
  17.     </select>

  18.     <update id="update" parameterType="addressBook">
  19.         update address_book
  20.         <set>
  21.             <if test="consignee != null">
  22.                 consignee = #{consignee},
  23.             </if>
  24.             <if test="sex != null">
  25.                 sex = #{sex},
  26.             </if>
  27.             <if test="phone != null">
  28.                 phone = #{phone},
  29.             </if>
  30.             <if test="detail != null">
  31.                 detail = #{detail},
  32.             </if>
  33.             <if test="label != null">
  34.                 label = #{label},
  35.             </if>
  36.             <if test="isDefault != null">
  37.                 is_default = #{isDefault},
  38.             </if>
  39.         </set>
  40.         where id = #{id}
  41.     </update>

  42. </mapper>
复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  4. <mapper namespace="com.sky.mapper.DishFlavorMapper">

  5.     <insert id="insertBatch">
  6.         insert into dish_flavor (dish_id, name, value) VALUES
  7.         <foreach collection="flavors" item="df" separator=",">
  8.             (#{df.dishId},#{df.name},#{df.value})
  9.         </foreach>
  10.     </insert>
  11. </mapper>
复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  4. <mapper namespace="com.sky.mapper.OrderMapper">

  5.     <insert id="insert" parameterType="Orders" useGeneratedKeys="true" keyProperty="id">
  6.         insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status,
  7.                             amount, remark, phone, address, consignee, estimated_delivery_time, delivery_status,
  8.                             pack_amount, tableware_number, tableware_status)
  9.         values
  10.                (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},
  11.                 #{payStatus},#{amount}, #{remark}, #{phone}, #{address}, #{consignee}, #{estimatedDeliveryTime},
  12.                 #{deliveryStatus},#{packAmount}, #{tablewareNumber}, #{tablewareStatus})
  13.     </insert>

  14.     <update id="update" parameterType="com.sky.entity.Orders">
  15.         update orders
  16.         <set>
  17.             <if test="cancelReason != null and cancelReason!='' ">
  18.                 cancel_reason=#{cancelReason},
  19.             </if>
  20.             <if test="rejectionReason != null and rejectionReason!='' ">
  21.                 rejection_reason=#{rejectionReason},
  22.             </if>
  23.             <if test="cancelTime != null">
  24.                 cancel_time=#{cancelTime},
  25.             </if>
  26.             <if test="payStatus != null">
  27.                 pay_status=#{payStatus},
  28.             </if>
  29.             <if test="payMethod != null">
  30.                 pay_method=#{payMethod},
  31.             </if>
  32.             <if test="checkoutTime != null">
  33.                 checkout_time=#{checkoutTime},
  34.             </if>
  35.             <if test="status != null">
  36.                 status = #{status},
  37.             </if>
  38.             <if test="deliveryTime != null">
  39.                 delivery_time = #{deliveryTime}
  40.             </if>
  41.         </set>
  42.         where id = #{id}
  43.     </update>

  44.     <select id="pageQuery" resultType="Orders">
  45.         select * from orders
  46.         <where>
  47.             <if test="number != null and number!=''">
  48.                 and number like concat('%',#{number},'%')
  49.             </if>
  50.             <if test="phone != null and phone!=''">
  51.                 and phone like concat('%',#{phone},'%')
  52.             </if>
  53.             <if test="userId != null">
  54.                 and user_id = #{userId}
  55.             </if>
  56.             <if test="status != null">
  57.                 and status = #{status}
  58.             </if>
  59.             <if test="beginTime != null">
  60.                 and order_time &gt;= #{beginTime}
  61.             </if>
  62.             <if test="endTime != null">
  63.                 and order_time &lt;= #{endTime}
  64.             </if>
  65.         </where>
  66.         order by order_time desc
  67.     </select>

  68.     <select id="sumByMap" resultType="java.lang.Double">
  69.         select sum(amount) from orders
  70.         <where>
  71.             <if test="begin != null">
  72.                 and order_time &gt; #{begin}
  73.             </if>
  74.             <if test="end != null">
  75.                 and order_time &lt; #{end}
  76.             </if>
  77.             <if test="status != null">
  78.                 and status = #{status}
  79.             </if>
  80.         </where>
  81.     </select>
  82.     <select id="countByMap" resultType="java.lang.Integer">
  83.         select count(id) from orders
  84.         <where>
  85.             <if test="begin != null">
  86.                 and order_time &gt; #{begin}
  87.             </if>
  88.             <if test="end != null">
  89.                 and order_time &lt; #{end}
  90.             </if>
  91.             <if test="status != null">
  92.                 and status = #{status}
  93.             </if>
  94.         </where>
  95.     </select>
  96.     <select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">
  97.         select od.name, sum(od.number) number
  98.         from order_detail od,orders o
  99.         where od.order_id = o.id and o.status = 5
  100.         <if test="begin != null">
  101.             and o.order_time &gt; #{begin}
  102.         </if>
  103.         <if test="end != null">
  104.             and o.order_time &lt; #{end}
  105.         </if>
  106.         group by od.name
  107.         order by number desc
  108.         limit 0,10
  109.     </select>
  110. </mapper>
复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3.         "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  4. <mapper namespace="com.sky.mapper.OrderDetailMapper">

  5.     <insert id="insertBatch">
  6.         insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount)
  7.             values
  8.         <foreach collection="orderDetailList" item="od" separator=",">
  9.             (#{od.name},#{od.image},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},#{od.number},#{od.amount})
  10.         </foreach>
  11.     </insert>
  12. </mapper>
复制代码

参考完整代码:
java,spring boot2,jdk17外卖项目完整版代码
https://www.zidiu.com/thread-48-1-1.html

网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表