본문 바로가기
SPRING

230223_MyBatis

by 경 훈 2023. 2. 23.

relaxedQueryChars="[]()^|""

마이바티스(Mybatis) : 스프링에서 오라클 데이터베이스를 다루는 기술

1. pom.xml  파일에 필요한 라이브러리 3개 다운 받아설치

2. servlet-context.xml 파일 수정

3. 마이바티스를 사용하려면

  3-1. 인터페이스를 생성하여 추상메소드를 생성

  3-2. sample.xml 파일이 필요

         이 파일이 CRUD 명령어를 기술부분...여기 id가 인터페이스의 함수명

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<img src="/com4_001/resources/apple.jpg" width="300px" height="250px">
	<img src="/com4_001/image/fantasy.jpg" width="300px" height="250px">
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="savea" method="post">
		이름 : <input type="text" name="name"><br>
		국어 : <input type="text" name="kor"><br>
		영어 : <input type="text" name="eng"><br>
		수학 : <input type="text" name="mat"><br>
		<input type="submit" value="전송"><br>
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3 align="center">${message}</h3>
	<hr>
	이름 : ${name}<br>
	국어 : ${kor}<br>
	영어 : ${eng}<br>
	수학 : ${mat}<br>
	총점 : ${tot}<br>
	평균 : <fmt:formatNumber value="${avg}" pattern="#,##0.0"></fmt:formatNumber><br>
	평점 : ${bigo}<br>
</body>
</html>

 

package com.ezen.com4_001;

public class Scorebigo {
	int su,wo,mi,ya,ga;

	public Scorebigo() {
	}
	public Scorebigo(int su, int wo, int mi, int ya, int ga) {
		super();
		this.su = su;
		this.wo = wo;
		this.mi = mi;
		this.ya = ya;
		this.ga = ga;
	}
	public int getSu() {
		return su;
	}
	public void setSu(int su) {
		this.su = su;
	}
	public int getWo() {
		return wo;
	}
	public void setWo(int wo) {
		this.wo = wo;
	}
	public int getMi() {
		return mi;
	}
	public void setMi(int mi) {
		this.mi = mi;
	}
	public int getYa() {
		return ya;
	}
	public void setYa(int ya) {
		this.ya = ya;
	}
	public int getGa() {
		return ga;
	}
	public void setGa(int ga) {
		this.ga = ga;
	}
	public String bigo(double avg) {
		String bi = null;
		if(avg>=su) bi="수";
		else if(avg>=wo) bi="우";
		else if(avg>=mi) bi="미";
		else if(avg>=ya) bi="양";
		else bi="가";
		return bi;
	}
}

 

package com.ezen.com4_001;

