쿼리문
SELECT A.COM_CODE, A.COM_CODE_NM, A.DESCRIPTIONS
,B.COM_DET_CODE, B.COM_DET_CODE_NM
FROM COM_CODE_INFO A INNER JOIN COM_DET_CODE_INFO B
ON( A.COM_CODE = B.COM_CODE)
ORDER BY A.COM_CODE,B.COM_DET_CODE
;
VO 생성
package kr.or.ddit.vo;
import java.util.List;
import lombok.Data;
//공통 코드 정보
@Data
public class ComCodeInfoVO {
private String comCode;
private String comCodeNm;
private String descriptions;
//COM_CODE_INFO : COM_DET_CODE_INFO = 1:N
private List<ComDetCodeInfoVO> comDetCodeInfoVOList;
}
ComDetCodeInfo.java
package kr.or.ddit.vo;
import lombok.Data;
//공통 상세 코드 정보
@Data
public class ComDetCodeInfo {
private String comDetCode;
private String comCode;
private String comDetCodeNm;
private String descriptions;
}
mybatisAlias.xml에서 vo 추가
ComCodeInfoMapper 생성
SQL_xml 생성 & ComCodeInfoMapper 연결
Controller CRUD 메소드 만들기
package kr.or.ddit.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import kr.or.ddit.service.ComCodeInfoService;
import kr.or.ddit.vo.ComCodeInfoVO;
import lombok.extern.slf4j.Slf4j;
import oracle.jdbc.proxy.annotation.Post;
@Slf4j
@RequestMapping("/comCodeInfo")
@Controller
public class ComCodeInfoController {
@Autowired
ComCodeInfoService comCodeInfoService;
//입력 뷰
@GetMapping("/create")
public String create(Model model) {
//공통 코드 불러오기
List<ComCodeInfoVO> comCodeInfoVOList=this.comCodeInfoService.create();
//forwarding
model.addAttribute("comCodeInfoVOList", comCodeInfoVOList);
return "comCodeInfo/create";
}
//입력 실행
@PostMapping("/createPost")
public String createPost(ComCodeInfoVO comCodeInfoVO) {
log.info("createPost->comCodeInfoVO"+comCodeInfoVO);
int result =this.comCodeInfoService.createPost(comCodeInfoVO);
log.info("createPost ->result:"+result);
//입력 후 상세로 이동
return "redirect:/detail?comCode="+comCodeInfoVO.getComCode();
}
// 상세뷰
@GetMapping("/detail")
public String detail(ComCodeInfoVO comCodeInfoVO) {
//forwarding
return "comCodeInfo/detail";
}
//수정 실행
@PostMapping("/updatePost")
public String updatePost(ComCodeInfoVO comCodeInfoVO) {
//수정 후 상세 이동
return "redirect:/detail?comCode="+comCodeInfoVO.getComCode();
}
//삭제 실행
@PostMapping("/deletePost")
public String deletePost() {
//삭제 후 목록 이동
return "redirect:/comCodeInfo/list";
}
@GetMapping("/list")
public String list(Map<String, Object> map, Model model) {
List<ComCodeInfoVO> ComCodeInfoVOList =this.comCodeInfoService.list(map);
log.info("list ->ComCodeInfoVOList" +ComCodeInfoVOList);
model.addAttribute("ComCodeInfoVOList", ComCodeInfoVOList);
return "comCodeInfo/list";
}
}
Service 생성
ComCodeInfoService.java
package kr.or.ddit.service;
import java.util.List;
import java.util.Map;
import kr.or.ddit.vo.ComCodeInfoVO;
public interface ComCodeInfoService {
public List<ComCodeInfoVO> create();
public int createPost(ComCodeInfoVO comCodeInfoVO);
public ComCodeInfoVO detail(ComCodeInfoVO comCodeInfoVO);
public int updatePost(ComCodeInfoVO comCodeInfoVO);
public int deletePost(ComCodeInfoVO comCodeInfoVO);
public List<ComCodeInfoVO> list(Map<String, Object> map);
}
ComCodeInfoServiceImpl.java
package kr.or.ddit.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import kr.or.ddit.mapper.ComCodeInfoMapper;
import kr.or.ddit.service.ComCodeInfoService;
import kr.or.ddit.vo.ComCodeInfoVO;
@Service
public class ComCodeInfoServiceImpl implements ComCodeInfoService {
@Autowired
ComCodeInfoMapper comCodeInfoMapper;
//입력 실행
@Override
public List<ComCodeInfoVO> create(){
return this.comCodeInfoMapper.create();
}
//입력 실행
@Override
public int createPost(ComCodeInfoVO comCodeInfoVO) {
return this.comCodeInfoMapper.createPost(comCodeInfoVO);
}
//상세뷰
@Override
public ComCodeInfoVO detail(ComCodeInfoVO comCodeInfoVO) {
return this.comCodeInfoMapper.detail(comCodeInfoVO);
}
//수정실행
@Override
public int updatePost(ComCodeInfoVO comCodeInfoVO) {
return this.comCodeInfoMapper.updatePost(comCodeInfoVO);
}
//삭제 실행
@Override
public int deletePost(ComCodeInfoVO comCodeInfoVO) {
return this.comCodeInfoMapper.deletePost(comCodeInfoVO);
}
//목록뷰
@Override
public List<ComCodeInfoVO> list(Map<String, Object> map){
return this.comCodeInfoMapper.list(map);
}
}
ComCodeInfoMapper.java
package kr.or.ddit.mapper;
import java.util.List;
import java.util.Map;
import kr.or.ddit.vo.ComCodeInfoVO;
public interface ComCodeInfoMapper {
//입력 뷰
public List<ComCodeInfoVO> create();
public int createPost(ComCodeInfoVO comCodeInfoVO);
public ComCodeInfoVO detail(ComCodeInfoVO comCodeInfoVO);
public int updatePost(ComCodeInfoVO comCodeInfoVO);
public int deletePost(ComCodeInfoVO comCodeInfoVO);
public List<ComCodeInfoVO> list(Map<String, Object> map);
}
comCode_SQL.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="kr.or.ddit.mapper.ComCodeInfoMapper">
<!-- 입력 뷰 -->
<select id="create" resultType="comCodeInfoVO">
SELECT COM_CODE, COM_CODE_NM, DESCRIPTIONS
FROM COM_CODE_INFO
</select>
<!-- 입력 실행 -->
<insert id="createPost" parameterType="comCodeInfoVO">
INSERT INTO COM_CODE_INFO (COM_CODE, COM_CODE_NM
<if test="descriptions!=null and descriptions !=''">
, DESCRIPTIONS
</if>
)
VALUES(#{comCode},#{comCodeNm}
<if test="descriptions!=null and descriptions !=''">
, #{descriptions}
</if>
)
</insert>
<!-- 상세 뷰 -->
<select id="detail" parameterType="comCodeInfoVO" resultType="comCodeInfoVO">
SELECT COM_CODE, COM_CODE_NM, DESCRIPTIONS
FROM COM_CODE_INFO
WHERE COM_CODE= #{comCode}
</select>
<!-- 수정 실행 -->
<update id="updatePost" parameterType="comCodeInfoVO">
UPDATE COM_CODE_INFO
SET
COM_CODE_NM = #{comCodeNm}
<if test="descriptions!=null and descriptions !=''">
, DESCRIPTIONS = #{descriptions}
</if>
WHERE
COM_CODE = #{comCode}
</update>
<delete id="deletePost" parameterType="comCodeInfoVO">
DELETE FROM COM_CODE_INFO
WHERE COM_CODE= #{comCode}
</delete>
<select id="list" parameterType="hashMap" resultType="comCodeInfoVO">
SELECT COM_CODE, COM_CODE_NM, DESCRIPTIONS
FROM COM_CODE_INFO
</select>
</mapper>
create.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<script type="text/javascript" src="/resources/js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#selComCode").on("change",function(){
let comCode=$(this).val();//change 됬을때 value 값 가져옴
console.log("comCode:",comCode);
let data ={
"comCode":comCode
}
$.ajax({
url:"/ComDetCodeInfo/getNextComCode",
contentType:"application/json;charset=utf-8",
data:JSON.stringify(data),
type:"post",
dataType:"text",
beforeSend:function(xhr){
xhr.setRequestHeader("${_csrf.headerName}","${_csrf.token}");
},
success:function(result){
console.log("result:",result);
$("#comDetCode").val(result)
}
})//ajax end
})
});//$function end
</script>
<h3>공통 코드</h3>
<div class="row">
<div class="col-md-6">
<form action="/comCodeInfo/createPost" method="post">
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">공통 코드</h3>
</div>
<div class="card-body">
<div class="form-group">
<label for="comCode">공통 코드</label>
<input type="text" id="comCode" name="comCode" class="form-control"
placeholder="공통 코드" required />
</div>
<div class="form-group">
<label for="comCodeNm">공통 코드 명</label>
<input type="text" id="comCodeNm" name="comCodeNm" class="form-control"
placeholder="공통 코드 명" required />
</div>
<div class="form-group">
<label for="descriptions">설명</label>
<textarea name="descriptions" id="descriptions"
cols="30" rows="5" class="form-control"
placeholder="설명" required></textarea>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">공통 코드 등록</button>
</div>
</div>
<sec:csrfInput/>
</form>
</div>
<div class="col-md-6">
<form action="/ComDetCodeInfo/createPost" method="post">
<div class="card card-info">
<div class="card-header">
<h3 class="card-title">공통 상세 코드</h3>
</div>
<div class="card-body">
<div class="form-group">
<label for="selComCode">공통 코드</label>
<select id="selComCode" name="comCode" class="form-control custom-select">
<option selected disabled>공통 코드 선택</option>
<c:forEach var="comCodeInfoVO" items="${comCodeInfoVOList}" varStatus="stat">
<option value="${comCodeInfoVO.comCode}">${comCodeInfoVO.comCodeNm}</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<label for="comDetCode">공통 상세 코드</label>
<input type="text" id="comDetCode" name="comDetCode" class="form-control"
placeholder="공통 상세 코드" required />
</div>
<div class="form-group">
<label for="comDetCodeNm">공통 상세 코드 명</label>
<input type="text" id="comDetCodeNm" name="comDetCodeNm" class="form-control"
placeholder="공통 상세 코드 명" required />
</div>
<div class="form-group">
<label for="descriptions2">설명</label>
<textarea name="descriptions" id="descriptions2"
cols="30" rows="5" class="form-control"
placeholder="설명" required></textarea>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">공통 상세 코드 등록</button>
</div>
</div>
<sec:csrfInput/>
</form>
</div>
</div>
ComDetCodeInfoController.java
package kr.or.ddit.vo;
import lombok.Data;
@Data
public class ComCodeDetailVO {
private String comCodeDetail;
private String comCodeDetailNm;
private String comCode;
}
ComDetCodeInfoController.java
package kr.or.ddit.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import kr.or.ddit.service.ComCodeInfoService;
import kr.or.ddit.service.ComDetCodeInfoService;
import kr.or.ddit.vo.ComDetCodeInfoVO;
import lombok.extern.slf4j.Slf4j;
import oracle.jdbc.proxy.annotation.Post;
@Slf4j
@RequestMapping("/ComDetCodeInfo")
@Controller
public class ComDetCodeInfoController {
@Autowired
ComDetCodeInfoService comDetCodeInfoService;
//입력 뷰
@GetMapping("/create")
public String create() {
//forwarding
return "comCodeInfo/create";
}
//입력 실행
@PostMapping("/createPost")
public String createPost(ComDetCodeInfoVO comDetCodeInfoVO) {
log.info("createPost->comDetCodeInfoVO"+comDetCodeInfoVO);
int result =this.comDetCodeInfoService.createPost(comDetCodeInfoVO);
log.info("createPost ->result:"+result);
//입력 후 상세로 이동
return "redirect:/comCodeInfo/detail?comCode="+comDetCodeInfoVO.getComCode();
}
// 상세뷰
@GetMapping("/detail")
public String detail(ComDetCodeInfoVO comDetCodeInfoVO) {
//forwarding
return "comCodeInfo/detail";
}
//수정 실행
@PostMapping("/updatePost")
public String updatePost(ComDetCodeInfoVO comDetCodeInfoVO) {
//수정 후 상세 이동
return "redirect:/detail?comCode="+comDetCodeInfoVO.getComCode();
}
//삭제 실행
@PostMapping("/deletePost")
public String deletePost() {
//삭제 후 목록 이동
return "redirect:/comCodeInfo/list";
}
@GetMapping("/list")
public String list(Map<String, Object> map, Model model) {
List<ComDetCodeInfoVO> ComCodeInfoVOList =this.comDetCodeInfoService.list(map);
log.info("list ->ComCodeInfoVOList" +ComCodeInfoVOList);
model.addAttribute("ComCodeInfoVOList", ComCodeInfoVOList);
return "comCodeInfo/list";
}
//요청파라미터:{"comCode":"HBY01"}
//기본키+1
@ResponseBody
@PostMapping("/getNextComCode")
public String getNextComCode(@RequestBody ComDetCodeInfoVO comDetCodeInfoVO) {
log.info("getNextComCode ->comDetCodeInfoVO:"+comDetCodeInfoVO);
String comDetCode =
this.comDetCodeInfoService.getNextComCode(comDetCodeInfoVO);
log.info("comDetCode:"+comDetCode);
return comDetCode;
}
}
ComDetCodeInfoService.java
package kr.or.ddit.service;
import java.util.List;
import java.util.Map;
import kr.or.ddit.vo.ComDetCodeInfoVO;
public interface ComDetCodeInfoService {
public List<ComDetCodeInfoVO> create();
public int createPost(ComDetCodeInfoVO comCodeInfoVO);
public ComDetCodeInfoVO detail(ComDetCodeInfoVO comCodeInfoVO);
public int updatePost(ComDetCodeInfoVO comCodeInfoVO);
public int deletePost(ComDetCodeInfoVO comCodeInfoVO);
public List<ComDetCodeInfoVO> list(Map<String, Object> map);
public String getNextComCode(ComDetCodeInfoVO comDetCodeInfoVO);
}
ComDetCodeInfoServiceImpl.java
package kr.or.ddit.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import kr.or.ddit.mapper.ComCodeInfoMapper;
import kr.or.ddit.mapper.ComDetCodeInfoMapper;
import kr.or.ddit.service.ComCodeInfoService;
import kr.or.ddit.service.ComDetCodeInfoService;
import kr.or.ddit.vo.ComDetCodeInfoVO;
@Service
public class ComDetCodeInfoServiceImpl implements ComDetCodeInfoService {
@Autowired
ComDetCodeInfoMapper comDetCodeInfoMapper;
//입력 실행
@Override
public List<ComDetCodeInfoVO> create(){
return this.comDetCodeInfoMapper.create();
}
//입력 실행
@Override
public int createPost(ComDetCodeInfoVO comCodeInfoVO) {
return this.comDetCodeInfoMapper.createPost(comCodeInfoVO);
}
//상세뷰
@Override
public ComDetCodeInfoVO detail(ComDetCodeInfoVO comCodeInfoVO) {
return this.comDetCodeInfoMapper.detail(comCodeInfoVO);
}
//수정실행
@Override
public int updatePost(ComDetCodeInfoVO comCodeInfoVO) {
return this.comDetCodeInfoMapper.updatePost(comCodeInfoVO);
}
//삭제 실행
@Override
public int deletePost(ComDetCodeInfoVO comCodeInfoVO) {
return this.comDetCodeInfoMapper.deletePost(comCodeInfoVO);
}
//목록뷰
@Override
public List<ComDetCodeInfoVO> list(Map<String, Object> map){
return this.comDetCodeInfoMapper.list(map);
}
@Override
public String getNextComCode(ComDetCodeInfoVO comDetCodeInfoVO) {
return this.comDetCodeInfoMapper.getNextComCode(comDetCodeInfoVO);
}
}
ComDetCodeInfoMapper.java
package kr.or.ddit.mapper;
import java.util.List;
import java.util.Map;
import kr.or.ddit.vo.ComDetCodeInfoVO;
public interface ComDetCodeInfoMapper {
//입력 뷰
public List<ComDetCodeInfoVO> create();
public int createPost(ComDetCodeInfoVO comDetCodeInfoVO);
public ComDetCodeInfoVO detail(ComDetCodeInfoVO comDetCodeInfoVO);
public int updatePost(ComDetCodeInfoVO comDetCodeInfoVO);
public int deletePost(ComDetCodeInfoVO comDetCodeInfoVO);
public List<ComDetCodeInfoVO> list(Map<String, Object> map);
public String getNextComCode(ComDetCodeInfoVO comDetCodeInfoVO);
}
comCodeInfo_SQL.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="kr.or.ddit.mapper.ComDetCodeInfoMapper">
<!-- 입력 뷰 -->
<select id="create" resultType="comDetCodeInfoVO">
SELECT COM_DET_CODE, COM_CODE, COM_DET_CODE_NM, DESCRIPTIONS
FROM COM_DET_CODE_INFO
</select>
<!-- 입력 실행 -->
<insert id="createPost" parameterType="comDetCodeInfoVO">
INSERT INTO COM_DET_CODE_INFO (COM_DET_CODE, COM_CODE, COM_DET_CODE_NM
<if test="descriptions!=null and descriptions !=''">
, DESCRIPTIONS
</if>
)
VALUES(#{comDetCode},#{comCode},#{comDetCodeNm}
<if test="descriptions!=null and descriptions !=''">
, #{descriptions}
</if>
)
</insert>
<!-- 상세 뷰 -->
<select id="detail" parameterType="comDetCodeInfoVO" resultType="comDetCodeInfoVO">
SELECT COM_DET_CODE, COM_CODE, COM_DET_CODE_NM, DESCRIPTIONS
FROM COM_DET_CODE_INFO
WHERE COM_DET_CODE= #{comDetCode}
</select>
<!-- 수정 실행 -->
<update id="updatePost" parameterType="comDetCodeInfoVO">
UPDATE COM_DET_CODE_INFO
SET
COM_DET_CODE_NM = #{comDetCodeNm}
<if test="descriptions!=null and descriptions !=''">
, DESCRIPTIONS = #{descriptions}
</if>
WHERE
COM_DET_CODE = #{comDetCode}
</update>
<delete id="deletePost" parameterType="comDetCodeInfoVO">
DELETE FROM COM_DET_CODE_INFO
WHERE COM_DET_CODE= #{comDetCode}
</delete>
<select id="list" parameterType="hashMap" resultType="comDetCodeInfoVO">
SELECT COM_DET_CODE, COM_CODE, COM_DET_CODE_NM, DESCRIPTIONS
FROM COM_DET_CODE_INFO
</select>
<!--comDetCodeInfoVO{...,comcode=HBY01,..} -->
<select id="getNextComCode" parameterType="comDetCodeInfoVO" resultType="String">
SELECT NVL(SUBSTR(MAX(COM_DET_CODE),1,5)
|| TRIM(TO_CHAR(SUBSTR(MAX(COM_DET_CODE),6)+1,'000')),#{comCode}||'001')
FROM COM_DET_CODE_INFO
WHERE COM_CODE=#{comCode}
</select>
</mapper>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h3>공통 코드</h3>
<div class="row">
<div class="col-md-6">
<form action="/comCodeInfo/createPost" method="post">
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">공통 코드</h3>
</div>
<div class="card-body">
<!-- //공통 코드 목록 시작 -->
<table class="table table-bordered">
<thead>
<tr>
<th>공통 코드</th>
<th>공통 코드 명</th>
</tr>
</thead>
<tbody>
<c:forEach var="ComCodeInfoVO" items="${ComCodeInfoVOList}"
varStatus="stat">
<tr>
<td>${ComCodeInfoVO.comCode}</td>
<td>${ComCodeInfoVO.comCodeNm}</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- //공통 코드 목록 끝 -->
</div>
<div class="card-footer">
<!-- 페이징 처리 -->
</div>
</div>
<sec:csrfInput/>
</form>
</div>
<div class="col-md-6">
<div class="card card-info">
<div class="card-header">
<h3 class="card-title">공통 상세 코드</h3>
</div>
<div class="card-body">
<!-- //공통 상세 코드 목록 시작 -->
<table class="table table-bordered">
<thead>
<tr>
<th>공통 상세 코드</th>
<th>공통 상세 코드 명</th>
<th>공통 코드 </th>
</tr>
</thead>
<tbody>
<c:forEach var="ComDetCodeInfoVO" items="${ComDetCodeInfoVOList}"
varStatus="stat">
<tr>
<td>${ComDetCodeInfoVO.comDetCode}</td>
<td>${ComDetCodeInfoVO.comDetCodeNm}</td>
<td>${ComDetCodeInfoVO.comCode}</td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- //공통 코드 목록 끝 -->
<div class="card-footer">
</div>
</div>
</div>
</div>
'JAVA > spring' 카테고리의 다른 글
최종프로젝트 환경설정 (0) | 2024.06.06 |
---|---|
security(임시) (0) | 2024.05.20 |
SQL.xml 에서 join쿼리 사용하는 경우 (resultMap) (0) | 2024.05.15 |
페이징 처리 (0) | 2024.05.14 |
파일업로드 (0) | 2024.05.11 |