Java Code Examples for org.mybatis.spring.SqlSessionFactoryBean#setTypeAliasesPackage()
The following examples show how to use
org.mybatis.spring.SqlSessionFactoryBean#setTypeAliasesPackage() .
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: MybatisAutoConfiguration.java From FS-Blog with Apache License 2.0 | 6 votes |
@Bean(name = "sqlSessionFactory") @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(HikariDataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); if (StringUtils.hasText(this.properties.getConfig())) { factory.setConfigLocation( this.resourceLoader.getResource(this.properties.getConfig())); } else { if (this.interceptors != null && this.interceptors.length > 0) { factory.setPlugins(this.interceptors); } factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); factory.setMapperLocations(this.properties.getMapperLocations()); } return factory.getObject(); }
Example 2
Source File: MybatisConfig.java From ly-security with Apache License 2.0 | 6 votes |
@Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); //mybatis配置 Properties prop = new Properties(); prop.setProperty("mapUnderscoreToCamelCase", "true"); sqlSessionFactoryBean.setConfigurationProperties(prop); sqlSessionFactoryBean.setTypeAliasesPackage("com.tc.ly.bean"); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("classpath:mapper/*.xml"); sqlSessionFactoryBean.setMapperLocations(resources); sqlSessionFactoryBean.setDataSource(dataSource); return sqlSessionFactoryBean; }
Example 3
Source File: SqlSessionFactoryConfig.java From mybatis.flying with Apache License 2.0 | 6 votes |
@Bean(name = "sqlSessionFactory") @Primary public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); /** 设置datasource */ sqlSessionFactoryBean.setDataSource(dataSource1); VFS.addImplClass(SpringBootVFS.class); /** 设置mybatis configuration 扫描路径 */ sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("Configuration.xml")); /** 设置typeAlias 包扫描路径 */ sqlSessionFactoryBean.setTypeAliasesPackage("indi.mybatis.flying"); /** 添加mapper 扫描路径 */ PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] ra1 = resolver.getResources("classpath*:indi/mybatis/flying/mapper*/*.xml"); sqlSessionFactoryBean.setMapperLocations(ra1); // 扫描映射文件 return sqlSessionFactoryBean; }
Example 4
Source File: DeployerConfiguration.java From oneops with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactoryBean getSessionFactoryBean(DataSource dataSource) { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setTypeAliasesPackage("com.oneops.cms.dj.domain"); return sqlSessionFactoryBean; }
Example 5
Source File: MyBatisConfig.java From jim-framework with Apache License 2.0 | 5 votes |
@Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setTypeAliasesPackage("com.jim.dao.generated.entity"); //分页插件 PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("dialect", "postgresql"); properties.setProperty("reasonable", "true"); properties.setProperty("supportMethodsArguments", "true"); properties.setProperty("returnPageInfo", "check"); properties.setProperty("params", "count=countSql"); pageHelper.setProperties(properties); //添加插件 bean.setPlugins(new Interceptor[]{pageHelper}); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); return bean.getObject(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
Example 6
Source File: SqlSessionFactoryConfig.java From micro-service with Apache License 2.0 | 5 votes |
/** * 创建sqlSessionFactoryBean 实例 * 并且设置configtion 如驼峰命名.等等 * 设置mapper 映射路径 * 设置datasource数据源 * @return * @throws Exception */ @Bean(name = "sqlSessionFactory") public SqlSessionFactory createSqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); /** 设置datasource */ bean.setDataSource(dataSource); /** 设置typeAlias 包扫描路径 */ bean.setTypeAliasesPackage(dataSourceProperties.getTypeAliasPackage()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); bean.setMapperLocations(resolver.getResources(dataSourceProperties.getMapperLocations())); return bean.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: WriteOrReadDatabaseConfig.java From demo-project with MIT License | 5 votes |
/** * 多数据源需要自己设置sqlSessionFactory */ @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(routingDataSource()); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); // 实体类对应的位置 bean.setTypeAliasesPackage(typeAliasesPackage); // mybatis的XML的配置 bean.setMapperLocations(resolver.getResources(mapperLocation)); bean.setConfigLocation(resolver.getResource(configLocation)); return bean.getObject(); }
Example 9
Source File: MyBatisUserConfig.java From mini-platform with MIT License | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactoryBeanForUser(@Qualifier("dataSourceUser") DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setTypeAliasesPackage("com.github.hiling.auth.modules.user.model"); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); return factory.getObject(); }
Example 10
Source File: Test2dbConfig.java From storm_spring_boot_demo with MIT License | 5 votes |
@Bean(name = "test2dbSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("test2db") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.maxplus1.demo.entity"); factoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources("classpath:mapper/test2db/*.xml")); return factoryBean.getObject(); }
Example 11
Source File: DataSourceConfig.java From springboot-learning-experience with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource myTestDbDataSource, @Qualifier("slaveDataSource") DataSource myTestDb2DataSource) throws Exception { SqlSessionFactoryBean fb = new SqlSessionFactoryBean(); fb.setDataSource(this.dataSource(myTestDbDataSource, myTestDb2DataSource)); fb.setTypeAliasesPackage(env.getProperty("mybatis.type-aliases-package")); fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapper-locations"))); return fb.getObject(); }
Example 12
Source File: DataConfig.java From playground with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactoryBean sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setTypeAliasesPackage("org.lanyonm.playground.domain"); return sessionFactory; }
Example 13
Source File: MybatisConfigurer.java From mySpringBoot with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setTypeAliasesPackage(ProjectConstant.MODEL_PACKAGE); // 添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml")); return factory.getObject(); }
Example 14
Source File: MybatisTableConfig.java From fabric-net-server with Apache License 2.0 | 5 votes |
@Bean public SqlSessionFactoryBean sqlSessionFactory() throws Exception{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml")); sqlSessionFactoryBean.setTypeAliasesPackage("cn.aberic.fabric.dao.entity.*"); return sqlSessionFactoryBean; }
Example 15
Source File: MybatisConfiguration.java From huanhuan-blog with Apache License 2.0 | 5 votes |
/** * 将mybatis的sqlSessionFactory交给spring管理 */ @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml")); sessionFactory.setTypeAliasesPackage("com.huan.**.model"); return sessionFactory; }
Example 16
Source File: MapperAutoConfiguration.java From Mapper with MIT License | 5 votes |
@Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setVfs(SpringBootVFS.class); if (StringUtils.hasText(this.properties.getConfigLocation())) { factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation())); } applyConfiguration(factory); if (this.properties.getConfigurationProperties() != null) { factory.setConfigurationProperties(this.properties.getConfigurationProperties()); } if (!ObjectUtils.isEmpty(this.interceptors)) { factory.setPlugins(this.interceptors); } if (this.databaseIdProvider != null) { factory.setDatabaseIdProvider(this.databaseIdProvider); } if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) { factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage()); } if (this.properties.getTypeAliasesSuperType() != null) { factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType()); } if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) { factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage()); } if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) { factory.setMapperLocations(this.properties.resolveMapperLocations()); } return factory.getObject(); }
Example 17
Source File: MyBatisConfig.java From server-boilerplate with MIT License | 5 votes |
@Bean(name = "sqlSessionFactory") public SqlSessionFactory sqlSessionFactoryBean() { try { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml")); bean.setMapperLocations(resolver.getResources("classpath:com/boilerplate/server/dao/*.xml")); bean.setTypeAliasesPackage("com.boilerplate.server.dao"); return bean.getObject(); } catch (Exception e) { throw new IllegalStateException("Init sqlSessionFactory failed!", e); } }
Example 18
Source File: MybatisConfig.java From kitty with GNU Lesser General Public License v3.0 | 5 votes |
@Bean public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setTypeAliasesPackage("com.louis.kitty.*.model"); // 扫描Model PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath*:**/sqlmap/*.xml")); // 扫描映射文件 return sessionFactory.getObject(); }
Example 19
Source File: MapperScannerWithSharding.java From tsharding with MIT License | 4 votes |
private void initSqlSessionFactories(ConfigurableListableBeanFactory beanFactory) throws Exception { Map<String, SqlSessionFactory> sqlSessionFactories = new HashMap<>(this.dataSourceLookup.getMapping().size()); ReadWriteSplittingDataSource defaultDataSource = null; SqlSessionFactory defaultSqlSessionFactory = null; for (ReadWriteSplittingDataSource dataSource : this.dataSourceLookup.getMapping().values()) { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setMapperLocations(mapperLocations); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setTypeAliasesPackage(this.packageName + ".domain.entity"); // init 初始化所有sql对应的元数据、资源(sqlNode, sqlSource, mappedStatement)等 sessionFactoryBean.afterPropertiesSet(); if (defaultDataSource == null) { //第一个 defaultDataSource = dataSource; defaultSqlSessionFactory = sessionFactoryBean.getObject(); } else { SqlSessionFactory newSqlSessionFactory = sessionFactoryBean.getObject(); Field conf = newSqlSessionFactory.getClass().getDeclaredField("configuration"); conf.setAccessible(true); Configuration newConfiguration = (Configuration) conf.get(newSqlSessionFactory); Field mappedStatementField = newConfiguration.getClass().getDeclaredField("mappedStatements"); //去掉final修饰符 Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); modifiersField.setInt( mappedStatementField, mappedStatementField.getModifiers() & ~Modifier.FINAL); mappedStatementField.setAccessible(true); //后续的元数据复用 Configuration defaultConfiguration = defaultSqlSessionFactory.getConfiguration(); Map<String, MappedStatement> reUsedMappedStatement = (Map) mappedStatementField.get(defaultConfiguration); mappedStatementField.set(newConfiguration, reUsedMappedStatement); } beanFactory.registerSingleton(dataSource.getName() + "SqlSessionFactory", sessionFactoryBean); sqlSessionFactories.put(dataSource.getName(), sessionFactoryBean.getObject()); defaultSqlSessionFactory = sessionFactoryBean.getObject(); } this.sqlSessionFactoryLookup = new SqlSessionFactoryLookup(sqlSessionFactories); }
Example 20
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(); }