본문 바로가기

Archived(Programming)/Spring #1(기초)

Spring_Mybatis 연동

구조
root-context.xml 설정

root-context.xml 파일 소스 옮기기

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<!-- Root Context: defines shared resources visible to all other web components -->

	<!-- 1. DataSource 설정추가하고 JUnit 연결 테스트하기 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"></property>
		<property name="username" value="hr"></property>
		<property name="password" value="hr"></property>
	</bean>

	<!-- 2. MyBatis 연동을 위한 추가 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 3. resource에 mybatis-config.xml 만들고 추가하기 -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- 4. resource에 mappers 디렉토리를 만들고 추가하기 mybatis dqm -->
		<property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml" />
	</bean>

	<!-- 6. @repository 에서 의존성 주입을 위한 설정 -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"
		destroy-method="clearCache">
		<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
	</bean>
	
	
	
</beans>


	

mybatis 설정 파일 옮겨담기

mybatis-config.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>
	
</configuration>

/mappers/testMapper.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.sinc.intern.test">
		
</mapper>  
package com.sinc.intern.user.model.sql;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

@Repository("userD")
public class UserDaoImpl implements UserDao {
	
	@Resource(name="sqlSession")
	private SqlSession session ;
	

	@Override
	public Object loginRow(Object obj) {
		System.out.println( "user dao loginRow : " + obj );
		System.out.println( "user dao conn : " + session );
		return null;
	}
	
}

다음과 같은 설정 후 다시 로그인하면 connection을 가져오는 것을 확인할 수 있음

mybatis-config.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>
	
	<typeAliases>
		<typeAlias type="com.sinc.intern.user.model.vo.UserDTO" alias="userdto"/>	
		<typeAlias type="com.sinc.intern.user.model.vo.UserVO" alias="uservo"/>
	</typeAliases>
	
	
</configuration>

testMapper.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.sinc.intern.test">
	<select id="login" parameterType="userdto" resultType="uservo">
		SELECT 	ID, PWD, NAME, POINT, DEPT
		FROM	INTERN_USER_TBL 
		WHERE	ID = #{id} AND PWD = #{pwd}
	</select>
</mapper>  

 

DB로부터 select 성공

package com.sinc.intern.user.ctrl;
...

@Controller
public class UserCtrl {
	...
	// DTO로 파라미터 받아서 로그인 시도
	// HttpSession으로 받아오기
	@RequestMapping(value="/login.do" , method=RequestMethod.POST)
	public String login(UserDTO dto, HttpSession session) {
		System.out.println("user login dto : " + dto);
		Object user = service.login(dto);
		if(user != null) {
			session.setAttribute("loginUser", user) ;
		}
        	return "redirect:/main.do"; // 수정
	}
}

로그인 후 정상적으로 페이지 로드

'Archived(Programming) > Spring #1(기초)' 카테고리의 다른 글

Spring_data 처리2(글 작성)  (0) 2020.02.06
Spring_data 처리(글 목록)  (0) 2020.02.06
Web_0204  (0) 2020.02.04
Web_Spring  (0) 2020.02.03
Web_JSON  (0) 2020.02.03