<Spring> 13. Mybatis-Spring
by BFine반응형
1. Mybatis-Spring Connection
Spring-jdbc[오류날 경우 ${org.springframework-version} 으로 변경], Mybatis, Mybatis-spring ( Dependency 추가 )
root-context.xml에 sqlsession bean을 만들어 DB connection을 처리한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!-- 1. DataSource bean 객체설정 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.mariadb.jdbc.Driver"></property> <property name="url" value="jdbc:mariadb://Localhost:3306/DB이름"></property> <property name="username" value="root"></property> <property name="password" value="hr"></property> </bean> <!-- Oracle DB <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:@Localhost:1521:xe"></property> <property name="username" value="hr"></property> <property name="password" value="hr"></property> </bean> --> <!-- SqlSessionFactory builder는 스프링이 처리 한다. 위의 dataSource 연결 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:/config파일 경로"></property> <property name="mapperLocations" value="classpath:/mapper파일경로" ></property> </bean> <!-- SqlSessionTemplate 생성자를 통해서만 적용 위의 Factory 연결--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactory"></constructor-arg> </bean> | cs |
Spring에서는 Controller에서 DAO 호출 [ DAO <- Service class <- Service interface <- xml 으로 구현 ]
root.xml에서 DB connection을 처리하기 때문에 config.xml에서 처리할 필요가 없다. ( db.properties&mapper not use)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.mariadb.jdbc.Driver"></property> <property name="url" value="jdbc:mariadb://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="hr"></property> </bean> <!-- Oracle DB설정 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/> <property name="username" value="hr"/> <property name="password" value="hr" /> </bean> --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:/com/java/spq/mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:/com/java/spq/Mapper.xml"></property> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg ref="sqlSessionFactoryBean"></constructor-arg> </bean> | cs |
연결 확인 : 생성한 bean 객체를 어노테이션을 이용하여 DI한다. 이때 DAO 객체는 하나만 사용한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Controller public class HomeController { @Autowired DAO dao; @RequestMapping(value = "/", method = RequestMethod.GET) public String home(Locale locale, Model model) { .... dao.check(); .... } } ============================================================= @Component public class DAO { @Autowired SqlSession session; public void check() { System.out.println(session);} | cs |
@Component 이외의 객체를 직접생성시 적용되지 않는다. (bean 객체는 하나에 대해서 매핑)
실행
2. SelectList
Controller에서 Service 객체를 읽어 실행시키는 로직으로 이용한다
Service(Interface) --implements--> Service(Class)[DAO호출] -----------> Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ================interface===================== public interface ServiceInterface { public List<InfoVO> selectList(); } ================class======================== @Service public class ServiceClass implements ServiceInterface { @Autowired DAO dao; @Override public List<InfoVO> selectList() { return dao.selectList(); } } =================dao======================== @Component public class DAO { @Autowired SqlSession session; public List<InfoVO> selectList() { return session.selectList("test.selectList"); } } | cs |
1 2 3 4 5 6 | ====================XML===================================== <mapper namespace="test"> <select id="selectList" resultType="com.java.spq.InfoVO"> select * from Info; </select> </mapper> | cs |
실행
반응형
'공부(2018~2019) - 스킨변경전 > Spring' 카테고리의 다른 글
<Spring> 12. Mybatis (0) | 2018.05.17 |
---|---|
<Spring> 11. File Upload&Download (0) | 2018.05.15 |
<Spring> 10. internationalization, Spring JSON (1) | 2018.05.15 |
<Spring> 9. MVC pattern (0) | 2018.05.09 |
<Spring> 8. AOP (0) | 2018.05.04 |
블로그의 정보
57개월 BackEnd
BFine