MyBatis-Plus动态返回实体类示例详解
更新时间:2022年07月27日 14:01:50 作者:code-x
这篇文章主要为大家介绍了MyBatis-Plus动态返回实体类示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
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. 自定义基础Mapper
public interface SuperMapper<T> extends BaseMapper<T> { /** * selectById * * @param clazz 自定义结果集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. 使用
继承自定义的基础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动态返回实体类示例详解的详细内容,更多关于MyBatis-Plus返回实体类的资料请关注脚本之家其它相关文章!
相关文章
mybatis(mybatis-plus)映射文件(XML文件)中特殊字符转义的实现
XML 文件在解析时会将五种特殊字符进行转义,本文主要介绍了mybatis(mybatis-plus)映射文件(XML文件)中特殊字符转义的实现,具有一定的参考价值,感兴趣的可以了解一下2023-12-12SpringBoot4.5.2 整合HikariCP 数据库连接池操作
这篇文章主要介绍了SpringBoot4.5.2 整合HikariCP 数据库连接池操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09SpringBoot中使用Session共享实现分布式部署的示例代码
这篇文章主要介绍了SpringBoot中使用Session共享实现分布式部署的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07
最新评论