[SPRING] Mybatis 프레임워크 시작하기 6 [ Mybatis 복습하기]
1. 테이블만들기
2. 프로젝트 만들기
2-1. pom.xml
<!-- ORACLE DRIVER REPOSITORY -->
<repositories>
<repository>
<id>public</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<!-- ojdbc8 설정-->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<!-- Mybatis Spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.4</version>
</dependency>
<!-- Spring JDBC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-JDBC</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- 자료실파일업로드 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- AspectJ :AOP -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
2-2. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.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>
<!-- Enctype -->
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Processes application requests -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/presentation-layer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2-3. presentation-layer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.mybatis.biz" />
<!-- 자료실 사용객체 설정 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
2-4. applicationContext.xml
- database.properties (오라클기준)
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@//localhost:1521/xe
jdbc.username=system
jdbc.password=1234
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<context:component-scan base-package="com.mybatis.biz" />
<!-- DataSource 설정하기 -->
<context:property-placeholder location="classpath:config/database.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Mybatis 설정하기 -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:test-map-Config.xml" />
</bean>
<!-- Mybatis Template 설정하기 -->
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sessionFactory"/>
</bean>
<!-- 트랜잭션 설정하기 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager" >
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.psd.biz..ServiceImpl.*(..) )" />
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
</aop:config>
</beans>
2-5. Java ORM Plug-in 설치
3. MyBatis Generator
<?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>
<typeAliases>
<typeAlias type="com.mybatis.biz.common.MemberVO" alias="MemberVO" />
</typeAliases>
<mappers>
<mapper resource="mappings/MybatisMapper.xml" />
</mappers>
</configuration>
4. VO 만들기
package com.mybatis.biz.common;
public class MemberVO {
private int custno;
private String custname;
private String phone;
private String address;
private String joindate;
private String grade;
private String city;
private String ch1;
private String ch2;
public int getCustno() {
return custno;
}
public void setCustno(int custno) {
this.custno = custno;
}
public String getCustname() {
return custname;
}
public void setCustname(String custname) {
this.custname = custname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getJoindate() {
return joindate;
}
public void setJoindate(String joindate) {
this.joindate = joindate.substring(0, 10);
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCh1() {
return ch1;
}
public void setCh1(String ch1) {
this.ch1 = ch1;
}
public String getCh2() {
return ch2;
}
public void setCh2(String ch2) {
this.ch2 = ch2;
}
@Override
public String toString() {
return "MemberVO [custno=" + custno + ", custname=" + custname + ", phone=" + phone + ", address=" + address
+ ", joindate=" + joindate + ", grade=" + grade + ", city=" + city + ", ch1=" + ch1 + ", ch2=" + ch2
+ "]";
}
}
5. MybatisMapper.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="MybatisMapper">
<insert id="insertMember">
INSERT INTO MEMBER_TBL_02
(CUSTNO, CUSTNAME, PHONE, ADDRESS, JOINDATE, GRADE, CITY)
VALUES
((SELECT MAX(CUSTNO) FROM MEMBER_TBL_02)+1, #{custname},#{phone},#{address},#{joindate},#{grade},#{city})
</insert>
</mapper>
6. JSP 파일만들기 (form)
<%@ 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>
Hello World
안녕하세요
<br>
<h2>회원가입</h2>
<form action="insert.do" method="get">
<table border=1 width=600>
<tr>
<td align=center>회원성명 </td>
<td> <input type=text name=custname size=10></td>
</tr>
<tr>
<td align=center>회원전화 </td>
<td><input type=text name=phone size=20></td>
</tr>
<tr>
<td align=center>회원주소 </td>
<td><input type=text name=address size=40></td>
</tr>
<tr>
<td align=center>가입일자 </td>
<td><input type=text name=joindate size=10></td>
</tr>
<tr>
<td align=center>고객등급<br>[A:VIP,B:일반,C:직원] </td>
<td><input type=text name=grade size=10></td>
</tr>
<tr>
<td align=center>도시코드 </td>
<td><input type=text name=city size=10> </td>
</tr>
<tr>
<td colspan=2 align=center>
<input type=submit value="등록">  
<input type=button value="조회">
</td>
</tr>
</table>
</form>
</body>
</html>
7. Service, DAO, Controller
- ServiceImpl
package com.mybatis.biz.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mybatis.biz.common.MemberVO;
@Service
public class MemberServiceImpl implements MemberService{
@Autowired
memberDAO dao;
@Override
public void insert(MemberVO vo) {
dao.insert(vo);
}
}
- DAOImpl
package com.mybatis.biz.member;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.mybatis.biz.common.MemberVO;
@Repository
public class MemberDAOImpl implements memberDAO{
@Autowired
SqlSessionTemplate mybatis;
@Override
public void insert(MemberVO vo) {
mybatis.insert("MybatisMapper.insertMember", vo);
}
}
- Controller
package com.mybatis.biz.member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mybatis.biz.common.MemberVO;
@Controller
public class MemberController {
@Autowired
MemberService s;
@RequestMapping(value="insert.do")
public String insert(MemberVO vo) {
s.insert(vo);
return "ok.jsp";
}
}
8. 결과확인
Author And Source
이 문제에 관하여([SPRING] Mybatis 프레임워크 시작하기 6 [ Mybatis 복습하기]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@yseonjin/SPRING-Mybatis-프레임워크-시작하기-6-Mybatis-복습하기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)