- 黑桃云
-
参考配置方法如下:
1、修改SqlSessionFactoryBean,或者继承该类, 增加configLocations, 移除 configLocation
private Resource[] configLocations;
/*修改该方法*/
public void setConfigLocation(Resource configLocation){
this.configLocations = configLocation != null ? new Resource[] { configLocation } : null;
}
/*增加该方法*/
public void setConfigLocations(Resource[] configLocations) {
this.configLocations = configLocations;
}
/**
* 合并mybatis配置文件
*/
public Document SQLConfigMap()
{
Document doc = DocumentHelper.createDocument();
doc.setXMLEncoding("UTF-8");
DocumentFactory documentFactory = new DocumentFactory();
DocumentType docType = documentFactory.createDocType("configuration",
"-//mybatis.org//DTD Config 3.0//EN", "http://mybatis.org/dtd/mybatis-3-config.dtd");
doc.setDocType(docType);
Element rootElement = doc.addElement("configuration");
rootElement.addElement("typeAliases");
rootElement.addElement("mappers");
return doc;
}
public void readXML(Resource configXML, final Element elementTypeAlias,
final Element elementMapper)
{
// Document document = null;
SAXReader saxReader = new SAXReader();
// Element root = doc.getRootElement();
/*typeAliases合并*/
saxReader.addHandler("/configuration/typeAliases/typeAlias", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementTypeAlias.addElement("typeAlias");
els.addAttribute("alias", row.attributeValue("alias")).addAttribute("type",
row.attributeValue("type"));
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
/*mapper合并*/
saxReader.addHandler("/configuration/mappers/mapper", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementMapper.addElement("mapper");
String mapper = row.attributeValue("mapper");
String resource = row.attributeValue("resource");
els.addAttribute("mapper", mapper);
els.addAttribute("resource", resource);
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
try
{
saxReader.read(configXML.getInputStream());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//return doc;
}
/**
* Build a {@code SqlSessionFactory} instance.
*
* The default implementation uses the standard MyBatis {@code XMLConfigBuilder}
* API to build a
* {@code SqlSessionFactory} instance based on an Reader.
*
* @return SqlSessionFactory
* @throws IOException if loading the config file failed
*/
protected SqlSessionFactory buildSqlSessionFactory() throws IOException
{
Configuration configuration = null;
XMLConfigBuilder xmlConfigBuilder = null;
Document document = this.SQLConfigMap();
Element root = document.getRootElement();
Element elementMapper = root.element("mappers");
Element elementTypeAlias = root.element("typeAliases");
for (Resource configLocation : configLocations)
{
readXML(configLocation, elementTypeAlias, elementMapper);
}
// Reader reader = null; InputStream inputStream = null;
if (this.configLocations != null)
{
logger.debug(document.asXML());
InputStream inputSteam = new ByteArrayInputStream(document.asXML().getBytes());
xmlConfigBuilder = new XMLConfigBuilder(inputSteam, null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();
if (inputSteam != null)
{
inputSteam.close();
inputSteam = null;
}
document = null;
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property "configLocation" not specified,
using default MyBatis Configuration");
}
configuration = new Configuration();
configuration.setVariables(this.configurationProperties);
}
if (hasLength(this.typeAliasesPackage))
{
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray)
{
configuration.getTypeAliasRegistry().registerAliases(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: "" + packageToScan + "" for aliases");
}
}
}
if (!isEmpty(this.typeAliases))
{
for (Class<?> typeAlias : this.typeAliases)
{
configuration.getTypeAliasRegistry().registerAlias(typeAlias);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type alias: "" + typeAlias + """);
}
}
}
if (!isEmpty(this.plugins))
{
for (Interceptor plugin : this.plugins)
{
configuration.addInterceptor(plugin);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered plugin: "" + plugin + """);
}
}
}
if (hasLength(this.typeHandlersPackage))
{
String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeHandlersPackageArray)
{
configuration.getTypeHandlerRegistry().register(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: "" + packageToScan + "" for type handlers");
}
}
}
if (!isEmpty(this.typeHandlers))
{
for (TypeHandler<?> typeHandler : this.typeHandlers)
{
configuration.getTypeHandlerRegistry().register(typeHandler);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type handler: "" + typeHandler + """);
}
}
}
if (xmlConfigBuilder != null)
{
try
{
xmlConfigBuilder.parse();
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed configuration file: "" + this.configLocations + """);
}
}
catch (Exception ex)
{
throw new NestedIOException("Failed to parse config resource: " +
this.configLocations, ex);
}
finally
{
ErrorContext.instance().reset();
}
}
if (this.transactionFactory == null)
{
this.transactionFactory = new SpringManagedTransactionFactory();
}
Environment environment = new Environment(this.environment, this.transactionFactory,
this.dataSource);
configuration.setEnvironment(environment);
if (this.databaseIdProvider != null)
{
try
{
configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
}
catch (SQLException e)
{
throw new NestedIOException("Failed getting a databaseId", e);
}
}
if (!isEmpty(this.mapperLocations))
{
for (Resource mapperLocation : this.mapperLocations)
{
if (mapperLocation == null)
{
continue;
}
try
{
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
configuration, mapperLocation.toString(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
}
catch (Exception e)
{
throw new NestedIOException("Failed to parse mapping resource: "" +
mapperLocation + """, e);
}
finally
{
ErrorContext.instance().reset();
}
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed mapper file: "" + mapperLocation + """);
}
}
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property "mapperLocations" was not specified or
no matching resources found");
}
}
return this.sqlSessionFactoryBuilder.build(configuration);
}
- gitcloud
-
mybatis数据库连接池的配置maxActive配大一些,initialSize也稍加一些。
tomcat200个是没问题的。
表设计的好点,一切都ok。