首页 > 其他分享 >MyBatis-Plus 注解方式(一对多、多对一)

MyBatis-Plus 注解方式(一对多、多对一)

时间:2023-01-16 18:34:31浏览次数:35  
标签:name column private Plus Result MyBatis 注解 property id

UserMapper.java

@Repository
public interface UserMapper extends BaseMapper<User> {
	//多对一
    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "age", column = "age"),
            @Result(property = "email", column = "email"),
            @Result(property = "dept", column = "deptid", one = @One(select = "com.atguigu.mybatisplus.mapper.DeptMapper.selectById")),
    })
    @Select("select id, name, age, email, deptid from t_user")
    List<UserVO> getUserDept();

    @Select("select id, name, age, email from t_user where deptid=#{deptid}")
    List<User> getUserByDeptId(@Param("deptid") int deptid);
}

DeptMapper.java

public interface DeptMapper extends BaseMapper<Dept> {
	//一对多
    @Results(value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "users", column = "id", many = @Many(select = "com.atguigu.mybatisplus.mapper.UserMapper.getUserByDeptId")),
    })
    @Select("select id, name from t_dept")
    List<DeptVO> getDeptUser();
}

DeptVO

@Data
public class DeptVO {

    private Integer id;

    private String name;

    private List<User> users;
}

UserVO

@Data
public class UserVO {
    private int id;
    
    private String name;
    
    private int age;
    
    private String email;
    
    private Dept dept;
}

调用

@SpringBootTest
class MybatisplusApplicationTests {

    @Resource
    private IUserService userService;

    @Resource
    private IDeptService deptService;

    @Test
    void contextLoads() {
        List<UserVO> userDept = userService.getUserDept();
        userDept.forEach(System.out::println);

        List<DeptVO> deptUser = deptService.getDeptUser();
        deptUser.forEach(System.out::println);
    }

}

标签:name,column,private,Plus,Result,MyBatis,注解,property,id
From: https://blog.51cto.com/u_14452299/6010730

相关文章

  • MyBatis-Plus通用枚举
    创建通用枚举类型packagecom.atguigu.mp.enums;importcom.baomidou.mybatisplus.annotation.EnumValue;importlombok.Getter;@GetterpublicenumSexEnum{......
  • MyBatis详细使用示例
    MyBatis的查询查询一个实体类对象<!--UsergetUserById(@Param("id")intid);--><selectid="getUserById"resultType="User">select*fromt_userwhe......
  • vue3.2 + element-plus 表单嵌套表格实现动态表单验证
    借鉴了这位兄弟的思想,进行了优化和vue3.2语法糖改造:https://blog.csdn.net/weixin_45295253/article/details/115582504实现:<template><el-formref="formRef":mod......
  • Mybatis Plus 的 page(E page) 方法
    MP的page(Epage)方法1、为何不能直接通过Page<DishDto>pageDto=newPage<>();来查数据库获取相关数据反而要用数据拷贝的方式来将Page<Dish>的和其他的数据拷贝......
  • cropperjs vue3.2 +ts +elment-plus实现图片裁剪上传功能 (复制可用)
    特别鸣谢插件github地址:https://github.com/fengyuanchen/cropper/blob/master/README.md插件在线演示:https://fengyuanchen.github.io/cropperjs/我是基于这个作者代码......
  • Mybatis|MybatisPlus批量插入
    创建一个SpringBoot工程<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XML......
  • springboot反射 + 自定义注解
    利用反射调用方法获取bean寻找bean中指定的方法method(方法名可能匹配,参数类型不匹配,故还要分析参数类型)利用method.invoke方法Spring已经为此实现了完整的机制,......
  • 8.使用注解开发
    在spring4之后,要使用注解开发,必须要保证aop的包导入了<dependencies><!--https://mvnrepository.com/artifact/org.springframework/spring-webmvc--><depend......
  • 什么是注解(Annotation)
    什么是注解(Annotation)Annotation的作用:用于对程序作出解释,可以被其他程序读取Annotation的格式:@注释名//还可以添加一些参数如:@SuppressWarnings(value="unchecked")......
  • (已解决)Mybatis逆向生成时,没有生成根据主键的select、update和delete方法
    使用mybatis逆向生成时,发现mapper接口中没有生成...ByPrimaryKey的方法,在generatorConfig.xml配置文件的数据库连接,即jdbcConnection标签中加入<propertyname="useInf......