1,常见的注解
(1)基本注释:实现简单的增删改查操作。
(2)结果映射注解:实现结果的映射关系,也可以完成级联映射。
(3)动态SQL注解:实现动态 SQL 的内容。
2,Insert 新增
// 新增学生
@Insert("insert into student (sname,birthday,ssex,classid) "
+ "values(#{sname},#{birthday},#{ssex},#{classid})")
@Options(keyProperty = "sid", useGeneratedKeys = true)
public int addStudent(Student s);
3,Delete 删除
// 删除
@Delete("delete from student where sid = #{sid}")
public int delStudent(int sid);
4,Update 修改
// 修改
@Update("update student set sname=#{sname}, birthday= #{birthday},"
+ "ssex = #{ssex}, classid = #{classid} where sid = #{sid}")
public int updateStudent(Student s);
5,Select 查询
// 查询
@Select("select * from student")
public List<Student> findAllStudent();
@Select("select * from student where sid = #{v}")
public Student findStudentBysid(int sid);
// Pojo 和 Map 可以进行多参查询
// 注解的多参特有的形式
@Select("select * from student "
+ " where ssex=#{k} and classid = #{classid} "
+ " limit #{curpage},#{sizepage}")
public List<Student> findStudentByssexandClassid(
@Param("k") String sex, @Param("classid") int cid,
@Param("curpage") int curpage,
@Param("sizepage") int sizepage
);
6,@Results 结果映射
语法:@Results({ @Result(id = 是否为主键, column = "字段", property = "属性" ) })
@Results(id = "smMap",value = {
@Result(column = "smid",property = "smid"),
@Result(column = "sm_name",property = "smname"),
@Result(column = "smsex",property = "smsex")
})
@Select("select * from schoolmaster")
@ResultMap("smMap") // 方法外去找映射
public List<Smaster> findSm();
7,一对一映射
// one 一对一的表联查 @One(select="能得到属性对象的反射字符串",fetchType="加载机制")
@Results({
@Result(column = "classid", property = "classid"),
@Result(column = "classid",property = "bj",
one=@One(
select = "com.ape.mapper.BanjiMapper.findBanjiByClassid")
)
})
@Select("select * from student")
public List<Student> findAllStudent();
@Select("select * from class where classid = #{v}")
public Banji findBanjiByClassid(int classid);
8,多表映射
// many 一对多的映射 @Many(select ="通过字符串反射得到的集合对象")
@Results({
@Result(column ="classid",property = "classid"),
@Result(column = "classid",property = "slist",
many = @Many(
select = "com.ape.mapper.StudentMapper.findStudentByClassid")
)
})
@Select("select * from class")
public List<Banji> findAllBanji();
@Select("select * from student where classid = #{v}")
public List<Student> findStudentByClassid(int classid);
9,动态SQL注解
(1)脚本SQL
// sql脚本
// <script></script>
@Select("<script>"
+ "select * from student "
+ "<where>"
+ "<if test=\"ssex != null\"> and ssex = #{ssex} </if>"
+ "<if test=\"classid != 0\"> and classid = #{classid} </if>"
+ "</where>"
+ "</script>")
public List<Student> findStudentScript(Student s);
(2)方法中构建SQL
// 方法构建sql语句
// @SelectProvider (type = 内部类.class, method = "方法名")
// @InsertProvider
// @UpdateProvider
// @DeleteProvider
// 不推荐
@SelectProvider(type = StuSql.class ,method = "selectFunc")
public List<Student> findStudentFun(Student s);
class StuSql{
public String selectFunc(Student s) {
String sql = "select * from student where 1=1";
if(s.getClassid() != 0) {
sql += " and classid = #{classid}";
}
if(s.getSsex() != null) {
sql += " and ssex = #{ssex}";
}
return sql;
}
}
(3)SQL语句构造器
// sql构造器
// 官方推荐的方式
// 方法构建sql语句
// @SelectProvider (type = 内部类.class, method = "方法名")
// @InsertProvider
// @UpdateProvider
// @DeleteProvider
@SelectProvider(type=StuSql.class, method = "selectPro")
public List<Student> findStudentProvider(Student s);
class StuSql{
public String selectPro(Student s) {
return new SQL() {
{
// SELECT("sid,sname,birthday"); // 字段
// SELECT("ssex");
// SELECT("classid");
SELECT("*");
FROM("student");
if(s.getClassid() != 0) {
WHERE("classid = #{classid}");
}
if(s.getSsex() != null) {
OR(); WHERE("ssex = #{ssex}");
}
}
}.toString();
}