spring boot数据操作Mapper.xml映射操作教程
1,配置文件设置
代码如下:
- mybatis:
- #mapper配置文件
- mapper-locations: classpath:mapper/*.xml
- type-aliases-package: com.sky.entity
- configuration:
- #开启驼峰命名
- map-underscore-to-camel-case: true
复制代码 xml操作数据库映射代码参考:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.sky.mapper.AddressBookMapper">
- <select id="list" parameterType="AddressBook" resultType="AddressBook">
- select * from address_book
- <where>
- <if test="userId != null">
- and user_id = #{userId}
- </if>
- <if test="phone != null">
- and phone = #{phone}
- </if>
- <if test="isDefault != null">
- and is_default = #{isDefault}
- </if>
- </where>
- </select>
- <update id="update" parameterType="addressBook">
- update address_book
- <set>
- <if test="consignee != null">
- consignee = #{consignee},
- </if>
- <if test="sex != null">
- sex = #{sex},
- </if>
- <if test="phone != null">
- phone = #{phone},
- </if>
- <if test="detail != null">
- detail = #{detail},
- </if>
- <if test="label != null">
- label = #{label},
- </if>
- <if test="isDefault != null">
- is_default = #{isDefault},
- </if>
- </set>
- where id = #{id}
- </update>
- </mapper>
复制代码- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.sky.mapper.DishFlavorMapper">
- <insert id="insertBatch">
- insert into dish_flavor (dish_id, name, value) VALUES
- <foreach collection="flavors" item="df" separator=",">
- (#{df.dishId},#{df.name},#{df.value})
- </foreach>
- </insert>
- </mapper>
复制代码- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.sky.mapper.OrderMapper">
- <insert id="insert" parameterType="Orders" useGeneratedKeys="true" keyProperty="id">
- insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status,
- amount, remark, phone, address, consignee, estimated_delivery_time, delivery_status,
- pack_amount, tableware_number, tableware_status)
- values
- (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},
- #{payStatus},#{amount}, #{remark}, #{phone}, #{address}, #{consignee}, #{estimatedDeliveryTime},
- #{deliveryStatus},#{packAmount}, #{tablewareNumber}, #{tablewareStatus})
- </insert>
- <update id="update" parameterType="com.sky.entity.Orders">
- update orders
- <set>
- <if test="cancelReason != null and cancelReason!='' ">
- cancel_reason=#{cancelReason},
- </if>
- <if test="rejectionReason != null and rejectionReason!='' ">
- rejection_reason=#{rejectionReason},
- </if>
- <if test="cancelTime != null">
- cancel_time=#{cancelTime},
- </if>
- <if test="payStatus != null">
- pay_status=#{payStatus},
- </if>
- <if test="payMethod != null">
- pay_method=#{payMethod},
- </if>
- <if test="checkoutTime != null">
- checkout_time=#{checkoutTime},
- </if>
- <if test="status != null">
- status = #{status},
- </if>
- <if test="deliveryTime != null">
- delivery_time = #{deliveryTime}
- </if>
- </set>
- where id = #{id}
- </update>
- <select id="pageQuery" resultType="Orders">
- select * from orders
- <where>
- <if test="number != null and number!=''">
- and number like concat('%',#{number},'%')
- </if>
- <if test="phone != null and phone!=''">
- and phone like concat('%',#{phone},'%')
- </if>
- <if test="userId != null">
- and user_id = #{userId}
- </if>
- <if test="status != null">
- and status = #{status}
- </if>
- <if test="beginTime != null">
- and order_time >= #{beginTime}
- </if>
- <if test="endTime != null">
- and order_time <= #{endTime}
- </if>
- </where>
- order by order_time desc
- </select>
- <select id="sumByMap" resultType="java.lang.Double">
- select sum(amount) from orders
- <where>
- <if test="begin != null">
- and order_time > #{begin}
- </if>
- <if test="end != null">
- and order_time < #{end}
- </if>
- <if test="status != null">
- and status = #{status}
- </if>
- </where>
- </select>
- <select id="countByMap" resultType="java.lang.Integer">
- select count(id) from orders
- <where>
- <if test="begin != null">
- and order_time > #{begin}
- </if>
- <if test="end != null">
- and order_time < #{end}
- </if>
- <if test="status != null">
- and status = #{status}
- </if>
- </where>
- </select>
- <select id="getSalesTop10" resultType="com.sky.dto.GoodsSalesDTO">
- select od.name, sum(od.number) number
- from order_detail od,orders o
- where od.order_id = o.id and o.status = 5
- <if test="begin != null">
- and o.order_time > #{begin}
- </if>
- <if test="end != null">
- and o.order_time < #{end}
- </if>
- group by od.name
- order by number desc
- limit 0,10
- </select>
- </mapper>
复制代码- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.sky.mapper.OrderDetailMapper">
- <insert id="insertBatch">
- insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount)
- values
- <foreach collection="orderDetailList" item="od" separator=",">
- (#{od.name},#{od.image},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},#{od.number},#{od.amount})
- </foreach>
- </insert>
- </mapper>
复制代码
参考完整代码:
java,spring boot2,jdk17外卖项目完整版代码
https://www.zidiu.com/thread-48-1-1.html
|
|