MyBatis详解(5)
- 注解
- 映射器xml配置文件的缺陷:
- 常用注解
- 1.基本注解:实现简单的增删改查操作。
- @Insert 新增
- @Options(useGeneratedKeys = true, keyProperty = "主键属性") 主键回填
- @SelectKey ( statement = "自增规则", keyProperty = "主键属性", resultType = 结果类型, before = true ) 主键自增
- @Delete 删除
- @Update 更新
- @Selete 查询
- 传递多参
- 2.**结果映射注解**:实现结果的映射关系,也可以完成级联映射。
- @Results 结果映射
- 单表
- 多表(相当于两个单表查询,之后再拼接)
- 3.动态SQL注解:实现动态 SQL 的内容 (3种方式)
注解
映射器xml配置文件的缺陷:
1.繁琐:配置文件的书写本身繁琐,需要掌握的内容比较多
2.不直观:配置文件和接口直接只是名称相同,对应起来比较麻烦.
常用注解
1.基本注解:实现简单的增删改查操作。
@Insert 新增
@Options(useGeneratedKeys = true, keyProperty = “主键属性”) 主键回填
@Insert("insert into student(sname,birthday,ssex,classid) "
+ "values(#{sname},#{birthday},#{ssex},#{classid})")
@Options(keyProperty = "sid",useGeneratedKeys = true)
public int addStu(Student s);
@SelectKey ( statement = “自增规则”, keyProperty = “主键属性”, resultType = 结果类型, before = true ) 主键自增
@SelectKey ( statement = "select max(newsid) + 1 as newsId from news", keyProperty = "newsId", resultType = int.class, before = true )
@Insert(...)
public void addNews (News news);
@Delete 删除
@Delete(“delete from student where sid= #{sId} ”)
public int delStudent(int sid);
@Update 更新
@Update(“update student set ssex =#{ssex},classid = #{classid})
public int updateStudent(Student s);
@Selete 查询
@Select("select * from student")
public List<Student> findStudents();
传递多参
方法5:@Param 方式
@Select("select * from student where ssex=#{xingbie} limit #{weizhi},#{sizepage}")
public List<Student> findStudentsBysexpage(
@Param("xingbie") String sex,
@Param("weizhi") int curpage,
@Param("sizepage") int sizepage
);
2.结果映射注解:实现结果的映射关系,也可以完成级联映射。
@Results 结果映射
单表
@Results(id = "stuMap" , value= {
@Result(column = "sid", property = "sid"),
@Result(column = "sname", property = "sname"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "ssex", property = "ssex"),
@Result(column = "classid", property = "classid")
})
@Select("select * from student")
public List<Student> findStudent();
多表(相当于两个单表查询,之后再拼接)
一对一:@one
@Results(id = "stuMap" , value= {
@Result(column = "sid", property = "sid"),
@Result(column = "sname", property = "sname"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "ssex", property = "ssex"),
@Result(column = "classid", property = "classid"),
//一对一
@Result(property = "bj",column = "classid" ,
one = @One(select = "com.ape.mapper.ClassMapper.findBanjiList")
)
})
@Select("select * from student")
public List<Student> findStudentAndClass();
一对多:@many
@Results(id= "class_map" , value = {
@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> findBanjiAndStu();
3.动态SQL注解:实现动态 SQL 的内容 (3种方式)
1)脚本sql:XML配置方式的动态SQL,是用< script >的方式把它照搬过来,用注解来实现。适用于xml配置转换到注解配置
//脚本SQL
@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> findStudents(Student s);
2)方法中构建sql:
//在方法中构建动态SQL
@UpdateProvider(type = StudentSql.class, method = "xiugaiStuSql")
public int updateStudentFun(Student s);
//内部类
class StudentSql{
public String xiugaiStuSql(Student s) {
String sql = "update student set ";
if (s.getSsex() !=null) {
sql += ",ssex =#{ssex}";
}
if (s.getClassid() != 0) {
sql += ",classid =#{classid}";
}
sql += " where sid= #{sid}";
sql = sql.replaceFirst(",", " ");
return sql;
}
}
3)SQL 语句构造器:实现动态 SQL 的内容(推荐)
//SQL构造器
@SelectProvider(type = StudentSql.class , method = "findGZQ")
public List<Student> findStudentsGZQ(Student s);
@InsertProvider(type = StudentSql.class , method = "gzqADD")
public int addStudent(Student s);
@UpdateProvider(type = StudentSql.class , method = "gzqUpdate")
public int updateStudent(Student s);
@DeleteProvider(type = StudentSql.class , method = "gzqDel")
public int delStudent(int sid);
public String findGZQ(Student s) {
return new SQL() {
{
SELECT("*");
FROM("student");
if (s.getSsex() != null) {
WHERE("ssex = #{ssex}");
}
if (s.getClassid() != 0) {
OR();
WHERE("classid = #{classid}");
}
}
}.toString();
}
public String gzqADD(Student s) {
return new SQL() {
{
INSERT_INTO("student");
if (s.getSname() !=null) {
VALUES("sname","#{sname}");
}
if (s.getBirthday() !=null) {
VALUES("birthday","#{birthday}");
}
if (s.getSsex() !=null) {
VALUES("ssex","#{ssex}");
}
if (s.getClassid() !=0) {
VALUES("classid","#{classid}");
}
}
}.toString();
}
public String gzqUpdate(Student s) {
return new SQL() {
{
UPDATE("student");
if (s.getSname() !=null) {
SET("sname = #{sname}");
}
if (s.getBirthday() !=null) {
SET("birthday=#{birthday}");
}
if (s.getSsex() !=null) {
SET("ssex=#{ssex}");
}
if (s.getClassid() !=0) {
SET("classid= #{classid}");
}
WHERE("sid = #{sid}");
}
}.toString();
}
public String gzqDel(int sid) {
return new SQL() {
{
DELETE_FROM("student");
WHERE("sid = #{sid}");
}
}.toString();
}
}