在java中怎么对MyBatis注解进行映射
本篇文章为大家展示了在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.结果集映射
@Select("select * from mybatis_Student") @Results({ @Result(id=true,property="id",column="id"), @Result(property="name",column="name"), @Result(property="age",column="age") }) public ListgetAllStudents();
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 ListgetAllStudents();
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 ListgetAllStudents(); @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 Mapmap){ 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 ListgetStudents(Map map);
上述内容就是在java中怎么对MyBatis注解进行映射,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
本文名称:在java中怎么对MyBatis注解进行映射
转载源于:http://pwwzsj.com/article/gpcesj.html