mybaties 映射注解 详情
来源:互联网 发布:diy美工刀架 编辑:程序博客网 时间:2024/06/05 06:23
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<Student> getAllStudents();
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();
- 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);
- }
- 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.动态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();
- }
- }
- @SelectProvider(type=StudentDynaSqlProvider.class,method="getStudent")
- public List<Student> getStudents(Map<String,Object> map);
0 0
- mybaties 映射注解 详情
- mybaties 高级结果映射
- mybaties映射文件
- spring常用注解详情
- SpringBoot @Aspect注解详情
- mybaties
- mybaties
- MyBaties
- mybaties基础教程之四:SQL语句映射文件(1)
- mybaties基础教程之四:SQL语句映射文件(2)
- mybaties基础教程之四:SQL语句映射文件(3)
- java中的hibernate和Mybaties中注解的区别
- Mybaties的关联映射(嵌套结果映射,嵌套查询映射)
- spring中IOC和AOP详情注解
- Spring和SpirngMVC的注解详情
- 注解 关联映射
- hibernate注解 关系映射
- Hibernate注解关系映射
- Spring MVC 深入及对注解的详细讲解
- linux【进程学习】笔记——PCB、进程环境、进程原语、虚拟地址……
- 菲波拉契数列的通项公式
- POJ 2155 (二维树状数组)
- hadoop面试百题
- mybaties 映射注解 详情
- 测试用例设计综合策略
- LeetCode-97.Interleaving String
- deeplearning论文学习笔记(1)Convolutional Neural Networks for Sentence Classification
- isKindOfClass 与 isMemberOfClass的区别
- wm8976的初始化函数、写寄存器函数和音量控制函数分析
- rror LNK2001: unresolved external symbol _cvExtractSURF
- 几个常见的DP问题及解法
- vps搭建vpn