👀 coupon insert 예제;
-> 직전 setting 및 list 예제에서 넘어옴
- coupon_write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>spring-boot로 쿠폰등록 페이지</title>
</head>
<body>
<p>[ 쿠폰 등록페이지 ]</p>
<form id="frm" method="post" action="./coupon_insertok.do">
쿠폰명 : <input type="text" name="cpname"><br>
쿠폰할인율 : <input type="text" name="cprate"><br>
쿠폰사용여부 : <input type="radio" name="cpuse" value="Y" chekced>사용함
<input type="radio" name="cpuse" value="N">사용안함 <br>
쿠폰사용기한 : <input type="date" name="cpdate"><br>
<input type="button" value="쿠폰 등록" onclick="쿠폰등록()">
</form>
</body>
<script>
function 쿠폰등록(){
frm.submit();
}
</script>
</html>
- mapper.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.co.sen.coupon_repository">
<select id="getAllcoupon" resultType="kr.co.sen.coupon_dao">
select * from coupon order by cidx desc
</select> <!-- 이건 아까꺼 - list -->
<insert id="incoupon">
insert into coupon values ('0',#{cpname},#{cprate},#{cpuse},#{cpdate},now());
</insert>
</mapper>
- coupon_repository.java
package kr.co.sen;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper //얘는 mapper.xml이랑 연결
public interface coupon_repository { //Mapper에서 연동되는 id를 로드함
List<coupon_dao> getAllcoupon(); //연결된거임 - 전체 리스트 출력
int incoupon (coupon_dao dao); //메소드명 - 필히 mapper에 작성한 id랑 맞춰야함!
}
⭐ 메소드명 - 필히 mapper에 작성한 id랑 맞춰야함!
- coupon_service.java
package kr.co.sen;
import java.util.List;
//interface - repository + mapper.xml을 로드할 수 있는 interface를 생성
public interface coupon_service {
public List<coupon_dao> getAllCoupon(); //이건 아까꺼 - list
public int insert_service(coupon_dao dao); //여기서 지은 이름 - 어디서 가져온거아님
}
- coupon_service_impl.java
package kr.co.sen;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class coupon_service_impl implements coupon_service{
@Autowired
private coupon_repository coupon_repository;
@Override
public List<coupon_dao> getAllCoupon(){
return coupon_repository.getAllcoupon();
} //이건 아까꺼 - list
@Override
public int insert_service(coupon_dao dao) {
int result = coupon_repository.incoupon(dao);
return result;
}
}
- controller
최종출력
package kr.co.sen;
import java.io.PrintWriter;
import java.util.List;
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.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import jakarta.servlet.http.HttpServletResponse;
@Controller
public class main_controller {
@Autowired
private coupon_service coupon_service; //interface 호출되자마자 @service부분이 발동됨
PrintWriter pw = null;
@PostMapping("/coupon_insertok.do")
public void coupon_insertok(
@ModelAttribute("cp") coupon_dao dao,
HttpServletResponse res
) {
res.setContentType("text/html;charset=utf-8");
try {
this.pw = res.getWriter();
int result = coupon_service.insert_service(dao);
System.out.println(result);
if(result>0) {
this.pw.print("<script>"
+ "alert('정상적으로 등록 완료되었습니다.');"
+ "location.href='./list.do';"
+ "</script>");
}else {
this.pw.print("<script>"
+ "alert('데이터 오류로 인하여 정보가 저장되지 않았습니다.');"
+ "history.go(-1);"
+ "</script>");
}
}catch(Exception e) {
System.out.println(e);
}finally {
this.pw.close();
}
}
//아까꺼
@GetMapping("/list.do")
public String coupon_list(Model m) {
List<coupon_dao> all = coupon_service.getAllCoupon();
int ea = all.size();
m.addAttribute("ea",ea);
m.addAttribute("all",all);
return null;
}
}
🔽
👀 coupon delete 예제;
- mapper.xml
<delete id="delcoupon">
delete from coupon where cidx=#{cidx}
</delete>
- coupon_repository.java
@Mapper //얘는 mapper.xml이랑 연결
public interface coupon_repository { //Mapper에서 연동되는 id를 로드함
int delcoupon (String cidx);
}
- coupon_service.java
//interface - repository + mapper.xml을 로드할 수 있는 interface를 생성
public interface coupon_service {
public int delete_coupon(String cidx);
}
- coupon_service_impl.java
@Service
public class coupon_service_impl implements coupon_service{
@Autowired
private coupon_repository coupon_repository;
@Override
public int delete_coupon(String cidx) {
int result = coupon_repository.delcoupon(cidx);
return result;
}
}
- controller
@Controller
public class main_controller {
@Autowired
private coupon_service coupon_service; //interface 호출되자마자 @service부분이 발동됨
PrintWriter pw = null;
@GetMapping("/coupon_delok.do")
public void coupon_delok(
HttpServletResponse res,
@RequestParam(value="", required = false) String cidx) {
res.setContentType("text/html;charset=utf-8");
try {
this.pw = res.getWriter();
int result = coupon_service.delete_coupon(cidx);
if(result>0) {
this.pw.print("<script>"
+ "alert('정상적으로 삭제 완료되었습니다.');"
+ "location.href='./list.do';"
+ "</script>");
}else {
this.pw.print("<script>"
+ "alert('데이터 오류로 인하여 삭제에 실패했습니다.');"
+ "history.go(-1);"
+ "</script>");
}
}catch(Exception e) {
System.out.println(e);
}finally {
this.pw.close();
}
}
}
👀 정리
[ spring-boot process 순서 정리 ]
- properties 세팅 ( application.properties )
: Server 정보 , Databse 정보 , Mybatis config, mapper - controller 생성
- Repository (interface) 생성
- mapper.xml (namespace 등록 - 3의 interface명)
- Service(interface) : 핸들링을 사용할 메소드를 생성
- ServiceImpl(Class) : Service + Repository
- Controller : @Aturowired Service(interface)에서 return받은 값을 => veiw로 전달
❗ 암호화 method 추가시
interface service 위의 3번 에다가 다 때려박아서 써도됨 (다른 공통으로 묶은 module도 마찬가지)
or
public interface coupon_service entends { }
extends 이용해도 됨!!
'CLASS > SPRINGBOOT' 카테고리의 다른 글
#2-3 / AOP+springboot+로그기록(aop에서 dao 값 가져오기) (0) | 2024.08.13 |
---|---|
#2-2 / AOP-springboot (0) | 2024.08.13 |
#2-1 / AOP-java (0) | 2024.08.13 |
#1-2 / springboot 기본형태+ coupon list (0) | 2024.08.12 |
#1-1 / springboot setting (0) | 2024.08.09 |