nexacro-mybatis 조회

This commit is contained in:
hiphop5782
2020-12-04 12:35:40 +09:00
parent c6c6aad56c
commit 90b9bf7875
9 changed files with 197 additions and 12 deletions

27
nexacro-spring/emp.sql Normal file
View File

@@ -0,0 +1,27 @@
-- EMP TABLE
CREATE TABLE EMP(
EMP_ID NUMBER PRIMARY KEY,
EMP_NAME VARCHAR2(21) NOT NULL,
SALARY NUMBER NOT NULL CHECK(SALARY >= 0),
HIRE_DATE DATE DEFAULT SYSDATE NOT NULL,
PHONE_NUMBER VARCHAR2(11) NOT NULL,
EMAIL VARCHAR2(50),
POSITION VARCHAR2(30) NOT NULL,
DEPT VARCHAR2(30) NOT NULL
);
-- EMP SEQUENCE
CREATE SEQUENCE EMP_SEQ NOCACHE;
-- INSERT SAMPLE DATA
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트1', 1000000, TO_DATE('2020-01-01', 'YYYY-MM-DD'),'01011112222','test1@test.com','인턴','개발');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트2', 1100000, TO_DATE('2020-02-02', 'YYYY-MM-DD'),'01022223333','test2@test.com','사원','개발');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트3', 1200000, TO_DATE('2020-03-03', 'YYYY-MM-DD'),'01033334444','test3@test.com','사원','기획');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트4', 1300000, TO_DATE('2020-04-04', 'YYYY-MM-DD'),'01044445555','test4@test.com','대리','개발');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트5', 1400000, TO_DATE('2020-05-05', 'YYYY-MM-DD'),'01055556666','test5@test.com','대리','영업');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트6', 1500000, TO_DATE('2020-06-06', 'YYYY-MM-DD'),'01066667777','test6@test.com','과장','개발');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트7', 1600000, TO_DATE('2020-07-07', 'YYYY-MM-DD'),'01077778888','test7@test.com','차장','기획');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트8', 1700000, TO_DATE('2020-08-08', 'YYYY-MM-DD'),'01088889999','test8@test.com','부장','기획');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트9', 1800000, TO_DATE('2020-09-09', 'YYYY-MM-DD'),'01099990000','test9@test.com','이사','영업');
INSERT INTO EMP VALUES(EMP_SEQ.NEXTVAL, '테스트10', 2500000, TO_DATE('2020-10-10', 'YYYY-MM-DD'),'01000001111','test10@test.com','사장','경영');
COMMIT;

View File

@@ -1,9 +1,11 @@
package com.hacademy.nexacrospring.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.hacademy.nexacrospring.repository.EmpRepository;
import com.nexacro.uiadapter17.spring.core.data.NexacroResult;
/**
@@ -14,9 +16,14 @@ import com.nexacro.uiadapter17.spring.core.data.NexacroResult;
@RequestMapping("/nexacro")
public class NexacroController {
@Autowired
private EmpRepository empRepository;
/**
* 테스트 메소드
* @return NexacroResult 객체
테스트 메소드
- 브라우저에서 /컨텍스트/nexacro/ 로 접속하면 xml 형태의 PlatformData 확인 가능
- PlatformData에 message=Welcome! 형태의 데이터를 추가하여 반환
@return NexacroResult 객체
*/
@GetMapping("/")
public NexacroResult welcome() {
@@ -24,4 +31,12 @@ public class NexacroController {
result.addVariable("message", "Welcome!");
return result;
}
@GetMapping("/list")
public NexacroResult list() {
NexacroResult result = new NexacroResult();
result.addDataSet("list", empRepository.list());
return result;
}
}

View File

@@ -0,0 +1,20 @@
package com.hacademy.nexacrospring.entity;
import java.sql.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data @NoArgsConstructor @AllArgsConstructor @Builder
public class Emp {
private int empNo;
private String empName;
private int salary;
private Date hireDate;
private String phoneNumber;
private String email;
private String position;
private String dept;
}

View File

@@ -0,0 +1,9 @@
package com.hacademy.nexacrospring.repository;
import java.util.List;
import com.hacademy.nexacrospring.entity.Emp;
public interface EmpRepository {
List<Emp> list();
}

View File

@@ -0,0 +1,21 @@
package com.hacademy.nexacrospring.repository;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.hacademy.nexacrospring.entity.Emp;
@Repository
public class EmpRepositoryImpl implements EmpRepository{
@Autowired
private SqlSession sqlSession;
@Override
public List<Emp> list() {
return sqlSession.selectList("emp.list");
}
}

View File

@@ -0,0 +1,16 @@
<?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="emp">
<select id="list" resultType="Emp">
SELECT
EMP_ID, EMP_NAME, SALARY, HIRE_DATE,
PHONE_NUMBER, EMAIL, POSITION, DEPT
FROM EMP
</select>
</mapper>

View File

@@ -0,0 +1,18 @@
<?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>
<!-- camel-case를 kebab-case로 mapping -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.hacademy.nexacrospring.entity"/>
</typeAliases>
</configuration>

View File

@@ -7,21 +7,18 @@
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- nexacro-servlet-context.xml에서 mvc:annotation-driven 설정을 하기 때문에 제거 -->
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.hacademy.nexacrospring" />
<context:component-scan base-package="com.hacademy.nexacrospring" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

View File

@@ -1,8 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!--
데이터베이스 연결만 처리하는 도구 : 추가적으로 정보제공이 필요하다
- constructor-args : 생성자를 통한 정보 전달(필수)
- property : setter 메소드를 통한 정보 전달(선택)
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="nexacro"></property>
<property name="password" value="nexacro"></property>
</bean>
<!--
DBCP 방식의 연결을 수행하는 도구
- 기존 연결도구 정보 + 관리정보
- maxTotal : 관리할 총 연결의 개수
- maxIdle : 여유분으로 연결해둘 연결의 최대 개수
- minIdle : 여유분으로 연결해둘 연결의 최소 개수
- maxWaitMillis : 연결이 모자를 경우 최대 대기시간
-->
<bean id="dbcpSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
<property name="username" value="nexacro"></property>
<property name="password" value="nexacro"></property>
</beans>
<property name="maxTotal" value="20"></property>
<property name="maxIdle" value="10"></property>
<property name="maxWaitMillis" value="3000"></property>
</bean>
<!--
mybatis를 위해 필요한 도구를 등록
- SqlSessionFactory : 초기 설정 및 관리
- dataSource : 연결에 사용할 도구 정보
- configLocation : 설정파일의 위치 정보(classpath:/는 소스폴더(src)의 최상위 경로)
- mapperLocations : 매퍼 파일의 위치 정보들(패턴을 지정)
- SqlSession : 명령 실행
- factory의 정보를 반드시 알아야 함(설정,매퍼 정보등을 파악해야함)
- Spring에서 사용할 도구
-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dbcpSource"></property>
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
<property name="mapperLocations" value="classpath:/mybatis/mapper/*-mapper.xml"></property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="factory"></constructor-arg>
</bean>
<!--
controller를 제외한 스캔
-->
<context:component-scan base-package="com.hacademy.nexacrospring">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>