Mybatis-plus 一對多關聯查詢,附JRebel熱加載mapper.xml


1 public class Person extends PersonOthers{}
2 public class PersonOthers{private List<Map<String,String>> otherList;}

 

 1 public class PersonService {
 2 
 3     @Resource
 4     private PersonDao dao;
 5     
 6     public List<Person> selectPlusQuery(QueryPersonDto query, Page<Person> page) {
 7         String sql = "SELECT * FROM t_person_info";
 8         QueryWrapper<Person> qw = new QueryWrapper<>();
 9         if (null != query) {
10             String keyword = query.getKeyword();
11             if (StringUtils.isNotBlank(keyword)) qw.like("person_name", keyword);
12         }
13         return dao.selectPlusQuery(sql, page, qw);
14     }
15 }
@Service
@Repository
public interface PersonDao {
    /**
     * Mybatis注解 + xml(沒法省?) + mybatis-plus 條件查詢、分頁 
     * @param sql   自定義sql
     * @param page  分頁查詢
     * @param queryWrapper  mybatis-plus條件構造器
     */
    @Results({
            @Result(property = "otherList", javaType = List.class, column = "pid"
                    ,many = @Many(select = "selectOtherByPid"))
    })
    @Select("${sql} ${ew.customSqlSegment}")
    List<Person> selectPlusQuery(@Param("sql") String sql, Page<Person> page,
                                      @Param(Constants.WRAPPER) QueryWrapper<Person> queryWrapper);
}
@Mapper
<resultMap id="BaseResultMap" type="*.Person">
    <id column="id" property="id"/>
    <result column="pid" property="pid"/>
    <!-- ... -->
    <collection property="otherList" javaType="List" ofType="map"
                select="selectOtherByPid" column="pid">
        <!--    javaType:對應Person屬性otherList的類型,可以不寫;
                ofType:對應嵌套sql中的resultType;
                select: 對應文件中的select標簽id;
                column:對應主表中的字段(嵌套sql中的參數字段:#{pid});
                -->
    </collection>
</resultMap>
<select id="selectOtherByPid" resultType="map">
    SELECT * FROM t_other AS o WHERE o.pid = #{pid}
</select>
mapper.xml

 

jrebel-mybatisplus 下載地址 ,然后在git命令窗依次執行以下命令:

1 git clone */jrebel-mybatisplus.git
2 cd jrebel-mybatisplus
3 mvn -f pom.xml clean package

將構建好的插件jrebel-mybatisplus\target\jr-mybatisplus-*.jar拷貝至任意目錄dir

修改運行配置,增加VM參數:-Drebel.plugins=dir\jr-mybatisplus-*.jar,然后以JRebel方式啟動


修改你項目中的mapper xml 文件后,重新編譯,如果重新請求接口,你應該會看到控制台輸出 “Reloading SQL maps”


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM