最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解

 更新時(shí)間:2022年07月27日 14:01:50   作者:code-x  
這篇文章主要為大家介紹了MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1. 自定義SqlSession

@Slf4j
public class GenericSqlSession extends DefaultSqlSession {
    private static final ThreadLocal<Class<?>> CTX = new ThreadLocal<>();
    private final Executor generalExecutor;
    public GenericSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
        super(configuration, executor, autoCommit);
        this.generalExecutor = executor;
    }
    @Override
    public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
        return doSelectList(statement, parameter, rowBounds);
    }
    protected <E> List<E> doSelectList(String statement, Object parameter, RowBounds rowBounds) {
        try {
            return generalExecutor.query(getCustomMappedStatement(statement),
                    ParamNameResolver.wrapToMapIfCollection(parameter, null), rowBounds, Executor.NO_RESULT_HANDLER);
        } catch (Exception e) {
            throw ExceptionFactory.wrapException("Error querying database.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
    protected MappedStatement getCustomMappedStatement(String statement) {
        var ms = getConfiguration().getMappedStatement(statement);
        var clazz = GenericSqlSession.get();
        if (ObjectUtil.isEmpty(clazz)) {
            return ms;
        } else {
            var resultMaps = ms.getResultMaps();
            var resultMap = resultMaps.get(0);
            var customMap = new ResultMap.Builder(getConfiguration(), resultMap.getId(),
                    clazz, resultMap.getResultMappings(), resultMap.getAutoMapping()).build();
            return new MappedStatement.Builder(getConfiguration(), ms.getId(), ms.getSqlSource(), ms.getSqlCommandType())
                    .resultMaps(Collections.singletonList(customMap))
                    .resource(ms.getResource())
                    .useCache(ms.isUseCache())
                    .build();
        }
    }
    public static void set(Class<?> clazz) {
        CTX.set(clazz);
    }
    public static Class<?> get() {
        return CTX.get();
    }
    public static void remove() {
        CTX.remove();
    }
}

2. 自定義SqlSessionFactory

public class GenericSqlSessionFactory extends DefaultSqlSessionFactory {
    public GenericSqlSessionFactory(Configuration configuration) {
        super(configuration);
    }
    @Override
    public SqlSession openSession(ExecutorType execType) {
        Transaction tx = null;
        try {
            final var environment = getConfiguration().getEnvironment();
            final var transactionFactory = getTransactionFactoryFromEnvironment(environment);
            tx = transactionFactory.newTransaction(environment.getDataSource(), null, false);
            final var executor = getConfiguration().newExecutor(tx, execType);
            return new GenericSqlSession(getConfiguration(), executor, false);
        } catch (Exception e) {
            // may have fetched a connection so let's call close()
            closeTransaction(tx);
            throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
        } finally {
            ErrorContext.instance().reset();
        }
    }
    private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {
        if (environment == null || environment.getTransactionFactory() == null) {
            return new ManagedTransactionFactory();
        }
        return environment.getTransactionFactory();
    }
    private void closeTransaction(Transaction tx) {
        if (tx != null) {
            try {
                tx.close();
            } catch (SQLException ignore) {
                // Intentionally ignore. Prefer previous error.
            }
        }
    }
}

3. 自定義SqlSessionTemplate

@Component
public class GenericSqlSessionTemplate extends SqlSessionTemplate {
    public GenericSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        super(new GenericSqlSessionFactory(sqlSessionFactory.getConfiguration()));
    }
}

4. 自定義基礎(chǔ)Mapper

public interface SuperMapper<T> extends BaseMapper<T> {
    /**
     * selectById
     *
     * @param clazz 自定義結(jié)果集class
     * @param id    id
     * @param <D>   D
     * @return D
     */
    @SuppressWarnings("unchecked")
    default <D> D selectById(Class<D> clazz, Serializable id) {
        try {
            GenericSqlSession.set(clazz);
            return (D) selectById(id);
        } finally {
            GenericSqlSession.remove();
        }
    }
}

5. 使用

繼承自定義的基礎(chǔ)Mapper

@Data
@TableName("tag")
public class TagPO implements Serializable {
    private static final long serialVersionUID = 1L;
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
}
public interface TagDAO extends SuperMapper<TagPO> {
}
@Component
@Slf4j
public class MybatisTest implements CommandLineRunner {
    @Autowired
    TagDAO tagDAO;
    @Override
    public void run(String... args) throws Exception {
        TagDTO id1 = tagDAO.selectById(TagDTO.class, 1);
        log.info("{}", id1);
        TagPO id2 = tagDAO.selectById(1);
        log.info("{}", id2);
    }
}

以上就是MyBatis-Plus動(dòng)態(tài)返回實(shí)體類示例詳解的詳細(xì)內(nèi)容,更多關(guān)于MyBatis-Plus返回實(shí)體類的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java代理模式實(shí)例分析

    Java代理模式實(shí)例分析

    這篇文章主要介紹了Java代理模式,結(jié)合實(shí)例形式對(duì)比分析了java代理模式的使用方法與相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • Spring教程之refresh()執(zhí)行邏輯淺析

    Spring教程之refresh()執(zhí)行邏輯淺析

    這篇文章主要給大家介紹了關(guān)于Spring教程之refresh()執(zhí)行邏輯的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java基于Tcp的基礎(chǔ)聊天功能實(shí)例

    Java基于Tcp的基礎(chǔ)聊天功能實(shí)例

    這篇文章主要介紹了Java基于Tcp的基礎(chǔ)聊天功能,結(jié)合實(shí)例形式分析了java基于tcp協(xié)議的數(shù)據(jù)傳輸實(shí)現(xiàn)聊天功能相關(guān)操作技巧,需要的朋友可以參考下
    2020-01-01
  • Scala中Array和List的區(qū)別說明

    Scala中Array和List的區(qū)別說明

    這篇文章主要介紹了Scala中Array和List的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中的split使用方法詳解

    Java中的split使用方法詳解

    這篇文章主要介紹了Java中的split使用方法詳解,Java 中 String 的 split 方法可以將字符串根據(jù)指定的間隔進(jìn)行切割,經(jīng)過切割后得到的返回值是一個(gè)字符串?dāng)?shù)組,需要的朋友可以參考下
    2023-10-10
  • mybatis-plus 使用Condition拼接Sql語句各方法的用法

    mybatis-plus 使用Condition拼接Sql語句各方法的用法

    這篇文章主要介紹了mybatis-plus 使用Condition拼接Sql語句各方法的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 詳解Spring Boot2 Webflux的全局異常處理

    詳解Spring Boot2 Webflux的全局異常處理

    這篇文章主要介紹了詳解Spring Boot2 Webflux的全局異常處理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • springboot?集成activemq項(xiàng)目配置方法

    springboot?集成activemq項(xiàng)目配置方法

    這篇文章主要介紹了springboot?集成activemq項(xiàng)目配置方法,e-car項(xiàng)目配置通過引入activemq依賴,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • SpringBoot實(shí)現(xiàn)攔截器、過濾器、監(jiān)聽器過程解析

    SpringBoot實(shí)現(xiàn)攔截器、過濾器、監(jiān)聽器過程解析

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)攔截器、過濾器、監(jiān)聽器過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java使用jxl庫輕松玩轉(zhuǎn)Excel表操作

    Java使用jxl庫輕松玩轉(zhuǎn)Excel表操作

    jxl?是一個(gè)非常實(shí)用的?Java?庫,專門用于操作?Excel?表格,這篇文章主要為大家介紹了如何使用jxl進(jìn)行Excel的基本操作,有需要的可以了解下
    2025-02-02

最新評(píng)論

东山县| 潜山县| 格尔木市| 珲春市| 航空| 辉县市| 湄潭县| 威远县| 泰宁县| 休宁县| 板桥市| 象州县| 锡林郭勒盟| 邯郸县| 精河县| 晋州市| 偃师市| 江华| 高州市| 闻喜县| 鄂州市| 特克斯县| 开封县| 河西区| 佛坪县| 辽中县| 内黄县| 阳泉市| 饶阳县| 攀枝花市| 鹤峰县| 临武县| 汪清县| 咸阳市| 温宿县| 深州市| 天峻县| 海晏县| 连平县| 呼伦贝尔市| 丽江市|