本文整理匯總了Java中org.apache.ibatis.mapping.Environment類的典型用法代碼示例。如果您正苦於以下問題:Java Environment類的具體用法?Java Environment怎麽用?Java Environment使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Environment類屬於org.apache.ibatis.mapping包,在下文中一共展示了Environment類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: environmentsElement
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
private void environmentsElement(XNode context) throws Exception {
if (context != null) {
if (environment == null) {
environment = context.getStringAttribute("default");
}
for (XNode child : context.getChildren()) {
String id = child.getStringAttribute("id");
if (isSpecifiedEnvironment(id)) {
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
DataSource dataSource = dsFactory.getDataSource();
Environment.Builder environmentBuilder = new Environment.Builder(id)
.transactionFactory(txFactory)
.dataSource(dataSource);
configuration.setEnvironment(environmentBuilder.build());
}
}
}
}
示例2: createSqlSessionManager
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
/**
* This method creates the sqlSessionManager of myBatis. It integrates all the SQL mappers
*
* @return a {@link SqlSessionFactory}
*/
private SqlSessionManager createSqlSessionManager() {
Environment environment = new Environment(DEFAULT, this.transactionFactory,
taskanaEngineConfiguration.getDatasource());
Configuration configuration = new Configuration(environment);
// add mappers
configuration.addMapper(TaskMapper.class);
configuration.addMapper(TaskMonitorMapper.class);
configuration.addMapper(WorkbasketMapper.class);
configuration.addMapper(DistributionTargetMapper.class);
configuration.addMapper(ClassificationMapper.class);
configuration.addMapper(WorkbasketAccessMapper.class);
configuration.addMapper(ObjectReferenceMapper.class);
configuration.addMapper(QueryMapper.class);
configuration.addMapper(AttachmentMapper.class);
configuration.getTypeHandlerRegistry().register(MapTypeHandler.class);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(configuration);
SqlSessionManager sessionManager = SqlSessionManager.newInstance(sessionFactory);
return sessionManager;
}
示例3: getSqlSession
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
/**
* 獲取SqlSession
* @return
* @throws IOException
*/
public SqlSession getSqlSession() throws IOException, ClassNotFoundException {
org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();
config.setCallSettersOnNulls(true); // 設計null調用setter方法
config.setMapUnderscoreToCamelCase(true); // 駝峰命名支持
// 設置mapper
config.addMappers(targetPackage);
// 設置數據源,事務
PooledDataSourceFactory dataSourceFactory = new PooledDataSourceFactory();
dataSourceFactory.setProperties(DBHelper.properties);
DataSource dataSource = dataSourceFactory.getDataSource();
JdbcTransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("test", transactionFactory, dataSource);
config.setEnvironment(environment);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
return sqlSessionFactory.openSession(true);
}
示例4: DBManager
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
public DBManager(AppConfiguration appConfig) {
this.appConfig = appConfig;
boolean isDev = appConfig.getBool(CONFIG_KEY_APP_DEV_MODE, true);
String dbHost = appConfig.get(CONFIG_KEY_MYSQL_HOST, DEFAULT_MYSQL_HOST);
int dbPort = appConfig.getInt(CONFIG_KEY_MYSQL_PORT, DEFAULT_MYSQL_PORT);
String dbName = appConfig.get(CONFIG_KEY_MYSQL_DBNAME, DEFAULT_MEYSQL_DBNAME);
String dbUser = appConfig.get(CONFIG_KEY_MYSQL_USER, "");
String dbPass = appConfig.get(CONFIG_KEY_MYSQL_PASS, "");
String dbDriver = "com.mysql.cj.jdbc.Driver";
String connString = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + "?autoReconnect=true&useSSL=false&useUnicode=true";
DataSource dataSource = new PooledDataSource(dbDriver, connString, dbUser, dbPass);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment(isDev ? "development" : "production", transactionFactory, dataSource);
configuration = new Configuration(environment);
configuration.addMappers("com.elune.dao");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
示例5: init
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@Override
public void init(DbConnection connection) throws Exception {
JdbcConnectionPool pool = JdbcConnectionPool.create(connection.uri, connection.username, connection.password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("jdblender", transactionFactory, pool);
Configuration cfg = new Configuration(environment);
cfg.setMapUnderscoreToCamelCase(true);
cfg.addMapper(BrandsMapper.class);
cfg.addMapper(SeriesMapper.class);
cfg.addMapper(ModelsMapper.class);
cfg.addMapper(SparesMapper.class);
//Create session
sqlSessionFactory = new SqlSessionFactoryBuilder().build(cfg);
}
示例6: environmentsElement
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
private void environmentsElement(XNode context) throws Exception {
if (context != null) {
if (environment == null) {
environment = context.getStringAttribute("default");
}
for (XNode child : context.getChildren()) {
String id = child.getStringAttribute("id");
if (isSpecifiedEnvironment(id)) {
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
DataSource dataSource = dsFactory.getDataSource();
Environment.Builder environmentBuilder = new Environment.Builder(id)
.transactionFactory(txFactory)
.dataSource(dataSource);
configuration.setEnvironment(environmentBuilder.build());
}
}
}
}
示例7: setup
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@BeforeClass
public static void setup() throws Exception {
DataSource dataSource = BaseDataTest.createBlogDataSource();
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL);
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("Production", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.setLazyLoadingEnabled(true);
configuration.setUseActualParamName(false); // to test legacy style reference (#{0} #{1})
configuration.getTypeAliasRegistry().registerAlias(Blog.class);
configuration.getTypeAliasRegistry().registerAlias(Post.class);
configuration.getTypeAliasRegistry().registerAlias(Author.class);
configuration.addMapper(BoundBlogMapper.class);
configuration.addMapper(BoundAuthorMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
示例8: testConfiguration
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@Test
public void testConfiguration() {
UnpooledDataSourceFactory dataSourceFactory = new UnpooledDataSourceFactory();
Properties dataSourceProperties = new Properties();
dataSourceProperties.put("driver", "org.hsqldb.jdbcDriver");
dataSourceProperties.put("url", "jdbc:hsqldb:mem:xml_references");
dataSourceProperties.put("username", "sa");
dataSourceFactory.setProperties(dataSourceProperties);
Environment environment = new Environment("test", new JdbcTransactionFactory(), dataSourceFactory.getDataSource());
Configuration configuration = new Configuration();
configuration.setEnvironment(environment);
configuration.getTypeAliasRegistry().registerAlias(Person.class);
configuration.addMapper(PersonMapper.class);
configuration.addMapper(PersonMapper2.class);
new DefaultSqlSessionFactory(configuration);
}
示例9: testGetMessageForEmptyDatabase
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@Test
public void testGetMessageForEmptyDatabase() throws Exception {
final Environment environment = new Environment("test", new JdbcTransactionFactory(), BaseDataTest.createBlogDataSource());
final Configuration config = new Configuration(environment);
config.addMapper(PostMapper.class);
final SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(config);
final SqlSession session = factory.openSession();
PostMapper mapper = session.getMapper(PostMapper.class);
List<AnnoPost> posts = mapper.getPosts(101);
assertEquals(3,posts.size());
assertEquals(3,posts.get(0).getTags().size());
assertEquals(1,posts.get(1).getTags().size());
assertEquals(0,posts.get(2).getTags().size());
session.close();
}
示例10: setUp
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@BeforeClass
public static void setUp() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:annots", "sa", "");
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/substitution_in_annots/CreateDB.sql");
ScriptRunner runner = new ScriptRunner(c);
runner.setLogWriter(null);
runner.setErrorLogWriter(null);
runner.runScript(reader);
c.commit();
reader.close();
Configuration configuration = new Configuration();
Environment environment = new Environment("test", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:annots", null));
configuration.setEnvironment(environment);
configuration.addMapper(SubstitutionInAnnotsMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
示例11: getSqlSessionFactoryWithConstructor
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
private SqlSessionFactory getSqlSessionFactoryWithConstructor() {
UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory();
Properties properties = new Properties();
properties.setProperty("driver", "org.hsqldb.jdbcDriver");
properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor");
properties.setProperty("username", "sa");
unpooledDataSourceFactory.setProperties(properties);
Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource());
Configuration configuration = new Configuration();
configuration.setEnvironment(environment);
configuration.addMapper(StudentConstructorMapper.class);
configuration.addMapper(TeacherMapper.class);
configuration.getMappedStatementNames();
configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE);
return new DefaultSqlSessionFactory(configuration);
}
示例12: openSessionFromDataSource
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@SuppressWarnings("all")
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
final Environment environment = getConfiguration().getEnvironment();
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
DataSource ds = DataSourceHolder.currentDataSource().getNative();
if (ds == null) {
ds = environment.getDataSource();
}
tx = transactionFactory.newTransaction(ds, level, autoCommit);
final Executor executor = getConfiguration().newExecutor(tx, execType);
return new DefaultSqlSession(getConfiguration(), executor, autoCommit);
} catch (Exception e) {
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}
示例13: openSessionFromDataSource
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
/**
* 源碼解析: 從數據源創建會話
*
* @param execType 執行類型
* @param level 數據庫隔離級別
* @param autoCommit 是否自動提交
* @return
*/
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
// 源碼解析: 獲取環境
final Environment environment = configuration.getEnvironment();
// 源碼解析: 獲取事務工廠
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
// 源碼解析: 創建一個新的事務
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
// 源碼解析: 創建執行器
final Executor executor = configuration.newExecutor(tx, execType);
// 源碼解析: 創建一個新的sql會話
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
// 源碼解析: 關閉事務
closeTransaction(tx); // may have fetched a connection so lets call close()
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
// 源碼解析: 錯誤上下文清空重置
ErrorContext.instance().reset();
}
}
示例14: setup
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@BeforeClass
public static void setup() throws Exception {
DataSource dataSource = BaseDataTest.createBlogDataSource();
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL);
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("Production", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.setLazyLoadingEnabled(true);
configuration.getTypeAliasRegistry().registerAlias(Blog.class);
configuration.getTypeAliasRegistry().registerAlias(Post.class);
configuration.getTypeAliasRegistry().registerAlias(Author.class);
configuration.addMapper(BoundBlogMapper.class);
configuration.addMapper(BoundAuthorMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
示例15: setup
import org.apache.ibatis.mapping.Environment; //導入依賴的package包/類
@BeforeEach
public void setup() throws Exception {
Class.forName(JDBC_DRIVER);
InputStream is = getClass().getResourceAsStream("/examples/joins/CreateJoinDB.sql");
try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
ScriptRunner sr = new ScriptRunner(connection);
sr.setLogWriter(null);
sr.runScript(new InputStreamReader(is));
}
UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
Configuration config = new Configuration(environment);
config.addMapper(JoinMapper.class);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
}