[Spring-F.] 27. MyBatis 3.5, Spring Framework 5.4, Oracle 19g - 방법3(SqlMap-XML-Class)
소스 코드 위주로 소개하도록 하겠다.
[실험 결과]
1. [Spring-Framework] Spring Framework 5.4, MyBatis 3.5 연동 방법 - 실험 결과, 2020-10-02
[이전 게시글]
1. [Spring-F.] 26. MyBatis 3.5, Spring Framework 5.4, Oracle 19g - 방법2(SqlMap-XML-XML 맵핑) // 결과: 지원, 2020-10-02
24. 결과
완성된 실험 결과이다.
그림 21. 결과
그림 22. 프로젝트 구성도
25. web.xml - 기본값(변동 없음)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/appServlet/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
26. view - home.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page session="false" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
<h3>SqlMapSessionFactory 방식(XML - 환경설정 - 클래스-Mappers)</h3>
<c:forEach items="${list }" var="val" >
${val.username} /
${val.password} /
${val.enabled }<br>
</c:forEach>
<c:if test="${empty list }">
${"데이터가 존재하지않아요."}
</c:if>
<h3>XML - web.xml 설정방식</h3>
<c:forEach items="${list2 }" var="val" >
${val.username} /
${val.password} /
${val.enabled }<br>
</c:forEach>
<c:if test="${empty list2 }">
${"데이터가 존재하지않아요."}
</c:if>
</body>
</html>
파일명: home.jsp
[첨부(Attachments)]
27. com.example.spbatis.db - SqlMapSessionFactory.java
package com.example.spbatis.db;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.example.spbatis.mapper.CompUsersMapper;
public class SqlMapSessionFactory {
public SqlSessionFactory ssf;
public SqlMapSessionFactory () {
String resource = "com/example/spbatis/mapper/mybatis-config.xml";
InputStream is = null;
try {
is = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
ssf = new SqlSessionFactoryBuilder().build(is);
ssf.getConfiguration().addMapper(CompUsersMapper.class);
}
public SqlSessionFactory getSqlSessionFactory(){
return ssf;
}
}
파일명: SqlMapSessionFactory.java
[첨부(Attachments)]
28. com.example.spbatis.mapper - 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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 커넥션 풀(MyBatis) -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="UserName"/>
<property name="password" value="Password"/>
<property name="poolMaximumActiveConnections" value="20"/>
<property name="poolMaximumIdleConnections" value="20"/>
<property name="poolMaximumCheckoutTime" value="20000"/>
<property name="poolPingEnabled" value="true"/>
<property name="poolPingQuery" value="select 1"/>
<property name="poolPingConnectionsNotUsedFor" value="10000"/>
<property name="poolTimeToWait" value="15000"/>
</dataSource>
</environment>
</environments>
</configuration>
파일명: mybatis-config.xml
[첨부(Attachments)]
29. com.example.spbatis.mapper - CompUsersMapper.java
package com.example.spbatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.example.spbatis.model.CompUsers;
@Mapper
public interface CompUsersMapper {
@Select("SELECT * FROM comp_users")
List<CompUsers> selectAll();
}
파일명: CompUsersMapper.java
[첨부(Attachments)]
30. com.example.spbatis.dao - CompUsersDao.java
package com.example.spbatis.dao;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.spbatis.db.SqlMapSessionFactory;
import com.example.spbatis.mapper.CompUsersMapper;
import com.example.spbatis.model.CompUsers;
public class CompUsersDao {
private SqlMapSessionFactory ssfc;
private SqlSessionFactory factory;
public CompUsersDao() {
ssfc = new SqlMapSessionFactory();
factory = ssfc.getSqlSessionFactory();
}
// SQL 세션 열기
public List<CompUsers> selectAddress() {
List<CompUsers> user = null;
try (SqlSession session = factory.openSession()) {
CompUsersMapper mapper = session.getMapper(CompUsersMapper.class);
user = mapper.selectAll();
// System.out.println("계정명:" + user.getUsername());
}
return user;
}
}
파일명: CompUsersDao.java
[첨부(Attachments)]
31. com.example.spbatis.service - CompUsersService.java
package com.example.spbatis.service;
import java.util.List;
import com.example.spbatis.dao.CompUsersDao;
import com.example.spbatis.model.CompUsers;
public class CompUsersService {
private CompUsersDao dao = null;
public CompUsersService() {
dao = new CompUsersDao();
}
public List<CompUsers> getAllCompUsers() {
return dao.selectAddress();
}
}
파일명: CompUsersService.java
[첨부(Attachments)]
32. com.example.spbatis - HomeController.java
package com.example.spbatis;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.spbatis.service.CompUsersService;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
private CompUsersService compUserService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home3! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
compUserService = new CompUsersService();
model.addAttribute("serverTime", formattedDate );
model.addAttribute("list", compUserService.getAllCompUsers()) ;
return "home";
}
}
파일명: HomeController.java
[첨부(Attachments)]
* 4부에서 만나요.
1. [Spring-F.] 28. MyBatis 3.5, Spring Framework 5.4, Oracle 19g - 방법4(SqlMap Spring전용) // 결과: 지원, 2020-10-02