java 注解 mapping_java 中MyBatis注解映射的实例详解_java_脚本之家

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.结果集映射

@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 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 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 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 getStudentsByGradeId(int gradeId);

}

4.动态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 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 getStudents(Map map);

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值