MyBatis+Oracle

2018. 7. 2. 14:43SpringFramework

반응형

1. pom.xml

    <!-- MyBatis 3.4.1 : MyBitis 프레임워크-->

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis</artifactId>

            <version>3.4.6</version>

        </dependency>

        <!-- MyBatis-Spring : Spring과 MyBitis를 연결하는 라이브러리 -->

        <dependency>

            <groupId>org.mybatis</groupId>

            <artifactId>mybatis-spring</artifactId>

            <version>1.3.2</version>

        </dependency>


        <!-- transactionManager 관련 -->

        <dependency>

    <groupId>javax</groupId>

    <artifactId>javaee-api</artifactId>

    <version>8.0</version>

</dependency>


<!-- oracle driver, 밑의 repository 연관 -->

<dependency>

<groupId>com.oracle</groupId>

<artifactId>ojdbc6</artifactId>

<version>11.2.0.3</version>

</dependency>


2. DTO class ( com.app.myproject > model > Field.java )

public class Field implements Serializable{

private static final long serialVersionUID = 1L;

private String id;

private String password;

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}


3. DAO / DAOImpl ( com.app.myproject > dao > CrudDAO.java / > dao > impl > CrudDAOImpl.java )

@Repository

public interface CrudDAO {

public void insert(Field f) throws SQLException;

public void update(Field f) throws SQLException;

public void delete(String s) throws SQLException;

public List<Field> select(String s) throws SQLException;

}


@Repository

public class CrudDAOImpl implements CrudDAO{

private static final Logger logger = LoggerFactory.getLogger(CrudDAOImpl.class);

@Autowired

private SqlSession sqlSession;

@Override

public void insert(Field f) throws SQLException {

// TODO Auto-generated method stub

sqlSession.insert("com.app.myproject.dao.CrudDAO.insert", f);

}


@Override

public void update(Field f) throws SQLException {

// TODO Auto-generated method stub

sqlSession.update("com.app.myproject.dao.CrudDAO.update", f);

}


@Override

public void delete(String s) throws SQLException {

// TODO Auto-generated method stub

sqlSession.delete("com.app.myproject.dao.CrudDAO.delete", s);

}


@Override

public List<Field> select(String s) throws SQLException {

// TODO Auto-generated method stub

List<Field> list = sqlSession.selectList("com.app.myproject.dao.CrudDAO.select",s);

if(list.size() > 0) {

for(int i=0; i<list.size(); i++) {

logger.info(list.get(i).getId());

logger.info(list.get(i).getPassword());

}

}

return list;

}

}


4. Service / ServiceImpl ( com.app.myproject > service > CrudService.java / > service > impl > CrudServiceImpl.java )

@Service

public interface CrudService {

public void insert(Field f) throws SQLException;

public void update(Field f) throws SQLException;

public void delete(String s) throws SQLException;

public List<Field> select(String s) throws SQLException;

}


@Component

@Service

public class CrudServiceImpl implements CrudService{

@Autowired

private CrudDAO crudDAO;

@Override

public void insert(Field f) throws SQLException {

// TODO Auto-generated method stub

crudDAO.insert(f);

}


@Override

public void update(Field f) throws SQLException {

// TODO Auto-generated method stub

crudDAO.update(f);

}


@Override

public void delete(String s) throws SQLException {

// TODO Auto-generated method stub

crudDAO.delete(s);

}


@Override

public List<Field> select(String s) throws SQLException {

// TODO Auto-generated method stub

return crudDAO.select(s);

}

}


5. .properties ( com.app.myproject > resources > properties > jdbc.properties )

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:thin:@localhost:1521:xe

jdbc.username=scott

jdbc.password=tiger


6. sqlMapConfig.xml ( com.app.myproject > resources > mybatis > sqlMapConfig.xml )

<?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>

<settings>

  <setting name="cacheEnabled" value="true"/>

  <setting name="lazyLoadingEnabled" value="false"/>

  <setting name="multipleResultSetsEnabled" value="true"/>

  <setting name="useColumnLabel" value="true"/>

  <setting name="useGeneratedKeys" value="false"/>

  <setting name="autoMappingBehavior" value="PARTIAL"/>

  <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>

  <setting name="defaultExecutorType" value="SIMPLE"/>

  <setting name="defaultStatementTimeout" value="25"/>

  <setting name="defaultFetchSize" value="100"/>

  <setting name="safeRowBoundsEnabled" value="false"/>

  <setting name="mapUnderscoreToCamelCase" value="false"/>

  <setting name="localCacheScope" value="SESSION"/>

  <setting name="jdbcTypeForNull" value="OTHER"/>

  <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>

</settings>

    <typeAliases>

        <typeAlias alias="Field" type="com.app.myproject.model.Field"/>

    </typeAliases>

</configuration>


7. query xml ( com.app.myproject > resources > sqlmaps > crud.xml ) 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="com.app.myproject.dao.CrudDAO">

<insert id="insert" parameterType="Field">

INSERT INTO PROJECT(ID, PASSWORD) 

VALUES(#{id}, #{password})

</insert>

<update id="update" parameterType="Field">

<if test="id != null">

UPDATE PROJECT

SET PASSWORD = #{password}

WHERE ID = #{id}

</if>

</update>

<delete id="delete" parameterType="String">

<if test="value != null">

DELETE 

FROM PROJECT

WHERE ID = #{value}

</if>

</delete>

<select id="select" parameterType="String" resultType="Field">

    <if test="value != null">

    SELECT *

    FROM PROJECT 

    WHERE ID = #{value}

    </if>

</select>

</mapper>


8. context-database.xml (WEB-INF/classes/META-INF/context)

<beans:bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">

<beans:property name="transactionManager" ref="transactionManager"/>

</beans:bean>

 

<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>


<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<beans:property name="dataSource" ref="dataSource"/>

</beans:bean>


<!-- dataSource : JDBC 커넥션을 처리하는 기능 -->

<beans:bean id="xadataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<beans:property name="driverClassName" value="${jdbc_mysql.driverClassName}" />

<beans:property name="url"      value="${jdbc_mysql.url}" />

<beans:property name="username" value="${jdbc_mysql.username}" />

<beans:property name="password" value="${jdbc_mysql.password}" />

        </beans:bean>

    

        <!-- SQL 쿼리 로그 관련 설정 -->

    <beans:bean id="dataSource" class="net.sf.log4jdbc.Log4jdbcProxyDataSource">

    <beans:constructor-arg index="0" ref="xadataSource"/>

    <beans:property name="logFormatter">

    <beans:bean class="net.sf.log4jdbc.tools.Log4JdbcCustomFormatter">

    <beans:property name="loggingType" value="MULTI_LINE"/>

    <beans:property name="sqlPrefix" value="SQL:"/>

    </beans:bean>

    </beans:property>

    </beans:bean>

    

    <!-- 마이바티스 설정, sqlSessionFactory : DB와의 연결과 SQL의 실행에 대한 모든 것을 가진 객체 -->

    <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <beans:property name="dataSource" ref="dataSource"/>

        <beans:property name="configLocation"  value="classpath:/mybatis/sqlMapConfig.xml"/>

        <beans:property name="mapperLocations" value="classpath:/sqlmaps/*.xml"/>

    </beans:bean>

    

    <beans:bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">

    <beans:constructor-arg index="0" ref="sqlSessionFactory"/>

    </beans:bean>



반응형