List of usage examples for org.apache.ibatis.jdbc ScriptRunner closeConnection
public void closeConnection()
From source file:myguice.SampleBasicTest.java
License:Apache License
@Before public void setupMyBatisGuice() throws Exception { // bindings//from www.ja v a 2 s . co m this.injector = createInjector(new MyBatisModule() { @Override protected void initialize() { install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED); bindDataSourceProviderType(PooledDataSourceProvider.class); bindTransactionFactoryType(JdbcTransactionFactory.class); addMapperClass(UserMapper.class); bindProperties(binder(), createTestProperties()); bind(FooService.class).to(FooServiceMapperImpl.class); bind(UserDao.class).to(UserDaoImpl.class); } }); // prepare the test db Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration() .getEnvironment(); DataSource dataSource = environment.getDataSource(); ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(getResourceAsReader("sample/db/database-schema.sql")); runner.runScript(getResourceAsReader("sample/db/database-test-data.sql")); runner.closeConnection(); this.fooService = this.injector.getInstance(FooService.class); }
From source file:org.mybatis.guice.AbstractGuiceTestExtension.java
License:Apache License
public AbstractGuiceTestExtension() throws SQLException { final Contact contact = new Contact(); contact.setFirstName("John"); contact.setLastName("Doe"); contact.setCreated(new CustomType(currentTimeMillis())); contact.setAddress(null);/*www . j a va 2s . co m*/ final Contact contactWithAddress = new Contact(); contactWithAddress.setFirstName("John"); contactWithAddress.setLastName("Doe"); contactWithAddress.setCreated(new CustomType(currentTimeMillis())); final Address address = new Address(); address.setNumber(1234); address.setStreet("Elm street"); contactWithAddress.setAddress(address); final Counter counter = new Counter(); // bindings final List<Module> modules = this.createMyBatisModule(); modules.add(new Module() { @Override public void configure(Binder binder) { bindProperties(binder, createTestProperties()); binder.bind(Contact.class).toInstance(contact); binder.bind(Contact.class).annotatedWith(named("contactWithAddress")) .toInstance(contactWithAddress); binder.bind(Counter.class).toInstance(counter); } }); this.injector = createInjector(modules); // prepare the test db final Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration() .getEnvironment(); final DataSource dataSource = environment.getDataSource(); final ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(new StringReader("DROP TABLE IF EXISTS contact;" + "CREATE TABLE contact (id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1), " + "first_name VARCHAR(20) NOT NULL, " + "last_name VARCHAR(20) NOT NULL, " + "created TIMESTAMP, " + "address VARCHAR(100) DEFAULT NULL) ;")); runner.closeConnection(); }
From source file:org.mybatis.guice.AbstractGuiceTestRunner.java
License:Apache License
public AbstractGuiceTestRunner(Class<?> klass) throws InitializationError { super(klass); try {//from w ww.j a v a2 s . c o m final Contact contact = new Contact(); contact.setFirstName("John"); contact.setLastName("Doe"); contact.setCreated(new CustomType(currentTimeMillis())); contact.setAddress(null); final Contact contactWithAddress = new Contact(); contactWithAddress.setFirstName("John"); contactWithAddress.setLastName("Doe"); contactWithAddress.setCreated(new CustomType(currentTimeMillis())); Address address = new Address(); address.setNumber(1234); address.setStreet("Elm street"); contactWithAddress.setAddress(address); final Counter counter = new Counter(); // bindings List<Module> modules = this.createMyBatisModule(); modules.add(new Module() { public void configure(Binder binder) { bindProperties(binder, createTestProperties()); binder.bind(Contact.class).toInstance(contact); binder.bind(Contact.class).annotatedWith(named("contactWithAddress")) .toInstance(contactWithAddress); binder.bind(Counter.class).toInstance(counter); } }); this.injector = createInjector(modules); // prepare the test db Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration() .getEnvironment(); DataSource dataSource = environment.getDataSource(); ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(new StringReader("DROP TABLE IF EXISTS contact;" + "CREATE TABLE contact (id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1), " + "first_name VARCHAR(20) NOT NULL, " + "last_name VARCHAR(20) NOT NULL, " + "created TIMESTAMP, " + "address VARCHAR(100) DEFAULT NULL) ;")); runner.closeConnection(); } catch (Exception e) { throw new InitializationError(e); } }
From source file:org.mybatis.guice.CleanDatabaseRule.java
License:Apache License
public void evaluate() throws Exception { ScriptRunner runner = new ScriptRunner( sqlSession.getConfiguration().getEnvironment().getDataSource().getConnection()); try {//from w ww .j ava2 s.c o m runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(new StringReader( "DELETE FROM contact; " + "INSERT INTO contact (id, first_name, last_name, created) " + "VALUES (1, '" + contact.getFirstName() + "', '" + contact.getLastName() + "', '" + new Timestamp(contactWithAddress.getCreated().getValue()) + "'); " + "INSERT INTO contact (id, first_name, last_name, created, address) " + "VALUES (2, '" + contactWithAddress.getFirstName() + "', '" + contactWithAddress.getLastName() + "', '" + new Timestamp(contactWithAddress.getCreated().getValue()) + "', '" + addressConverter.convert(contactWithAddress.getAddress()) + "'); ")); contact.setId(1); contactWithAddress.setId(2); } finally { runner.closeConnection(); } }
From source file:org.mybatis.guice.nestedtx.NestedTxTest.java
License:Apache License
@BeforeEach public void setup() throws Exception { injector = Guice.createInjector(new MyBatisModule() { @Override//from www . jav a 2s . c o m protected void initialize() { bindDataSourceProviderType(PooledDataSourceProvider.class); bindTransactionFactoryType(JdbcTransactionFactory.class); install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED); Properties connectionProps = new Properties(); connectionProps.setProperty("mybatis.environment.id", "jdbc"); connectionProps.setProperty("JDBC.username", "sa"); connectionProps.setProperty("JDBC.password", ""); connectionProps.setProperty("JDBC.autoCommit", "false"); Names.bindProperties(binder(), connectionProps); addMapperClass(NestedTxMapper.class); bind(NestedTxService.class); } }); // prepare the test db Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration() .getEnvironment(); DataSource dataSource = environment.getDataSource(); ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(getResourceAsReader("org/mybatis/guice/nestedtx/setupdb.sql")); runner.closeConnection(); service = injector.getInstance(NestedTxService.class); }
From source file:org.mybatis.guice.sample.SampleBasicTest.java
License:Apache License
@BeforeEach public void setupMyBatisGuice() throws Exception { // bindings//from w w w. ja v a 2 s. c o m this.injector = createInjector(new MyBatisModule() { @Override protected void initialize() { install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED); bindDataSourceProviderType(PooledDataSourceProvider.class); bindTransactionFactoryType(JdbcTransactionFactory.class); addMapperClass(UserMapper.class); bindProperties(binder(), createTestProperties()); bind(FooService.class).to(FooServiceMapperImpl.class); } }); // prepare the test db Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration() .getEnvironment(); DataSource dataSource = environment.getDataSource(); ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-schema.sql")); runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-test-data.sql")); runner.closeConnection(); this.fooService = this.injector.getInstance(FooService.class); }
From source file:org.mybatis.guice.sample.SampleSqlSessionTest.java
License:Apache License
@BeforeEach public void setupMyBatisGuice() throws Exception { // bindings//from w w w.j av a 2 s.c o m List<Module> modules = this.createMyBatisModule(); this.injector = Guice.createInjector(modules); // prepare the test db Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration() .getEnvironment(); DataSource dataSource = environment.getDataSource(); ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-schema.sql")); runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-test-data.sql")); runner.closeConnection(); this.fooService = this.injector.getInstance(FooService.class); }
From source file:org.mybatis.guice.sample.SampleTestBase.java
License:Apache License
private void createTestDb(Injector injector) throws IOException, SQLException { // prepare the test db Environment environment = injector.getInstance(SqlSessionFactory.class).getConfiguration().getEnvironment(); DataSource dataSource = environment.getDataSource(); ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true);//from w w w . java2s . c o m runner.setStopOnError(true); runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-schema.sql")); runner.runScript(getResourceAsReader("org/mybatis/guice/sample/db/database-test-data.sql")); runner.closeConnection(); }
From source file:org.pau.assetmanager.test.GenericTest.java
License:Open Source License
private static void createDatabase(Properties properties) { try {/*from w w w . j a v a2 s . c o m*/ Connection connection = getJDBCConnection(properties); ScriptRunner scriptRunner = new ScriptRunner(connection); InputStreamReader inputStreamReader = new InputStreamReader( getInputStreamFromFile("table_creation.sql")); scriptRunner.runScript(inputStreamReader); inputStreamReader.close(); InputStreamReader inputStreamReaderInit = new InputStreamReader(getInputStreamFromFile("init.sql")); scriptRunner.runScript(inputStreamReaderInit); inputStreamReaderInit.close(); scriptRunner.closeConnection(); } catch (IOException e) { throw new RuntimeException("Error initalizing database", e); } }
From source file:sergey.ibudgetapp.dao.impl.TestDataConnection.java
public static void initDatabase() { Connection connection = null; Reader reader = null;// w w w . j a v a 2s .co m try { connection = MyBatisUtil.getConnection(); ScriptRunner scriptRunner = new ScriptRunner(connection); // DatabaseMetaData dbmd = connection.getMetaData(); // ResultSet rs = dbmd.getTables(null, "ROOT", null, null); // if (!rs.next()) { // System.out.println(" "); // } reader = Resources.getResourceAsReader("sql/drop_tables.sql"); scriptRunner.runScript(reader); reader = Resources.getResourceAsReader("sql/create_tables.sql"); scriptRunner.runScript(reader); reader = Resources.getResourceAsReader("sql/sample_data.sql"); scriptRunner.runScript(reader); connection.commit(); reader.close(); scriptRunner.closeConnection(); } catch (Exception ex) { Logger.getLogger(TestDataConnection.class.getName()).log(Level.SEVERE, null, ex); } }