BAEL-788 Added java config (#1660)

* BAEL-788 Added java config

* BAEL-788 Removed XML config
This commit is contained in:
baljeet20 2017-04-16 21:57:01 +05:30 committed by maibin
parent c313256e8e
commit 76409e6d53
3 changed files with 25 additions and 31 deletions

View File

@ -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;
}

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:testdb1;create=true"/>
<property name="username" value="sa"/>
<property name="password" value="pass123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.baeldung.mybatis.mapper.PersonMapper"/>
<mapper class="com.baeldung.mybatis.mapper.AddressMapper"/>
</mappers>
</configuration>

View File

@ -23,7 +23,7 @@ public class PersonMapperTest {
@Before
public void setup() throws SQLException {
session = MyBatisUtil.getSqlSessionFactory().openSession();
session = MyBatisUtil.buildqlSessionFactory().openSession();
createTables(session);
}