Tag: spring mybatis

动态数据源路由 – 未初始化DataSource路由器

我指的是本文 ,我们可以使用Spring Framework中的AbstractRoutingDataSource动态更改应用程序使用的数据源。 我正在使用Mybatis(3.3.0)和Spring(4.1.6.RELEASE)。 如果从主数据库获取数据时发生exception,我想切换到备份数据库。 在这个例子中,我使用了hsql和mysql db。 RoutingDataSource : public class RoutingDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getTargetDataSource(); } } DataSourceContextHolder : public class DataSourceContextHolder { private static final ThreadLocal contextHolder = new ThreadLocal(); public static void setTargetDataSource(DataSourceEnum targetDataSource) { contextHolder.set(targetDataSource); } public static DataSourceEnum getTargetDataSource() { return (DataSourceEnum) contextHolder.get(); } […]

使用MyBatis的Spring:期望的单个匹配bean但找到2

我一直在使用Spring和MyBatis,它对单个数据库的效果非常好。 我在尝试添加另一个数据库时遇到了困难(请参阅Github上的可重复示例 )。 我正在使用Spring Java配置(即不是XML)。 我见过的大多数示例都展示了如何使用XML实现这一点。 我有两个数据配置类(A和B),如下所示: @Configuration @MapperScan(“io.woolford.database.mapper”) public class DataConfigDatabaseA { @Bean(name=”dataSourceA”) public DataSource dataSourceA() throws SQLException { SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setDriver(new com.mysql.jdbc.Driver()); dataSource.setUrl(“jdbc:mysql://” + dbHostA + “/” + dbDatabaseA); dataSource.setUsername(dbUserA); dataSource.setPassword(dbPasswordA); return dataSource; } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSourceA()); return sessionFactory.getObject(); } […]

使用spring-mybatis进行Spring-boot – 如何强制它记录所有SQL查询

我有一个简单的spring-boot-mybatis应用程序(请记住,请)。 Mybatis仅在发生故障时(在例外情况下)记录SQL查询。 请告诉我,如何强制它将所有SQL查询记录到控制台? 此刻我正在使用slf4j logger(由spring-boot自动配置)。 我找到了这个链接: http : //www.mybatis.org/mybatis-3/logging.html 但是我没有设法遵循它。 首先显示log4j配置,我不确定如果我正确理解:在application.properties配置是否足够? 提前致谢

MyBatis与Guava multimap

我想使用Guava multimap作为带MyBatis的resultMap ,返回一个包含多个一对多条目的集合的结果集,但我无法弄清楚相同的语法。 以下是我的表格示例: +—-+———+———-+————-+ | ID | PART_ID | NAME | PART_FAMILY | +—-+———+———-+————-+ | 1 | 1 | bush | 300 | | 2 | 1 | a-bush | 300 | | 3 | 1 | 300-bush | 300 | | 4 | 2 | nut | 301 | +—-+———+———-+————-+ 我想要一个结果集,以便我有一个以PART_ID为键的Guava多图,以及NAME和PART_FAMILY作为结果。 例如: Index […]