本文整理汇总了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);
}