avatar
mjhl0717

Spring

Jun 19
·
4 min read

  • Spring JAVA 버전은 17이 제일 깔끔함

  • project Facets > Java (Version 1.6) > 11로 변경

    • properties → java compiler에서 11로 바꾸면 됨

  • 스프링 두 가지 XML 파일

    1. 전체 설정 파일

    2. 서블릿 파일 : <WEB> 설정

설치 (아래 링크 참고)

http://thedata.kr/down/spring4/01.spring-setup.html

회원가입 & 로그인

  1. 패키지 새로 생성 : hello2 만들기

    until-573until-575
  1. Version 설정

    • Package > Properties

      • java compiler

        until-576
      • Project Facets : Dynamic Web Module / Java 설정

        until-577

  2. pom.xml 설정

    • hello.pom을 복사해서 hello2.pom에 갖다 붙여넣기

      • mybatis, lombok, jstl, springjdbc, mysql, DB tool 등

  1. web.xml 설정

    • Tomcat이 제일 먼저 읽는 파일로 한글 필터 작업 해줘야 함

      <filter>
      	<filter-name>encodingFilter</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>
      	<init-param>
      		<param-name>forceEncoding</param-name>
      		<param-value>true</param-value>
      	</init-param>		
      </filter>
      	
      <filter-mapping>
      	<filter-name>encodingFilter</filter-name>
      	<url-pattern>/*</url-pattern>
      </filter-mapping>

  1. root-context.xml 파일에 DB 설정

    • bean : spring이 new 해서 만든 것 (= class 생성)

    <bean 
    	id="dataSource" 
    	class="org.apache.commons.dbcp2.BasicDataSource"
    	destroy-method="close">
    	<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
    	<property name="url" value="jdbc:mysql://localhost:3306/kdigital?serverTimezone=UTC" />
    	<property name="username" value="root" />
    	<property name="password" value="rpass"></property>	
    </bean>
    
    <bean 
    	id="sqlSessionFactory" 
    	class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="dataSource" ref="dataSource"></property>
    	<property name="configLocation" value="classpath:/sqlmap/mybatis-config.xml"></property>
    </bean>
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    	<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>

  2. 폴더 생성

    • views 하위에 member 폴더 생성

      until-580
    • DB에 테이블이 만들어져야 DTO가 만들 수 있음

      CREATE TABLE `member` (
        `id` varchar(50) NOT NULL,
        `pw` varchar(50) NOT NULL,
        `name` varchar(50) NOT NULL,
        `age` int NOT NULL DEFAULT '0',
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
  3. DTO 생성 후 @Data 생성 (lombok)

    until-581
  4. Controller에 스프링 설정

    until-582
  5. 파일 준비

    • MemberController

      package com.spring.hello2.controller;
         
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
         
      import com.spring.hello2.service.MemberService;
         
      @Controller
      public class MemberController {
      	
      	MemberService service;
      	
      	@Autowired
      	public MemberController() {
      		System.out.println("MemberController()");
        		this.service = service;
        	}
      }
    • MemberDAO

      package com.spring.hello2.dao;
      
      import org.mybatis.spring.SqlSessionTemplate;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Repository;
      
      @Repository
      public class MemberDAO {
      	
      	SqlSessionTemplate sqlSession; //root-context.xml에서 bean으로 생성함
      	
      	@Autowired
      	public MemberDAO() {
      		System.out.println("MemberDAO()");
      		this.sqlSession = sqlSession;
      	}
      }
    • MemberDTO

      package com.spring.hello2.dto;
      
      import lombok.Data;
      
      @Data
      public class MemberDTO {
      	String id;
      	String pw;
      	String name;
      	int age;
      }
    • MemberService

      package com.spring.hello2.service;
      
      public interface MemberService { }
    • MemberService.impl

      package com.spring.hello2.service.impl;
      
      import org.springframework.stereotype.Service;
      
      import com.spring.hello2.dao.MemberDAO;
      import com.spring.hello2.service.MemberService;
      
      @Service
      public class MemberServiceImpl implements MemberService {
      	
      	MemberDAO dao;
      	
      	public MemberServiceImpl() {
      		System.out.println("MemberServiceImpl()");
      		this.dao = dao;
      	}
      }

      => 이대로 실행하면 mapper 파일이 없어 오류가 나므로 mapper 파일 생성해줘야 함

      until-591
    • mybatis-config

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
      <configuration>
        
        <mappers>
          <mapper resource="/sqlmap/mapper/member-mapper.xml"/>
        </mappers>
      </configuration>
    • member-mapper.xml

      <?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="member">
      	<insert id="insert" parameterType="MemberDTO">
      	<![CDATA[
      		insert into book(id, pw, name, age) values (#{id}, #{pw}, #{name}, #{age})
      	]]>
      	</insert>
      
      	<update id="update" parameterType="MemberDTO">
            <![CDATA[
               update member set pw = #{pw}, name = #{name}, age = #{age}
               where id = #{id}
            ]]>
      	</update>
      
      	<delete id="delete" parameterType="MemberDTO">
      	<![CDATA[
      		delete from member where id = #{id}
      	]]>
      	</delete>
      
      	<select id="list" resultType="MemberDTO">
      	<![CDATA[
      		select id, pw, name, age
      		from member
      		where 1 = 1
      	]]>
      	</select>
      
      	<select id="detail" parameterType="MemberDTO" resultType="MemberDTO">
      	<![CDATA[
      		select id, pw, name, age
      		from member
      		where id = #{id} 
      	]]>
      	</select>
      
      </mapper>

  6. home.jsp 경로 설정

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
    <html>
    <head>
    	<title>Home</title>
    </head>
    <body>
    <h1>
    	Member
    </h1>
    <ul>
    <li><a href="member/list">List</a></li>
    <li><a href="member/create">Create</a></li>
    <li><a href="member/login">Login</a></li>
    <li><a href="member/logout">Logout</a></li>
    </ul>
    </body>
    </html>