java 中MyBatis注解映射的實例詳解
java 中MyBatis注解映射的實例詳解
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.結(jié)果集映射
@Select("select * from mybatis_Student")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="name",column="name"),
@Result(property="age",column="age")
})
public List<Student> getAllStudents();
3.關系映射
1),一對一
@Select("select * from mybatis_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"))
})
public List<Student> getAllStudents();
2)一對多
package com.skymr.mybatis.mappers;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import com.skymr.mybatis.model.Grade;
public interface Grade2Mapper {
@Select("select * from mybatis_grade where id=#{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"))
})
public Grade getGrade(int id);
}
Java代碼
package com.skymr.mybatis.mappers;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.skymr.mybatis.model.Student;
public interface Student2Mapper {
@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);
@Select("select * from mybatis_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"))
})
public List<Student> getAllStudents();
@Select("select * from mybatis_Student where grade_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"))
})
public List<Student> getStudentsByGradeId(int gradeId);
}
4.動態(tài)sql注解映射
provider類
package com.skymr.mybatis.mappers.provider;
import java.util.Map;
import org.apache.ibatis.jdbc.SQL;
import com.skymr.mybatis.model.Student;
public class StudentDynaSqlProvider {
public String insertStudent(final Student student){
return new SQL(){
{
INSERT_INTO("mybatis_Student");
if(student.getName() != null){
VALUES("name","#{name}");
}
if(student.getAge() > 0){
VALUES("age","#{age}");
}
}
}.toString();
}
public String updateStudent(final Student student){
return new SQL(){
{
UPDATE("mybatis_Student");
if(student.getName() != null){
SET("name=#{name}");
}
if(student.getAge() > 0){
SET("age=#{age}");
}
WHERE("id=#{id}");
}
}.toString();
}
public String getStudent(final Map<String,Object> map){
return new SQL(){
{
SELECT("*");
FROM("mybatis_Student");
if(map.containsKey("name")){
WHERE("name like #{name}");
}
if(map.containsKey("age")){
WHERE("age=#{age}");
}
}
}.toString();
}
public String deleteStudent(){
return new SQL(){
{
DELETE_FROM("mybatis_Student");
WHERE("id=#{id}");
}
}.toString();
}
}
Mapper接口
@SelectProvider(type=StudentDynaSqlProvider.class,method="getStudent") public List<Student> getStudents(Map<String,Object> map);
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
SpringCloud:feign對象傳參和普通傳參及遇到的坑解決
這篇文章主要介紹了SpringCloud:feign對象傳參和普通傳參及遇到的坑解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java實現(xiàn)把窗體隱藏到系統(tǒng)托盤方法
這篇文章主要介紹了Java實現(xiàn)把窗體隱藏到系統(tǒng)托盤方法,本文直接給出核心功能代碼,需要的朋友可以參考下2015-05-05
解決java攔截器獲取POST入?yún)е翤RequestBody參數(shù)丟失問題
文章講述了在Java開發(fā)中使用攔截器獲取POST請求入?yún)r,由于流關閉導致`@RequestBody`參數(shù)丟失的問題,并提出了一種解決方案,解決方案包括自定義方法、防止流丟失、過濾器和攔截器的合理組織和使用,最終確保了參數(shù)的正確傳遞2024-11-11
Spring?Boot實現(xiàn)WebSocket實時通信
本文主要介紹了Spring?Boot實現(xiàn)WebSocket實時通信,包含實現(xiàn)實時消息傳遞和群發(fā)消息等功能,具有一定的參考價值,感興趣的可以了解一下2024-05-05
完美解決MybatisPlus插件分頁查詢不起作用總是查詢?nèi)繑?shù)據(jù)問題
這篇文章主要介紹了解決MybatisPlus插件分頁查詢不起作用總是查詢?nèi)繑?shù)據(jù)問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
springboot2如何禁用自帶tomcat的session功能
這篇文章主要介紹了springboot2如何禁用自帶tomcat的session功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
JPA如何將查詢結(jié)果轉(zhuǎn)換為DTO對象
這篇文章主要介紹了JPA如何將查詢結(jié)果轉(zhuǎn)換為DTO對象,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

