겉바속촉

[spring] MyBatis 깔고 setting해주기 본문

IT일기(하반기)/SPRING

[spring] MyBatis 깔고 setting해주기

겉바속촉 2020. 6. 29. 15:28
728x90
반응형

 

이번에는 MyBatis를 설치해주고 여러가지 필요한 것들을 setting 해줄게요:)

우선은 프로젝트 생성하고 시작하겠습니다.

 

 

 

 

 

 

 

 

version 바꿔주기

 

 

 

sequence 생성

 

 

 

table 생성

 

 

 

 

 

servlet-context.xml

 

 

 

jsp파일들이랑 class 생성해주기

 

 

 

 

<board.data 패키지 생성> 

BoardDao, BoardDaointer(interface), BoardDto 생성

 

<spring.mvc.board패키지 >

BoardController.java 생성

 

 

 

 

 

 

 

 

 

 

 

 

이제부터 다음 사이트로 접속해서 코드들을 복사해오려고 합니다:)

 

 

https://mvnrepository.com/

 

pom.xml으로 가주세요

 

다음 dependecy 복사해서 가져오세요:)

<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc -->
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc</artifactId>
    <version>14</version>
</dependency>

 

 

 

그리고 다음 oracle을 ojdbc로 수정해주세요:)

 

 

 

dependency는 dependencies 안에다가 넣고

repositories는 dependency 밖에 넣어주세요

 

 

dependecy

	<dependency>
    <groupId>ojdbc</groupId>
    <artifactId>ojdbc</artifactId>
    <version>14</version>
	</dependency>
	

 

 

repository

<repositories>
      <repository>
          <id>mvn</id>
             <name>mvn</name>
             <url>http://www.ebi.ac.uk/intact/maven/nexus/content/repositories/ebi-repo/</url>
      </repository>
    </repositories>        

 

 

MyBatis -> 3.2.7버전

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.7</version>
</dependency>

 

 

mybatis-spring -> 1.1버전

 

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.1.1</version>
</dependency>

 

 

Commons IO -> 2.4버전

 

<!-- commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

 

 

 

commons logging -> 1.1.1버전

 

<!-- commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

 

 

Commons DBCP -> 1.4버전

 

<!-- commons-dbcp -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

 

 

 

Spring JDBC -> 5.1.2버전

 

 

<version>부분은 다음과 같이 ${org.springframework-version}</version>으로 고쳤습니다:)

<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>

 

전체 pom.xml 코드

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>spring.mvc</groupId>
	<artifactId>board</artifactId>
	<name>SpringMvcMybatis</name>
	<packaging>war</packaging>
	<version>1.0.0-BUILD-SNAPSHOT</version>
	<properties>
		<java-version>1.6</java-version>
		<org.springframework-version>5.1.2.RELEASE</org.springframework-version>
		<org.aspectj-version>1.6.10</org.aspectj-version>
		<org.slf4j-version>1.6.6</org.slf4j-version>
	</properties>
	
	<repositories>
      <repository>
          <id>mvn</id>
             <name>mvn</name>
             <url>http://www.ebi.ac.uk/intact/maven/nexus/content/repositories/ebi-repo/</url>
      </repository>
    </repositories>        
     
     
     
	<dependencies>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				 </exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
				
		<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${org.aspectj-version}</version>
		</dependency>	
		
		<!-- Logging -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j-version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j-version}</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.15</version>
			<exclusions>
				<exclusion>
					<groupId>javax.mail</groupId>
					<artifactId>mail</artifactId>
				</exclusion>
				<exclusion>
					<groupId>javax.jms</groupId>
					<artifactId>jms</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jdmk</groupId>
					<artifactId>jmxtools</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.sun.jmx</groupId>
					<artifactId>jmxri</artifactId>
				</exclusion>
			</exclusions>
			<scope>runtime</scope>
		</dependency>

		<!-- @Inject -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>
				
		<!-- Servlet -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	
		<!-- Test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>        
	<!-- Add dependecy -->
	<!-- oracle/ojdbc -->
	<dependency>
    <groupId>ojdbc</groupId>
    <artifactId>ojdbc</artifactId>
    <version>14</version>
	</dependency>
	
	<!-- mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.7</version>
</dependency>
<!-- mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.1.1</version>
</dependency>

<!-- commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<!-- commons-logging -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>
<!-- commons-dbcp -->
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${org.springframework-version}</version>
</dependency>


	</dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

 

 


 

 

 

 

1. src/main/resources에다가 패키지 생성

 

 

 

 

2. xml 파일 두개 생성

 

 

 

 