public class Score {
	String name;
	int kor,eng,mat;
	Scorebigo sb;
	public Score() {
	}
	public Score(String name, int kor, int eng, int mat) {
		super();
		this.name = name;
		this.kor = kor;
		this.eng = eng;
		this.mat = mat;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getKor() {
		return kor;
	}
	public void setKor(int kor) {
		this.kor = kor;
	}
	public int getEng() {
		return eng;
	}
	public void setEng(int eng) {
		this.eng = eng;
	}
	public int getMat() {
		return mat;
	}
	public void setMat(int mat) {
		this.mat = mat;
	}
	public Scorebigo getSb() {
		return sb;
	}
	public void setSb(Scorebigo sb) {
		this.sb = sb;
	}
	public String koko(double avg) {
		return sb.bigo(avg);
	}
}

 

package com.ezen.com4_001;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value="/")
	public String ko1() {
		return "index"; //이미지를 보이는 파일
	}
	@RequestMapping(value="/in")
	public String ko2() {
		return "input"; //이미지를 보이는 파일
	}
	@RequestMapping(value="/savea",method = RequestMethod.POST)
	public String ko3(HttpServletRequest request,Model mo) {
		String name = request.getParameter("name");
		int kor = Integer.parseInt(request.getParameter("kor"));
		int eng = Integer.parseInt(request.getParameter("eng"));
		int mat = Integer.parseInt(request.getParameter("mat"));
		int tot = kor+eng+mat;
		double avg = (double)tot/3;
		//
		String path = "classpath:ccc.xml";
		AbstractApplicationContext aac = 
				new GenericXmlApplicationContext(path);
		Score sc = aac.getBean("sc1",Score.class);
		String bb = sc.koko(avg);
		
		//
		mo.addAttribute("message", "회원성적현황");
		mo.addAttribute("name", name);
		mo.addAttribute("kor", kor);
		mo.addAttribute("eng", eng);
		mo.addAttribute("mat", mat);
		mo.addAttribute("tot", tot);
		mo.addAttribute("avg", avg);
		mo.addAttribute("bigo", bb);
		return "out"; //이미지를 보이는 파일
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="save2" method="post">
		이름:<input type="text" name="name"><br>
		전화번호:<input type="text" name="phone"><br>
		요금제:<input type="text" name="price"><br>
		<input type="submit" value="전송"><br>
	</form>
</body>
</html>

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	이름 : ${name}<br>
	전화번호 : ${phone}<br>
	요금제 : <fmt:formatNumber value="${price}" pattern="#,##0"></fmt:formatNumber><br>
	비고 : ${bigo}<br>
</body>
</html>

 

package com.ezen.kim4_002;

public class TelDTO {
	String name,phone;
	int price;
	Telbigo telbigo;
	public TelDTO() {
	}
	public TelDTO(String name, String phone, int price, Telbigo telbigo) {
		super();
		this.name = name;
		this.phone = phone;
		this.price = price;
		this.telbigo = telbigo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public Telbigo getTelbigo() {
		return telbigo;
	}
	public void setTelbigo(Telbigo telbigo) {
		this.telbigo = telbigo;
	}
	public String bigo(int price) {
		String bigo = null;
		if(price>=telbigo.bigo1) bigo="최우수";
		else if(price>=telbigo.bigo2) bigo="우수";
		else if(price>=telbigo.bigo3) bigo="보통";
		else if(price>=telbigo.bigo4) bigo="어르신";
		else bigo="노약자";
		return bigo;
	}
}

 

package com.ezen.kim4_002;

public class Telbigo {
	int bigo1,bigo2,bigo3,bigo4,bigo5;
	public Telbigo() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Telbigo(int bigo1, int bigo2, int bigo3, int bigo4, int bigo5) {
		super();
		this.bigo1 = bigo1;
		this.bigo2 = bigo2;
		this.bigo3 = bigo3;
		this.bigo4 = bigo4;
		this.bigo5 = bigo5;
	}
	public int getBigo1() {
		return bigo1;
	}
	public void setBigo1(int bigo1) {
		this.bigo1 = bigo1;
	}
	public int getBigo2() {
		return bigo2;
	}
	public void setBigo2(int bigo2) {
		this.bigo2 = bigo2;
	}
	public int getBigo3() {
		return bigo3;
	}
	public void setBigo3(int bigo3) {
		this.bigo3 = bigo3;
	}
	public int getBigo4() {
		return bigo4;
	}
	public void setBigo4(int bigo4) {
		this.bigo4 = bigo4;
	}
	public int getBigo5() {
		return bigo5;
	}
	public void setBigo5(int bigo5) {
		this.bigo5 = bigo5;
	}
}

 

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bi1" class="com.ezen.kim4_002.Telbigo">
	<constructor-arg value="100000"></constructor-arg>
	<constructor-arg value="70000"></constructor-arg>
	<constructor-arg value="50000"></constructor-arg>
	<constructor-arg value="30000"></constructor-arg>
	<constructor-arg value="0"></constructor-arg>
</bean>
<bean id="dto1" class="com.ezen.kim4_002.TelDTO">
	<property name="name" value="홍길동"></property>
	<property name="phone" value="010-1234-4256"></property>
	<property name="price" value="50000"></property>
	<property name="telbigo"><ref bean="bi1"></ref></property>
</bean>
</beans>

 

package com.ezen.kim4_002;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TelConfig {
	@Bean
	public TelDTO config() {
		Telbigo telbigo = new Telbigo();
		telbigo.setBigo1(100000);
		telbigo.setBigo2(70000);
		telbigo.setBigo3(50000);
		telbigo.setBigo4(30000);
		telbigo.setBigo5(0);
		TelDTO teldto = new TelDTO();
		teldto.setName("홍길동");
		teldto.setPhone("010-1234-4567");
		teldto.setPrice(80000);
		teldto.setTelbigo(telbigo);
		return teldto;
	}
}

 

package com.ezen.kim4_002;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	
	@RequestMapping(value="/in")
	public String ko1() {
		return "input";
	}
	@RequestMapping(value="/save", method = RequestMethod.POST)
	public String ko2(HttpServletRequest request,Model mo) {
		String path="classpath:aaa.xml";
		AbstractApplicationContext aac =
				new GenericXmlApplicationContext(path);
		TelDTO teldto = aac.getBean("dto1",TelDTO.class);
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		int price = Integer.parseInt(request.getParameter("price"));
		String bigo = teldto.bigo(price);
		mo.addAttribute("name", name);
		mo.addAttribute("phone", phone);
		mo.addAttribute("price", price);
		mo.addAttribute("bigo", bigo);
		return "out";
	}
	@RequestMapping(value="/in2")
	public String ko3() {
		return "input2";
	}
	@RequestMapping(value="/save2", method = RequestMethod.POST)
	public String ko4(HttpServletRequest request,Model mo) {
		AnnotationConfigApplicationContext acac =
				new AnnotationConfigApplicationContext(TelConfig.class);
		TelDTO teldto = acac.getBean("config",TelDTO.class);
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		int price = Integer.parseInt(request.getParameter("price"));
		String bigo = teldto.bigo(price);
		mo.addAttribute("name", name);
		mo.addAttribute("phone", phone);
		mo.addAttribute("price", price);
		mo.addAttribute("bigo", bigo);
		return "out2";
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="saved" method="post">
		이름:<input type="text" name="name"><br>
		나이:<input type="text" name="age"><br>
		<input type="submit" value="전송"><br>
	</form>
</body>
</html>

 

package com.ezen.kim4_003;

public interface Service {
	public void inserta(String name,int age); //자료를 저장
}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ezen.kim4_003.Service">
	<insert id="inserta">
		insert into my223 values (#{param1},#{param2})
	</insert>
</mapper>

 

package com.ezen.kim4_003;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {
	@Autowired
	SqlSession sqlSession;
	
	@RequestMapping(value="/")
	public String ko1() {
		return "index";
	}
	@RequestMapping(value="/in")
	public String ko2() {
		return "input";
	}
	// 폼에서 넘겨받은 자료를 디비에 저장
	@RequestMapping(value="/saved",method = RequestMethod.POST)
	public String ko3(HttpServletRequest request) {
		String name = request.getParameter("name");
		int age = Integer.parseInt(request.getParameter("age"));
		Service ss = sqlSession.getMapper(Service.class);
		ss.inserta(name,age);
		return "redirect:in";
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" width="400px">
		<tr>
			<th>이름</th><th>나이</th>
		</tr>
		<c:forEach items="${list}" var="my">
		<tr>
			<td>${my.name}</td><td>${my.age}</td>
		</tr>
		</c:forEach>
	</table>
</body>
</html>

 

package com.ezen.kim4_003;

public class Mydto {
	String name;
	int age;
	public Mydto() {
	}
	public Mydto(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

package com.ezen.kim4_003;

import java.util.ArrayList;

public interface Service {
	public void inserta(String name,int age); //자료를 저장
	public ArrayList<Mydto> out();
}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ezen.kim4_003.Service">
	<insert id="inserta">
		insert into my223 values (#{param1},#{param2})
	</insert>
	<select id="out" resultType="com.ezen.kim4_003.Mydto">
		select * from my223
	</select>
</mapper>

 

package com.ezen.kim4_003;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.SqlSession;
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;

@Controller
public class HomeController {
	@Autowired
	SqlSession sqlSession;
	
	@RequestMapping(value="/")
	public String ko1() {
		return "index";
	}
	@RequestMapping(value="/in")
	public String ko2() {
		return "input";
	}
	// 폼에서 넘겨받은 자료를 디비에 저장
	@RequestMapping(value="/saved",method = RequestMethod.POST)
	public String ko3(HttpServletRequest request) {
		String name = request.getParameter("name");
		int age = Integer.parseInt(request.getParameter("age"));
		Service ss = sqlSession.getMapper(Service.class);
		ss.inserta(name,age);
		return "redirect:in";
	}
	// 디비에 저장된 자료를 View
	@RequestMapping(value="/out")
	public String ko4(Model mo) {
		Service ss = sqlSession.getMapper(Service.class);
		ArrayList<Mydto> list = ss.out();
		mo.addAttribute("list", list);
		return "output";
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" width="400px">
		<tr>
			<th>이름</th><th>나이</th>
		</tr>
		<c:forEach items="${list}" var="my">
		<tr>
			<td>
			<a href="delete?name=${my.name}">
			${my.name}</a>
			</td><td>${my.age}</td>
		</tr>
		</c:forEach>
	</table>
</body>
</html>

 

package com.ezen.kim4_003;

import java.util.ArrayList;

public interface Service {
	public void inserta(String name,int age); //자료를 저장
	public ArrayList<Mydto> out();
	public void delete(String name);
}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "https://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.ezen.kim4_003.Service">
	<insert id="inserta">
		insert into my223 values (#{param1},#{param2})
	</insert>
	<select id="out" resultType="com.ezen.kim4_003.Mydto">
		select * from my223
	</select>
	<delete id="delete">
		delete from my223 where name=#{param1}
	</delete>
</mapper>

 

package com.ezen.kim4_003;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.SqlSession;
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;

@Controller
public class HomeController {
	@Autowired
	SqlSession sqlSession;
	
	@RequestMapping(value="/")
	public String ko1() {
		return "index";
	}
	@RequestMapping(value="/in")
	public String ko2() {
		return "input";
	}
	// 폼에서 넘겨받은 자료를 디비에 저장
	@RequestMapping(value="/saved",method = RequestMethod.POST)
	public String ko3(HttpServletRequest request) {
		String name = request.getParameter("name");
		int age = Integer.parseInt(request.getParameter("age"));
		Service ss = sqlSession.getMapper(Service.class);
		ss.inserta(name,age);
		return "redirect:in";
	}
	// 디비에 저장된 자료를 View
	@RequestMapping(value="/out")
	public String ko4(Model mo) {
		Service ss = sqlSession.getMapper(Service.class);
		ArrayList<Mydto> list = ss.out();
		mo.addAttribute("list", list);
		return "output";
	}
	@RequestMapping(value="/delete")
	public String ko5(HttpServletRequest request) {
		String name = request.getParameter("name");
		Service ss = sqlSession.getMapper(Service.class);
		ss.delete(name);
		return "redirect:out";
	}
}

 

'SPRING' 카테고리의 다른 글

230227_MyBatis  (0) 2023.02.27
230224_MyBatis  (0) 2023.02.24
230222_기본  (0) 2023.02.22
230221_기본  (0) 2023.02.21
230220_기본  (0) 2023.02.20

댓글