package com.abc.mapper;
import com.abc.entity.Teacher;
import com.abc.provider.TeacherProvider;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;
public interface TeacherMapper {
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert({"insert into a_teacher(create_time,modify_time,name,age,home_address)",
" values(#{createTime},#{modifyTime},#{name},#{age},#{homeAddress})"})
void insert(Teacher teacher);
@Update({"<script>",
"update a_teacher set id=id",
"<if test='name != null and name != \"\"'>",
" ,name=#{name}",
"</if>",
"<if test='age != null'>",
" ,age=#{age}",
"</if>",
"<if test='homeAddress != null and homeAddress != \"\"'>",
" ,home_address=#{homeAddress}",
"</if>",
"where id=#{id}",
"</script>"
})
void update(Teacher teacher);
@Select("select * from a_teacher where id=#{id}")
Teacher selectById(Long id);
@Select({"<script>",
"select * from a_teacher where 1=1",
"<if test='name != null and name != \"\"'>",
" and name like #{name}",
"</if>",
"<if test='homeAddress != null and homeAddress != \"\"'>",
" and home_address like #{homeAddress}",
"</if>",
"</script>"
})
List<Teacher> select(@Param("name") String name, @Param("homeAddress") String homeAddress);
@SelectProvider(type = TeacherProvider.class)
List<Map<String, Object>> select2(String name, String homeAddress);
@Delete({"<script>",
"delete from a_teacher where id in",
"<foreach collection='array' item='id' index='index' open='(' close=')' separator=','>",
" #{id}",
"</foreach>",
"</script>"
})
void delete(Long[] ids);
}
TeacherMapper 使用注解来编写 SQL,如果使用了动态 SQL,需添加 script 元素;如果 SQL 比较复杂,不太方便使用注解,可以通过 Provider 使用 Java 代码来构建 SQL。