3. BoardDto.java 작성해주세요

package board.data;

import java.sql.Timestamp;

public class BoardDto {
	
	private String num;
	private String writer;
	private String subject;
	private String content;
	private Timestamp writeday;
	
	
	
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Timestamp getWriteday() {
		return writeday;
	}
	public void setWriteday(Timestamp writeday) {
		this.writeday = writeday;
	}
	

	

}

 

4-1. BoardSql.xml에 다음 코드 추가

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

 

4-2. SqlMapConfig.xml에 다음 코드 추가

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTDConfig 3.0//EN" "HTTP://mybatis.org/dtd/mybatis-3-config.dtd">

 

 

<BoardSql.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="board">
	<select id="selectCountOfBoard" resultType="int">
		select count(*) from board
	</select>
</mapper>

 

 

<SqlMapConfig.xml>

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTDConfig 3.0//EN" "HTTP://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<typeAliases>
		<typeAlias type="board.data.BoardDto" alias="bdto"/>	
	</typeAliases>
	
	<mappers>
		<mapper resource="mybatis/setting/BoardSql.xml"	/>
	</mappers>
</configuration>

 

 

 

 

5. board 폴더 생성 후 list, writeForm 2개의 jsp파일 생성

 

 

 

 

6. BoardDao.java 작성

-> 다음 표시한 곳은 BoardSql에서 작성한 id값과 동일함 주의

 

 

7. WEB-INF에서 File 생성

 

 

 

 

8. root-context.xml에 다음 코드 추가

 <!-- dataSource -->
 <bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource">
  <property name="url" value="${url}"/>
  <property name="driverClassName" value="${driver}"/>
  <property name="username" value="${username}"/>
  <property name="password" value="${password}"/>
 </bean> 
 
 <bean id="sqlSessionFactory"
 class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="configLocation">
   <value>classpath:mybatis/setting/SqlMapConfig.xml</value>
  </property>
 </bean>
 
 <bean id="sqlSessionTemplate"
   class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg ref="sqlSessionFactory"/> 
 </bean>  

 

전체 root-context.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Root Context: defines shared resources visible to all other web components -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>WEB-INF/oracleserver.properties</value>
		</property>
	</bean>	
	 <!-- dataSource -->
 <bean id="dataSource"
   class="org.apache.commons.dbcp.BasicDataSource">
  <property name="url" value="${url}"/>
  <property name="driverClassName" value="${driver}"/>
  <property name="username" value="${username}"/>
  <property name="password" value="${password}"/>
 </bean> 
 
 <bean id="sqlSessionFactory"
 class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="configLocation">
   <value>classpath:mybatis/setting/SqlMapConfig.xml</value>
  </property>
 </bean>
 
 <bean id="sqlSessionTemplate"
   class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg ref="sqlSessionFactory"/> 
 </bean>  
	
</beans>

 

 

 

9. index.jsp만들기

 

코드는

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
 <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Insert title here</title>
</head>
<body>
<h1><c:redirect url="list"/></h1>
<!-- <a href="list">스프링 게시판 연습</a> -->
</body>
</html>

 

 

10. BoardController.java 작성하기

 

 

GetMapping 주소는 index시작경로와 동일합니다:)

 

 

 

BoardController.java 코드

 

package spring.mvc.board;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import board.data.BoardDaointer;

@Controller
public class BoardController {
	
	@Autowired
	private BoardDaointer dao;
	
	@GetMapping("/list") //index시작 경로와 같다
	public ModelAndView list()
	{
		
		ModelAndView model=new ModelAndView();
		int totalCount=dao.getTotalCount();
		model.addObject("totalCount", totalCount);
		model.setViewName("board/list");
		return model;
		
	}

}

 

 

 

11. list.jsp 작성하기

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
 <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>Insert title here</title>
</head>
<body>
<span class="alert alert-danger">
	<b>총 ${totalCount}개의 데이타가 있습니다</b>

</span>
</body>
</html>

 

12. 컴파일해보기

 

다음과 같이 totalCount가 구해진다면 모든 세팅을 정상적으로 해주셨기 때문에

이제는 zip파일로 보관해주시면 더 좋겠쥬?!

 

나오면 성공!^^!

 


 

++추가++

zip파일로 만들어놓을 때

 

작성해준 프로젝트 - 우클릭 - Export - Export

 

 

Arhive File 클릭

 

 

 

 

zip파일 두고 싶은 곳으로 경로 적어주시고 - finish

 

 

 

사용해주실 때는 Import해주시면 되겠쥬?!

 

 

진짜진짜 끝

728x90
반응형