Mybatis-动态SQL - 学员管理系统plus

文章展示了如何在MyBatis中结合Lombok来简化数据对象和Mapper接口,通过动态SQL实现增删改查操作,包括条件查询和批量操作。Lombok的注解如@Data、@NoArgsConstructor和@AllArgsConstructor用于自动生成getter/setter,减少了代码冗余。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态SQL和lombok

   <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.26</version>
        </dependency>
package com.itheima.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    private  Integer id;
    private  String name;
    private String sex;
    private Integer age;
    private Double height;
}

package com.itheima.mapper;

import com.itheima.domain.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentMapper {

    public void addStudent(Student student);

    public List<Student> findAllStu();

    public List<Student> findByContions(@Param("id") Integer id,
                                        @Param("name") String name,
                                        @Param("sex") String sex,
                                        @Param("minAge") Integer minAge,
                                        @Param("maxAge") Integer maxAge,
                                        @Param("minHeight") Double minHeight,
                                        @Param("maxHeight") Double maxHeight);

    public Student findStuById(Integer id);

    public void updateStudent(Student student);

    public void deleteStuByIds(@Param("ids") List<Integer> ids);

    public List<Student> findStuByIds(@Param("ids") List<Integer> ids);


}

<?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.itheima.mapper.StudentMapper">


    <insert id="addStudent" parameterType="com.itheima.domain.Student" useGeneratedKeys="true" keyProperty="id">
  insert into student values (null,#{name},#{sex},#{age},#{height})

  </insert>
    <update id="updateStudent">
update student
<set>
    <if test="name != null">
         name = #{name},
    </if>
    <if test="sex != null">
          sex = #{sex},
    </if>
    <if test="age != null">
        age = #{age},
    </if>
    <if test="height != null">
        height = #{height}
    </if>
</set>
    where id = #{id}
    </update>
    <delete id="deleteStuByIds">

delete from student where id in
<foreach collection="ids" item="id" separator="," open="(" close=")" >
  #{id}
</foreach>

    </delete>

    <sql id="commenSelect">
        select * from student
    </sql>
    <select id="findAllStu" resultType="com.itheima.domain.Student">

<include refid="commenSelect"></include>

    </select>
    <select id="findByContions" resultType="com.itheima.domain.Student">
<include refid="commenSelect"></include>
        <where>
            <if test="id != null">
                id = #{id}
            </if>
            <if test="name != null">
              and  name  like  concat('%',#{name},'%')
            </if>
            <if test="sex != null">
                and  sex = #{sex}
            </if>
            <if test="minAge != null">
                and  age >= #{minAge}
            </if>
            <if test="maxAge != null">
                and  age <![CDATA[<=]]> #{maxAge}
            </if>
            <if test="minHeight != null">
                and  height >= #{minHeight}
            </if>
            <if test="maxHeight != null">
                and  height <![CDATA[<=]]> #{maxHeight}
            </if>

        </where>

    </select>
    <select id="findStuById" resultType="com.itheima.domain.Student">

<include refid="commenSelect"></include>
        <where>
            <if test="id !=null">
                id = #{id}
            </if>
        </where>
    </select>
    <select id="findStuByIds" resultType="com.itheima.domain.Student">
<include refid="commenSelect"></include>

where id in
<foreach collection="ids" item="id" separator="," open="(" close=")" >
         #{id}
    </foreach>
    </select>


</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PY_XAT_SFZL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值