<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>