Java Code Examples for org.mybatis.spring.SqlSessionFactoryBean#setConfiguration()
The following examples show how to use
org.mybatis.spring.SqlSessionFactoryBean#setConfiguration() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataSourceConfig.java From springbootexamples with Apache License 2.0 | 6 votes |
/** * 多数据源的 SqlSessionFactory * @param dynamicDataSource * @return * @throws Exception */ @Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dynamicDataSource); //设置数据数据源的Mapper.xml路径 bean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")); //设置Mybaties查询数据自动以驼峰式命名进行设值 org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session .Configuration(); configuration.setMapUnderscoreToCamelCase(true); bean.setConfiguration(configuration); return bean.getObject(); }
Example 2
Source File: DataSource1Config.java From Resource with GNU General Public License v3.0 | 6 votes |
@Bean(name = "db1SqlSessionFactory") public SqlSessionFactory setSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource, @Qualifier("db1Configuration") org.apache.ibatis.session.Configuration configuration) throws Exception { String url = "classpath:com/zsm/mapper/db1/*.xml"; SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setConfiguration(configuration); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(url)); org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration(); config.setMapUnderscoreToCamelCase(true); // 开启驼峰命名支持 bean.setConfiguration(config); //格式化sql语句打印 PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); Properties properties = new Properties(); properties.setProperty("format", "true"); performanceInterceptor.setProperties(properties); bean.setPlugins(new Interceptor[] {performanceInterceptor}); return bean.getObject(); }
Example 3
Source File: DataSource2Config.java From Resource with GNU General Public License v3.0 | 6 votes |
@Bean(name = "db2SqlSessionFactory") public SqlSessionFactory setSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource, @Qualifier("db2Configuration") org.apache.ibatis.session.Configuration configuration) throws Exception { String url = "classpath:com/zsm/mapper/db2/*.xml"; SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setConfiguration(configuration); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(url)); org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration(); config.setMapUnderscoreToCamelCase(true); // 开启驼峰命名支持 bean.setConfiguration(config); //格式化sql语句打印 PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); Properties properties = new Properties(); properties.setProperty("format", "true"); performanceInterceptor.setProperties(properties); bean.setPlugins(new Interceptor[] {performanceInterceptor}); return bean.getObject(); }
Example 4
Source File: MybatisConfig1.java From spring-boot-inside with MIT License | 5 votes |
@Bean(name = "sqlSessionFactory1") public SqlSessionFactory sqlSessionFactory1(DataSource dataSource1) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); org.apache.ibatis.session.Configuration ibatisConfiguration = new org.apache.ibatis.session.Configuration(); sqlSessionFactoryBean.setConfiguration(ibatisConfiguration); sqlSessionFactoryBean.setDataSource(dataSource1); sqlSessionFactoryBean.setTypeAliasesPackage("sample.mybatis.domain"); return sqlSessionFactoryBean.getObject(); }
Example 5
Source File: BaseDataSourceConfig.java From spring-boot-tutorials with Apache License 2.0 | 5 votes |
public SqlSessionFactory getSqlSessionFactory(DataSource dataSourceTwo, String location) throws Exception { final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSourceTwo); sqlSessionFactoryBean.setConfiguration(configuration()); sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(location)); return sqlSessionFactoryBean.getObject(); }
Example 6
Source File: MybatisAutoConfiguration.java From spring-boot-mybatis-rw with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); Interceptor rwplugin = new RWPlugin(); if (StringUtils.hasText(this.properties.getConfigLocation())) { factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); } factory.setConfiguration(properties.getConfiguration()); if (ObjectUtils.isEmpty(this.interceptors)) { Interceptor[] plugins = { rwplugin }; factory.setPlugins(plugins); } else { List<Interceptor> interceptorList = new ArrayList<Interceptor>(Arrays.asList(interceptors)); interceptorList.add(rwplugin); factory.setPlugins(interceptorList.toArray(new Interceptor[interceptorList.size()])); } if (this.databaseIdProvider != null) { factory.setDatabaseIdProvider(this.databaseIdProvider); } if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); } if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); } if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { factory.setMapperLocations(this.properties.resolveMapperLocations()); } factory.setDataSource(dataSource); return factory.getObject(); }
Example 7
Source File: MybatisConfig.java From genericdao with Artistic License 2.0 | 5 votes |
@Bean public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource , Environment environment) throws IOException{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean() ; sqlSessionFactoryBean.setDataSource(dataSource); //别名包 sqlSessionFactoryBean.setTypeAliasesPackage(environment.getRequiredProperty(PROPERTY_MYBATIS_ALIAS_PACKAGE)); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver() ; //获取mybatis映射文件路径,classpath:mybatis/mybatis-config.xml if(environment.containsProperty(PROPERTY_MYBATIS_CONFIG)){ sqlSessionFactoryBean.setConfigLocation(resolver.getResource(environment.getRequiredProperty(PROPERTY_MYBATIS_CONFIG))); } //获取mybatis映射文件路径,例:classpath*:com/framework/sample/mapper/*Mapper.xml sqlSessionFactoryBean.setMapperLocations(resolver.getResources(environment.getRequiredProperty(PROPERTY_MYBATIS_MAPPER_LOCATION))); //开启后将在启动时检查设定的parameterMap,resultMap是否存在,是否合法 sqlSessionFactoryBean.setFailFast(true) ; //存储mybatis的插件 List<Interceptor> interceptorList = new ArrayList<Interceptor>() ; //初始化mybatis的插件 initPlugins(interceptorList , environment) ; //设置mybatis插件 sqlSessionFactoryBean.setPlugins(interceptorList.toArray(new Interceptor[]{})); //创建自定义的的Configuration Configuration configuration = new Configuration() ; //设置ObjectWrapperFactory,在其中映射mybatis查询结果和jpa实体 configuration.setObjectWrapperFactory(new JPAObjectWrapperFactory()); sqlSessionFactoryBean.setConfiguration(configuration); return sqlSessionFactoryBean ; }
Example 8
Source File: SpringAnnotationTest.java From Mapper with MIT License | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration(); configuration.setMapperHelper(new MapperHelper()); sessionFactory.setConfiguration(configuration); return sessionFactory.getObject(); }
Example 9
Source File: DatabaseConfig.java From java-master with Apache License 2.0 | 4 votes |
@Bean @SuppressWarnings("ALL") public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); //分页插件 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("reasonable", "true"); properties.setProperty("supportMethodsArguments", "true"); properties.setProperty("returnPageInfo", "check"); properties.setProperty("params", "count=countSql"); properties.setProperty("dialect", "MySQL"); pageHelper.setProperties(properties); sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper}); final String mapperLocation = "classpath*:mapper/**/*.xml"; sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources(mapperLocation)); // 只指定包名,则mybatis会自动为 JavaBean 注册一个小写字母开头的非完全限定的类名形式的别名 sqlSessionFactoryBean.setTypeAliasesPackage("org.javamaster.b2c.core.entity"); org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(); configuration.setLogImpl(Slf4jImpl.class); TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); sqlSessionFactoryBean.setConfiguration(configuration); // 找到EnumBase接口所在的包下所有实现该接口的枚举类 Set<Class<?>> set = ClassUtils.getAllClassesFromPackage(EnumBase.class.getPackage().getName()) .stream() .filter(clz -> clz.isEnum() && EnumBase.class.isAssignableFrom(clz)) .collect(Collectors.toSet()); // 动态注册所有实现了EnumBase接口枚举类的类型转换器到Mybatis set.forEach(enumClass -> { EnumBaseTypeHandler handler = new EnumBaseTypeHandler(enumClass); typeHandlerRegistry.register(enumClass, JdbcType.TINYINT, handler); typeHandlerRegistry.register(enumClass, null, handler); }); return sqlSessionFactoryBean.getObject(); }