请给出三张数据表,并且用mybatis完成最基础的增删改查以及联合查询,批量删除等功能
时间: 2024-03-22 11:36:48 浏览: 47
Mybatis 条件查询 批量增删改查功能
5星 · 资源好评率100%
好的,以下是三张数据表的示例以及使用MyBatis实现的基础增删改查和联合查询、批量删除等功能。
1. 用户表(user)
| 字段名 | 类型 | 描述 |
| --------- | --------- | -------- |
| id | int | 用户ID |
| username | varchar | 用户名 |
| password | varchar | 密码 |
| email | varchar | 邮箱 |
| created_at| timestamp | 创建时间 |
```xml
<!-- UserMapper.xml -->
<mapper namespace="com.example.mapper.UserMapper">
<!-- 基础查询 -->
<select id="getUserById" parameterType="int" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
<insert id="insertUser" parameterType="com.example.model.User">
INSERT INTO user(username, password, email, created_at)
VALUES(#{username}, #{password}, #{email}, #{createdAt})
</insert>
<update id="updateUser" parameterType="com.example.model.User">
UPDATE user SET username = #{username}, password = #{password}, email = #{email}
WHERE id = #{id}
</update>
<delete id="deleteUserById" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
<!-- 联合查询 -->
<select id="getUserOrders" parameterType="int" resultType="com.example.model.User">
SELECT u.*, o.order_no, o.total_price FROM user u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.id = #{id}
</select>
<!-- 批量删除 -->
<delete id="deleteUsers" parameterType="java.util.List">
DELETE FROM user WHERE id IN
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
```
2. 商品表(product)
| 字段名 | 类型 | 描述 |
| --------- | --------- | -------- |
| id | int | 商品ID |
| name | varchar | 商品名 |
| price | decimal | 价格 |
| stock | int | 库存 |
| created_at| timestamp | 创建时间 |
```xml
<!-- ProductMapper.xml -->
<mapper namespace="com.example.mapper.ProductMapper">
<!-- 基础查询 -->
<select id="getProductById" parameterType="int" resultType="com.example.model.Product">
SELECT * FROM product WHERE id = #{id}
</select>
<insert id="insertProduct" parameterType="com.example.model.Product">
INSERT INTO product(name, price, stock, created_at)
VALUES(#{name}, #{price}, #{stock}, #{createdAt})
</insert>
<update id="updateProduct" parameterType="com.example.model.Product">
UPDATE product SET name = #{name}, price = #{price}, stock = #{stock}
WHERE id = #{id}
</update>
<delete id="deleteProductById" parameterType="int">
DELETE FROM product WHERE id = #{id}
</delete>
<!-- 联合查询 -->
<select id="getProductOrders" parameterType="int" resultMap="com.example.mapper.ProductMapper.ProductOrderResultMap">
SELECT p.*, o.order_no, o.total_price FROM product p
LEFT JOIN order_item oi ON p.id = oi.product_id
LEFT JOIN orders o ON oi.order_id = o.id
WHERE p.id = #{id}
</select>
<resultMap id="ProductOrderResultMap" type="com.example.model.Product">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="price" column="price"/>
<result property="stock" column="stock"/>
<collection property="orders" ofType="com.example.model.Order">
<id property="orderNo" column="order_no"/>
<result property="totalPrice" column="total_price"/>
</collection>
</resultMap>
<!-- 批量删除 -->
<delete id="deleteProducts" parameterType="java.util.List">
DELETE FROM product WHERE id IN
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
```
3. 订单表(orders)
| 字段名 | 类型 | 描述 |
| --------- | --------- | -------- |
| id | int | 订单ID |
| order_no | varchar | 订单号 |
| user_id | int | 用户ID |
| total_price| decimal | 总价 |
| created_at| timestamp | 创建时间 |
```xml
<!-- OrderMapper.xml -->
<mapper namespace="com.example.mapper.OrderMapper">
<!-- 基础查询 -->
<select id="getOrderById" parameterType="int" resultType="com.example.model.Order">
SELECT * FROM orders WHERE id = #{id}
</select>
<insert id="insertOrder" parameterType="com.example.model.Order">
INSERT INTO orders(order_no, user_id, total_price, created_at)
VALUES(#{orderNo}, #{userId}, #{totalPrice}, #{createdAt})
</insert>
<update id="updateOrder" parameterType="com.example.model.Order">
UPDATE orders SET order_no = #{orderNo}, user_id = #{userId}, total_price = #{totalPrice}
WHERE id = #{id}
</update>
<delete id="deleteOrderById" parameterType="int">
DELETE FROM orders WHERE id = #{id}
</delete>
<!-- 联合查询 -->
<select id="getOrderItems" parameterType="int" resultMap="com.example.mapper.OrderMapper.OrderItemResultMap">
SELECT oi.quantity, oi.price, p.* FROM order_item oi
LEFT JOIN product p ON oi.product_id = p.id
WHERE oi.order_id = #{id}
</select>
<resultMap id="OrderItemResultMap" type="com.example.model.Order">
<id property="id" column="id"/>
<result property="quantity" column="quantity"/>
<result property="price" column="price"/>
<association property="product" javaType="com.example.model.Product">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="price" column="price"/>
<result property="stock" column="stock"/>
</association>
</resultMap>
<!-- 批量删除 -->
<delete id="deleteOrders" parameterType="java.util.List">
DELETE FROM orders WHERE id IN
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>
```
阅读全文