Android_서버통신(POST)_Server

홍성채·2022년 4월 21일
0

Android

목록 보기
19/27

서버 통신하기(POST 형식)

서버 만들기

  • 프로젝트 생성하기

  • 수정하기(5.1.5버전)

    • 5.1.5 버전으로 수정하기
    • source, target 1.8로 수정하기
  • 업데이트 하기

  • pom.xml에 추가하기

          <!-- OJDBC6 -->
    			<dependency>
    				<groupId>oracle</groupId>
    				<artifactId>ojdbc6</artifactId>
    				<version>11.2.0.3</version>
    			</dependency>
    			<!-- mybatis-spring -->
    			<dependency>
    				<groupId>org.mybatis</groupId>
    				<artifactId>mybatis-spring</artifactId>
    				<version>2.0.7</version>
    			</dependency>
    
    			<!-- hikariCP -->
    			<dependency>
    				<groupId>com.zaxxer</groupId>
    				<artifactId>HikariCP</artifactId>
    				<version>4.0.3</version>
    			</dependency>
    
    			<!-- SPRING-JDBC -->
    			<dependency>
    				<groupId>org.springframework</groupId>
    				<artifactId>spring-jdbc</artifactId>
    				<version>5.3.14</version>
    			</dependency>
    
    			<!-- MYBATIS -->
    			<dependency>
    				<groupId>org.mybatis</groupId>
    				<artifactId>mybatis</artifactId>
    				<version>3.5.5</version>
    			</dependency>
    
    			<!-- Jackson -->
    			<dependency>
    				<groupId>com.fasterxml.jackson.core</groupId>
    				<artifactId>jackson-databind</artifactId>
    				<version>2.9.8</version>
    			</dependency>
    
    			<!-- Lombok -->
    			<dependency>
    				<groupId>org.projectlombok</groupId>
    				<artifactId>lombok</artifactId>
    				<version>1.18.12</version>
    				<scope>provided</scope>
              </dependency>
    		</dependency> 
          <repositories>
    			<repository>
    				<id>oracle</id>
    				<name>ORACLE JDBC Repository</name>
    				<url>http://www.datanucleus.org/downloads/maven2/</url>
    			</repository>
    		</repositories>
          ```
  • root-context.xml에 추가하기

    <!-- HikariCP -->
    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
       <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"/>
       <property name="username" value="hr"/>
       <property name="password" value="hr"/>
    </bean>
    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
       <constructor-arg ref="hikariConfig" />
    </bean>
    
    <!-- myBatis-spring API -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <mybatis-spring:scan base-package="kr.smhrd.mapper"/>
  • 데이터 베이스

    create table t_member(
    		   t_id varchar(20) primary key,
        t_pw varchar(20),
    		   t_nick varchar(30)
    );
    select * from t_member;

소스코드

  • MemberController

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import kr.smhrd.mapper.MemberVO_Mapper;
    import kr.smhrd.model.MemberVO;
    
    @RestController
    public class MemberController {
    	   @Autowired
    		   MemberVO_Mapper mapper;
    		   @RequestMapping(value="/Join", method=RequestMethod.POST)
    		   public String Join(MemberVO vo) {
    			   System.out.println(vo.getId());
    			   System.out.println(vo.getPw());
    			   System.out.println(vo.getNick());
    			   mapper.memberJoin(vo);
    			   return "succes";
    		   }
    		   @RequestMapping(value="/Login", method=RequestMethod.POST)
    		   public String Login(MemberVO vo) {
    			   String nick = mapper.memberLogin(vo);
    		
    			   return "nick";
    		   }
    }

    @RequestMapping :

    1. value : Android에서 전송했을 때 주소의 마지막이 /Join이나 /Login일 경우 각각의 메소드를 호출한다.
    2. method : 전송을 받는 방식을 의미한다.

인터페이스 부분

  • MemberVO_Mapper.java

    import kr.smhrd.model.MemberVO;
    
    public interface MemberVO_Mapper {
    		     //회원가입 메소드
        public void memberJoin(MemberVO vo);
    		     //로그인 메소드
    		   public String memberLogin(MemberVO vo);
    }
  • MemberVO_Mapper.xml

    <insert id="memberJoin" parameterType="kr.smhrd.model.MemberVO">
      	insert into t_member values(#{id}, #{pw}, #{nick})
    </insert>
       <select id="memberLogin" parameterType="kr.smhrd.model.MemberVO" resultType="String">
          select t_nick from t_member where t_id=#{id} and t_pw=#{pw}
       </select>

    id속성 값 : MemberVo_mapper 인터페이스의 메소드명과 동일하게 해야된다.
    parameterType : 클래스타입 정의할 때 클래스 이름까지 정의할 것
    resultType : 반환할 데이터타입

  • MemberVO.java

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class MemberVO {
    		   private String id;
    		   private String pw;
    		   private String nick;
    	
     }
profile
초보 코딩

0개의 댓글