Мы можем использовать аннотацию TypeDiscriminator для реализации полиморфизма «SELECT», но код @TypeDiscriminator всегда утомителен.
Поскольку мы можем использовать @ResultMap для замены кода @Results, как мы можем удалить дублирующийся код @TypeDiscriminator?
Проблема очень раздражает, если есть много функций «SELECT», потому что мы должныдобавьте один большой структурированный @TypeDiscriminator для каждого оператора «SELECT».
Пример кода:
@Select("SELECT * FROM material")
@Results(id = "MaterialResultMap", value = {
@Result(property = "labels", column = "label_ids", many = @Many(select = "com.clear.ims4.entity.label.LabelRepository.searchByIds")),
@Result(property = "download", column = "download_id", one = @One(select = "com.clear.ims4.business.material.download.DownloadRepository.searchById")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) })
@TypeDiscriminator(javaType = String.class, column = "type", cases = {
@Case(value = "program", type = Program.class, results = {
@Result(property = "layout", javaType = ProgramLayout.class, column = "{objectName=type,objectId=id}", one = @One(select = "com.clear.ims4.business.material.program.layout.ProgramLayoutRepository.searchByObject")),
@Result(property = "widgets", column = "id", many = @Many(select = "com.clear.ims4.business.material.program.widget.WidgetRepository.searchByProgram")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }),
@Case(value = "office", type = Office.class, results = {
@Result(property = "images", column = "id", many = @Many(select = "com.clear.ims4.business.material.MaterialRepository.searchByMaterial")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }),
@Case(value = "playlist", type = Playlist.class, results = {
@Result(property = "materials", column = "id", many = @Many(select = "com.clear.ims4.business.material.MaterialRepository.searchByMaterial")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }),
@Case(value = "webapp", type = Webapp.class, results = {
@Result(property = "layout", javaType = Layout.class, column = "{objectName=type,objectId=id}", one = @One(select = "com.clear.ims4.business.entity.layout.LayoutRepository.searchByObject")),
@Result(property = "pages", column = "id", many = @Many(select = "com.clear.ims4.business.material.webapp.page.PageRepository.searchByWebapp")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }) })
List<Material> search();
@Select("SELECT * FROM material WHERE id=#{id} LIMIT 1")
@ResultMap(value = "MaterialResultMap")
@TypeDiscriminator(javaType = String.class, column = "type", cases = {
@Case(value = "program", type = Program.class, results = {
@Result(property = "layout", javaType = ProgramLayout.class, column = "{objectName=type,objectId=id}", one = @One(select = "com.clear.ims4.business.material.program.layout.ProgramLayoutRepository.searchByObject")),
@Result(property = "widgets", column = "id", many = @Many(select = "com.clear.ims4.business.material.program.widget.WidgetRepository.searchByProgram")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }),
@Case(value = "office", type = Office.class, results = {
@Result(property = "images", column = "id", many = @Many(select = "com.clear.ims4.business.material.MaterialRepository.searchByMaterial")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }),
@Case(value = "playlist", type = Playlist.class, results = {
@Result(property = "materials", column = "id", many = @Many(select = "com.clear.ims4.business.material.MaterialRepository.searchByMaterial")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }),
@Case(value = "webapp", type = Webapp.class, results = {
@Result(property = "layout", javaType = Layout.class, column = "{objectName=type,objectId=id}", one = @One(select = "com.clear.ims4.business.entity.layout.LayoutRepository.searchByObject")),
@Result(property = "pages", column = "id", many = @Many(select = "com.clear.ims4.business.material.webapp.page.PageRepository.searchByWebapp")),
@Result(property = "id", column = "id", jdbcType = JdbcType.INTEGER, id = true) }) })
Material searchById(@Param("id") Integer id);