当前位置: 首页 > 工具软件 > jfinal-admin > 使用案例 >

jfinal 简单工厂模式构建config

淳于升
2023-12-01

接口

/**
 * @author admin
 */
public interface initConfig {

	/**
	 * 初始化config
	 * @param configName
	 * @param dataSource
	 * @param dialect
	 * @param showSql
	 * @param devMode
	 * @param transactionLevel
	 * @param iContainerFactory
	 * @param ehCache
	 */
	void init(String configName, DataSource dataSource, Dialect dialect, Boolean showSql, Boolean devMode, Integer transactionLevel, IContainerFactory iContainerFactory, EhCache ehCache);
}

实现类

public class ConfigFactory implements initConfig {

	Config config = null;
	Integer defaultLevel = 4;
	IContainerFactory defaultIContainerFactory = IContainerFactory.defaultContainerFactory;
	EhCache defaultEhCache = new EhCache();

	public ConfigFactory(String configName, DataSource dataSource) {
		this.init(configName, dataSource, new MysqlDialect(), true, true, defaultLevel, defaultIContainerFactory, defaultEhCache);
	}

	public ConfigFactory(String configName, DataSource dataSource, Dialect dialect, Boolean showSql, Boolean devMode, Integer transactionLevel, IContainerFactory iContainerFactory, EhCache ehCache) {
		this.init(configName, dataSource, dialect, showSql, devMode, transactionLevel, iContainerFactory, ehCache);
	}

	@Override
	public void init(String configName, DataSource dataSource, Dialect dialect, Boolean showSql, Boolean devMode, Integer transactionLevel, IContainerFactory iContainerFactory, EhCache ehCache) {
		Config config = new Config(configName, dataSource, dialect, showSql, devMode, transactionLevel, iContainerFactory, ehCache);
		if (this.config == null) {
			this.config = config;
		}
	}

	public Config getConfig() {
		this.initTemplate(config);
		return config;
	}

	public Config initTemplate(Config config) {
		config.getSqlKit().addSqlTemplate(WebContant.defaultSqlTemplate);
		config.getSqlKit().addSqlTemplate(WebContant.defaultCodeTemplate);
		config.getSqlKit().parseSqlTemplate();
		return config;
	}
}

使用

			ConfigFactory configFactory = new ConfigFactory(GeneratorKit.dbType, druidPlugin.getDataSource());
			Config config = configFactory.getConfig();
			DbKit.addConfig(config);
			db = Db.use(GeneratorKit.dbType);
 类似资料: