java 中MyBatis注解映射的实例详解!
java 中MyBatis注解映射的实例详解
1.普通映射
@Select("select*frommybatis_Studentwhereid=#{id}")
publicStudentgetStudent(intid);
@Insert("insertintomybatis_Student(name,age,remark,pic,grade_id,address_id)values(#{name},#{age},#{remark},#{pic},#{grade.id},#{address.id})")
publicintinsert(Studentstudent);
@Update("updatemybatis_Studentsetname=#{name},age=#{age}whereid=#{id}")
publicintupdate(Studentstudent);
@Delete("deletefrommybatis_Studentwhereid=#{id}")
publicintdelete(intid);
2.结果集映射
@Select("select*frommybatis_Student")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="name",column="name"),
@Result(property="age",column="age")
})
publicListgetAllStudents();
3.关系映射
1),一对一
@Select("select*frommybatis_Student")
@Results({
@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"))
})
publicListgetAllStudents();
2)一对多
packagecom.skymr.mybatis.mappers;
importorg.apache.ibatis.annotations.Many;
importorg.apache.ibatis.annotations.Result;
importorg.apache.ibatis.annotations.Results;
importorg.apache.ibatis.annotations.Select;
importcom.skymr.mybatis.model.Grade;
publicinterfaceGrade2Mapper{
@Select("select*frommybatis_gradewhereid=#{id}")
@Results({
@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"))
})
publicGradegetGrade(intid);
}
Java代码
packagecom.skymr.mybatis.mappers;
importjava.util.List;
importorg.apache.ibatis.annotations.Delete;
importorg.apache.ibatis.annotations.Insert;
importorg.apache.ibatis.annotations.One;
importorg.apache.ibatis.annotations.Result;
importorg.apache.ibatis.annotations.Results;
importorg.apache.ibatis.annotations.Select;
importorg.apache.ibatis.annotations.Update;
importcom.skymr.mybatis.model.Student;
publicinterfaceStudent2Mapper{
@Select("select*frommybatis_Studentwhereid=#{id}")
publicStudentgetStudent(intid);
@Insert("insertintomybatis_Student(name,age,remark,pic,grade_id,address_id)values(#{name},#{age},#{remark},#{pic},#{grade.id},#{address.id})")
publicintinsert(Studentstudent);
@Update("updatemybatis_Studentsetname=#{name},age=#{age}whereid=#{id}")
publicintupdate(Studentstudent);
@Delete("deletefrommybatis_Studentwhereid=#{id}")
publicintdelete(intid);
@Select("select*frommybatis_Student")
@Results({
@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"))
})
publicListgetAllStudents();
@Select("select*frommybatis_Studentwheregrade_id=#{gradeId}")
@Results({
@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"))
})
publicListgetStudentsByGradeId(intgradeId);
}
4.动态sql注解映射
provider类
packagecom.skymr.mybatis.mappers.provider;
importjava.util.Map;
importorg.apache.ibatis.jdbc.SQL;
importcom.skymr.mybatis.model.Student;
publicclassStudentDynaSqlProvider{
publicStringinsertStudent(finalStudentstudent){
returnnewSQL(){
{
INSERT_INTO("mybatis_Student");
if(student.getName()!=null){
VALUES("name","#{name}");
}
if(student.getAge()>0){
VALUES("age","#{age}");
}
}
}.toString();
}
publicStringupdateStudent(finalStudentstudent){
returnnewSQL(){
{
UPDATE("mybatis_Student");
if(student.getName()!=null){
SET("name=#{name}");
}
if(student.getAge()>0){
SET("age=#{age}");
}
WHERE("id=#{id}");
}
}.toString();
}
publicStringgetStudent(finalMapmap){
returnnewSQL(){
{
SELECT("*");
FROM("mybatis_Student");
if(map.containsKey("name")){
WHERE("namelike#{name}");
}
if(map.containsKey("age")){
WHERE("age=#{age}");
}
}
}.toString();
}
publicStringdeleteStudent(){
returnnewSQL(){
{
DELETE_FROM("mybatis_Student");
WHERE("id=#{id}");
}
}.toString();
}
}
Mapper接口
@SelectProvider(type=StudentDynaSqlProvider.class,method="getStudent") publicListgetStudents(Map map);
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!