1. 程式人生 > >springboot+mybatis專案自動生成

springboot+mybatis專案自動生成

springboot_data_access_demo基於rapid,根據自定義模版生成的基於mybatis+mysql的資料庫訪問示例專案。簡單配置資料庫資訊,配置不同的生成策略生成可以直接執行訪問資料庫的專案,吸取了mybatis generator的動態條件優勢,同時又稍有擴充套件。可以生成簡單易懂的sql,支援大部分單表操作,一般情況下不需要自己動手寫sql。模板可以根據自己需求做相應的修改(如有需要可以提供springboot_data_access_demo專案的生成模板)。支援的方法有:

    /** 根據id查詢 */
    XXXXEntity getByPrimaryKey(Long id);

    /** 根據ids查詢 */
    List<XXXXEntity> getByPrimaryIds(@Param("ids") List<Long> ids);

    /** 根據條件查詢 */
    XXXXEntity getSingleByPredicate(XXXXExample example);

    /** 根據條件查詢所有 */
    List<XXXXEntity> getAllByPredicate(XXXXExample example);

    /** 根據條件查詢 */
    int queryCount(XXXXExample example);

    /** 分頁查詢(配合pageHelper) */
    List<XXXXEntity> getXXXXByPage(XXXXExample example);

    /** 根據key更新除了key以外的其他欄位 */
    int updateByPrimaryKey(@Param("record") XXXXEntity record);

    /** 按條件更新 */
    int updateByPredicate(@Param("record") XXXXEntity record, @Param("example")XXXXExample example);

    /** 新增記錄 */
    int insert(XXXXEntity entity);

    /** 批量新增 */
    int batchInsertXXXX(@Param("list") List<XXXXEntity> list);

生成結構

生成的專案結構:

目前僅支援單個數據庫的配置,後續考慮同時支援多個。此專案是以名為Test的資料庫生成的示例(示例地址)。

  • com.demo.gener.configuration.dbconfig : 包下是以資料庫名 + Configuration命名的資料庫配置類(eg : TestConfiguration)
  • com.demo.gener.entity.test : 名為test的庫下的所有實體(包含實體類 和 predicate(查詢條件))
  • com.demo.gener.mapper.test : mapper介面
  • conf/mybatis/test : mapper對應的xml