List of usage examples for org.apache.ibatis.session Configuration addMappers
public void addMappers(String packageName)
From source file:com.baifendian.swordfish.dao.datasource.ConnectionFactory.java
License:Apache License
/** * sql session factory//from ww w . j a v a 2s . c o m */ public static SqlSessionFactory getSqlSessionFactory() { if (sqlSessionFactory == null) { synchronized (ConnectionFactory.class) { if (sqlSessionFactory == null) { DataSource dataSource = getDataSource(); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.setLazyLoadingEnabled(true); configuration.addMappers("com.baifendian.swordfish.dao.mapper"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); sqlSessionFactory = builder.build(configuration); } } } return sqlSessionFactory; }
From source file:com.ehensin.paypal.infra.db.DBRepository.java
License:Apache License
private SqlSessionFactory getSqlSessionFactory(Map<String, String> properties) { DataSourceConfig config = new DataSourceConfig(); config.setDbUrl(properties.get("url")); config.setDriver(properties.get("driver")); config.setPartition(Integer.valueOf(properties.get("partions"))); config.setUserName(properties.get("username")); config.setPassword(properties.get("password")); config.setMinConnections(Integer.valueOf(properties.get("minconnection"))); config.setMaxConnections(Integer.valueOf(properties.get("maxconnection"))); DataSource dataSource = DataSourceFactory.getDataSource(config); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("account-service", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); for (String mapper : mappers) { configuration.addMappers(mapper); }/*w ww . java 2s. c om*/ SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); return sqlSessionFactory; }
From source file:com.stone.base.dao.SqlSessionFactoryBean.java
License:Apache License
/** * Build a SqlSessionFactory instance.//from w ww. j a v a 2s . c o m * * The default implementation uses the standard MyBatis * {@link XMLConfigBuilder} API to build a SqlSessionFactory instance based * on an Reader. * * @see org.apache.ibatis.builder.xml.XMLConfigBuilder#parse() * * @return SqlSessionFactory * * @throws IOException * if loading the config file failed * @throws IllegalAccessException * @throws InstantiationException */ protected SqlSessionFactory buildSqlSessionFactory() throws IOException, IllegalAccessException, InstantiationException { XMLConfigBuilder xmlConfigBuilder; Configuration configuration; if (this.configLocation != null) { Reader reader = null; try { reader = new InputStreamReader(this.configLocation.getInputStream()); // Null environment causes the configuration to use the default. // This will be overwritten below regardless. xmlConfigBuilder = new XMLConfigBuilder(reader, null, this.configurationProperties); configuration = xmlConfigBuilder.parse(); } catch (IOException ex) { throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex); } finally { if (reader != null) { try { reader.close(); } catch (IOException ignored) { // close quietly } } } if (this.logger.isDebugEnabled()) { this.logger.debug("Parsed configuration file: '" + this.configLocation + "'"); } } else { if (this.logger.isDebugEnabled()) { this.logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration"); } configuration = new Configuration(); } TransactionFactory transactionFactory = this.transactionFactoryClass.newInstance(); // expose IllegalAccessException, // InstantiationException transactionFactory.setProperties(this.transactionFactoryProperties); Environment environment = new Environment(this.environment, transactionFactory, this.dataSource); configuration.setEnvironment(environment); boolean mapper = false; if (!ObjectUtils.isEmpty(this.mapperLocations)) { Map<String, XNode> sqlFragments = new HashMap<String, XNode>(); for (Resource mapperLocation : this.mapperLocations) { if (mapperLocation == null) { continue; } // MyBatis holds a Map using "resource" name as a key. // If a mapper file is loaded, it searches for a mapper // interface type. // If the type is found then it tries to load the mapper file // again looking for this: // // String xmlResource = type.getName().replace('.', '/') + // ".xml"; // // So if a mapper interface exists, resource cannot be an // absolute path. // Otherwise MyBatis will throw an exception because // it will load both a mapper interface and the mapper xml file, // and throw an exception telling that a mapperStatement cannot // be loaded twice. String path; if (mapperLocation instanceof ClassPathResource) { path = ((ClassPathResource) mapperLocation).getPath(); } else { // this won't work if there is also a mapper interface in // classpath path = mapperLocation.toString(); } Reader reader = null; try { reader = new InputStreamReader(mapperLocation.getInputStream()); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(reader, configuration, path, sqlFragments); xmlMapperBuilder.parse(); } catch (Exception e) { throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException ignored) { } } } if (this.logger.isDebugEnabled()) { this.logger.debug("Parsed mapper file: '" + mapperLocation + "'"); } } mapper = true; } // if(this.mapperPackageName!=null&&!"".equals(this.mapperPackageName)){ // configuration.addMappers(this.mapperPackageName); // mapper = true; // } for (String p : this.mapperPackageNames) { if (p != null && !"".equals(p)) { configuration.addMappers(p); mapper = true; } } if (!mapper) { if (this.logger.isDebugEnabled()) { this.logger.debug( "Property 'mapperLocations' or 'mapperPackageName' was not specified, only MyBatis mapper files specified in the config xml were loaded"); } } return this.sqlSessionFactoryBuilder.build(configuration); }
From source file:org.workspace7.osgi.mybatis.extender.impl.MyBatisMapperRegistry.java
License:Apache License
public void registerBundle(Bundle bundle) { Map<String, List<String>> dsMappers = ExtenderUtil.getMapperClauses(bundle); for (String dsName : dsMappers.keySet()) { Environment environment = registeredEnvironments.get(dsName); Configuration configuration = new Configuration(environment); if (configuration != null) { List<String> packageNames = dsMappers.get(dsName); for (String packageName : packageNames) { String cleanPackageName = packageName.trim(); logger.info("Adding mappers and xmls from package {} to Configuration {}", cleanPackageName, dsName);/*from w w w. ja va 2 s.c o m*/ configuration.addMappers(cleanPackageName); Enumeration<URL> mapperXmls = ExtenderUtil.findFilesFromBundle(bundle, packageName, "*.xml"); if (mapperXmls != null) { while (mapperXmls.hasMoreElements()) { URL mapperxmlUrl = mapperXmls.nextElement(); try { String mappingFile = ExtenderUtil.mappingFileName(mapperxmlUrl.getFile()); logger.info("Adding Mapper XML {} to Config {} ", mappingFile, dsName); InputStream in = mapperxmlUrl.openStream(); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, mappingFile, configuration.getSqlFragments()); xmlMapperBuilder.parse(); in.close(); Collection<String> mappedStatements = configuration.getMappedStatementNames(); for (String mappedStmt : mappedStatements) { logger.info(" Added Mapped Statement: {}", mappedStmt); } unregisterSqlSessionFactory(dsName); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder() .build(configuration); Dictionary<String, String> props = new Hashtable<>(); props.put("dataSourceName", dsName); ServiceRegistration serviceRegistration = bundleContext.registerService( SqlSessionFactory.class.getName(), sqlSessionFactory, props); registeredSqlSessionFactories.put(dsName, serviceRegistration); } catch (IOException e) { logger.error("Unable to add mapper {} ", mapperxmlUrl.toString(), e); } } } } } } logger.info("Registering Mapper Bundle {} ", bundle.getSymbolicName()); activeBundles.add(bundle); }