# AutoAssociateDemo **Repository Path**: mayan50/auto-associate-demo ## Basic Information - **Project Name**: AutoAssociateDemo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-01-05 - **Last Updated**: 2022-06-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Mybatis-Plus 一对多插件 > 受[MPRelation](https://gitee.com/dreamyoung/mprelation.git)启发完成 > [源码](https://gitee.com/mayan50/auto-associate) > [示例](https://gitee.com/mayan50/auto-associate-demo) ### 注解 ###### @OneToMany 一对多注解 ###### @OneToOne 一对一注解 > 参数 - targetEntity 对应的Mapper class - column 本类关联的字段(数据库的字段名称) - associateColumn 对应关联的字段(数据库的字段名称) - table 表名 (暂时没啥用) - dive 默认为true,一对一或一对多时将递归执行两层,否则为一层 ### 实体类(使用) ```java import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.my.autoassociate.annotation.OneToMany; import lombok.Data; import lombok.ToString; import lombok.experimental.Accessors; import java.util.List; @Data @Accessors(chain = true) @ToString public class Pirates { @TableId(type = IdType.AUTO) private Long id; private String name; private Long captain; private String remark; @TableField(exist = false) @OneToMany(column = "id", associateColumn = "pirates_id") List users; } ``` ```java @Data @Accessors(chain = true) @ToString public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; private Long piratesId; @TableField(exist = false) @OneToOne(column = "pirates## Mybatis-Plus 一对多插件 > 受[MPRelation](https://gitee.com/dreamyoung/mprelation.git)启发完成 > [源码](https://gitee.com/mayan50/auto-associate) > [示例](https://gitee.com/mayan50/auto-associate-demo) ### 注解 ###### @OneToMany 一对多注解 ###### @OneToOne 一对一注解 > 参数 - mapperEntity 对应的Mapper class - column 本类关联的字段(数据库的字段名称) - associateColumn 对应关联的字段(数据库的字段名称) - table 表名 (暂时没啥用) - dive 默认为true,一对一或一对多时将递归执行两层,否则为一层 - sql 这里可使用原生sql(一些复杂关联无法通过以上关联,大量映射写起来很费解,遂开发)使用时参数使用?代替 - condition 原生sql对应参数(没有可以不管),如参数是死值,则传死值,如是对应实体类的值,则为【a:数据库字段名】形式 ### 实体类(使用) ```java import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import OneToMany; import lombok.Data; import lombok.ToString; import lombok.experimental.Accessors; import java.util.List; @Data @Accessors(chain = true) @ToString public class Pirates { @TableId(type = IdType.AUTO) private Long id; private String name; private Long captain; private String remark; @TableField(exist = false) @OneToMany(column = "id", associateColumn = "pirates_id") List users; } ``` ```java @Data @Accessors(chain = true) @ToString public class User { @TableId(type = IdType.AUTO) private Long id; private String name; private Integer age; private Long piratesId; @TableField(exist = false) // @OneToOne(column = "pirates_id", associateColumn = "id") // 形式如此,a:pirates_id 实际执行时会取当前对象的piratesId作为最终参数查询 @OneToOne(sql = "SELECT * FROM `pirates` WHERE id = ?", condition = "a:pirates_id") private Pirates pirates; } ``` ###### @AutoAssociate 自定义方法只需要加上此注解(Service接口中)可自动完成一对一和一对多关联 继承原生IService接口,只需要多配置一个Bean ```java @Configuration public class AutoAssociateConfig { // 自动关联Bean 必须声明 @Bean public AutoAssociate autoAssociate() { //这里需要传入实体类所在包,用于分辨实体类(必须) return new AutoAssociate(new String[]{"com.my.autoassociate.entity"}); } // 如使用原生sql自动关联时需定义 @Bean public DBUtils dbUtils() { return new DBUtils(); } //定义切面Bean @Bean public AutoAssociateAop autoAssociateAop() { return new AutoAssociateAop(); } } ``` ```java import com.baomidou.mybatisplus.extension.service.IService; import com.my.autoassociate.entity.User; public interface UserService extends IService { User findById(Long id); } ``` ```java @Service public class UserServiceImpl extends ServiceImpl implements UserService { @Override @AutoAssociate public User findById(Long id) { return baseMapper.findById(id); } } ``` 配置好bean后原生的方法会自动关联,两种方式输出结果完全相同,第二种使用切面的形式,但相对来说更加灵活,推荐使用(以后主要发展) > 如果有使用的小伙伴有好的意见尽管提出,我可能会修改了 > 没有提交到中央仓库,暂时以本地编译pom的形式接入