diff --git a/mybatis/src/main/java/com/baeldung/mybatis/utils/MyBatisUtil.java b/mybatis/src/main/java/com/baeldung/mybatis/utils/MyBatisUtil.java index a045e70333..cd5291f2d2 100644 --- a/mybatis/src/main/java/com/baeldung/mybatis/utils/MyBatisUtil.java +++ b/mybatis/src/main/java/com/baeldung/mybatis/utils/MyBatisUtil.java @@ -1,24 +1,39 @@ package com.baeldung.mybatis.utils; + +import com.baeldung.mybatis.mapper.AddressMapper; +import com.baeldung.mybatis.mapper.PersonMapper; +import org.apache.ibatis.datasource.pooled.PooledDataSource; import org.apache.ibatis.io.Resources; import org.apache.ibatis.jdbc.SQL; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; +import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; public class MyBatisUtil { + public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; + public static final String URL = "jdbc:derby:testdb1;create=true"; + public static final String USERNAME = "sa"; + public static final String PASSWORD = "pass123"; private static SqlSessionFactory sqlSessionFactory; - static { - String resource = "mybatis-config.xml"; - InputStream inputStream; - try { - inputStream = Resources.getResourceAsStream(resource); - sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); - } catch (IOException e) { - e.printStackTrace(); - } + + public static SqlSessionFactory buildqlSessionFactory(){ + DataSource dataSource=new PooledDataSource(DRIVER, URL, USERNAME, PASSWORD); + Environment environment=new Environment("Development",new JdbcTransactionFactory(),dataSource); + Configuration configuration = new Configuration(environment); + configuration.addMapper(PersonMapper.class); + configuration.addMapper(AddressMapper.class); + SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); + SqlSessionFactory factory = builder.build(configuration); + return factory; + } + public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; } diff --git a/mybatis/src/main/resources/mybatis-config.xml b/mybatis/src/main/resources/mybatis-config.xml deleted file mode 100644 index 6987797068..0000000000 --- a/mybatis/src/main/resources/mybatis-config.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/mybatis/src/test/java/com/baeldung/mybatis/mapper/PersonMapperTest.java b/mybatis/src/test/java/com/baeldung/mybatis/mapper/PersonMapperTest.java index 8724aaa545..51888043e9 100644 --- a/mybatis/src/test/java/com/baeldung/mybatis/mapper/PersonMapperTest.java +++ b/mybatis/src/test/java/com/baeldung/mybatis/mapper/PersonMapperTest.java @@ -23,7 +23,7 @@ public class PersonMapperTest { @Before public void setup() throws SQLException { - session = MyBatisUtil.getSqlSessionFactory().openSession(); + session = MyBatisUtil.buildqlSessionFactory().openSession(); createTables(session); }