java 中MyBatis注解映射的實(shí)例詳解
java 中MyBatis注解映射的實(shí)例詳解
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.關(guān)系映射
1),一對(duì)一
@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)一對(duì)多
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.動(dòng)態(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);
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Maven導(dǎo)入依賴時(shí)報(bào)錯(cuò)如何解決
這篇文章主要介紹了Maven導(dǎo)入依賴時(shí)報(bào)錯(cuò)如何解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Java基于NIO實(shí)現(xiàn)群聊系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java基于NIO實(shí)現(xiàn)群聊系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Java如何調(diào)用wsdl的webservice接口
這篇文章主要介紹了Java如何調(diào)用wsdl的webservice接口問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Java編程二項(xiàng)分布的遞歸和非遞歸實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java編程二項(xiàng)分布的遞歸和非遞歸實(shí)現(xiàn)代碼實(shí)例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
EasyUi+Spring Data 實(shí)現(xiàn)按條件分頁(yè)查詢的實(shí)例代碼
這篇文章主要介紹了EasyUi+Spring Data 實(shí)現(xiàn)按條件分頁(yè)查詢的實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07
解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.
這篇文章主要介紹了解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.config.DynamicPropertyFactory的問(wèn)題,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java 創(chuàng)建URL的常見問(wèn)題及解決方案
這篇文章主要介紹了Java 創(chuàng)建URL的常見問(wèn)題及解決方案的相關(guān)資料,需要的朋友可以參考下2016-10-10

