MyBatis的基本注解:
增删改查
@Select("select * from teacher")
public List<Teacher> selAll();
//增删改返回值都是int
@Insert("insert into teacher values(default,#{name})")
public int add(Teacher teacher);
@Update("update teacher set name=#{name} where id=#{id}")
public int update(Teacher teacher);
@Delete("delete from teacher where id=#{id}")
public int delete(int id);
注解映射:
1.普通映射
-
@Select("select * from mybatis_Student where id=#{id}")
-
public Student getStudent(int id);
-
@Insert("insert into mybatis_Student (name, age, remark, pic,grade_id,address_id) values (#{name},#{age},#{remark}, #{pic},#{grade.id},#{address.id})")
-
public int insert(Student student);
-
@Update("update mybatis_Student set name=#{name},age=#{age} where id=#{id}")
-
public int update(Student student);
-
@Delete("delete from mybatis_Student where id=#{id}")
-
public int delete(int id);
2.结果集映射
-
@Select("select * from mybatis_Student")
-
-
@Result(id=true,property="id",column="id"),
-
@Result(property="name",column="name"),
-
@Result(property="age",column="age")
-
-
public List<Student> getAllStudents();
3.关系映射
3.1一对一
-
@Select("select * from mybatis_Student")
-
-
@Result(id=true,property="id",column="id"),
-
@Result(property="name",column="name"),
-
@Result(property="age",column="age"),
-
@Result(property="address",column="address_id",one=@One(select="com.skymr.mybatis.mappers.AddressMapper.getAddress"))
-
-
public List<Student> getAllStudents();
3.2一对多
-
@Select("select * from mybatis_grade where id=#{id}")
-
-
@Result(id=true,column="id",property="id"),
-
@Result(column="grade_name",property="gradeName"),
-
@Result(property="students",column="id",many=@Many(select="com.skymr.mybatis.mappers.Student2Mapper.getStudentsByGradeId"))
-
-
public Grade getGrade(int id);
-
注解的使用
一. 查询@Select
二.自动关联@Results
三.引用@Results映射(@ResultMap)
四.关联映射:@One
五. 关联映射:@Many
六.添加(@Insert)并且返回主键
七.修改@Update
八.删除@Delete