본문 바로가기
카테고리 없음

국비 - 스프링 MV 로그인

by Jeong.dev 2022. 10. 1.

■ 배운것

- 스프링 MVC 로그인 기능 구현

1. 마이바티스 연동

2. MCV 만들기 (컨트롤러, 서비스, 맵퍼)

 

 

* nullPointException : null.sava()

참조변수에 null 있는데 그 변수에 메소드 실행하려고 해서

 

 

■ 스프링 MVC 로그인 기능 구현

1. 마이바티스 연동

pom.xml에 마이바티스 관련 라이브러리 추가

- ojdbc6, mybatis, mybatis-spring, spring-jdbc, commons-dbcp2

 

src > main > resources 밑에

- mappers 폴더 생성

- member-mapper.xml 파일 생성(mappers > member 안에, 파일종류 : ????) : member 관련 쿼리문

- dirver.properties 파일 생성 : DB 연결 정보

 

mybatis-context.xml 생성(WEB-INF > spring )

 

 파일종류 : Spring Bean Configuration File / beans 첫번째꺼, c, p 선택

 

- mybatis-context (DB관련 빈 생성함)

dataSource : DB 연결정보

mapperScanner : @Mapper 어노테이션 스캔해서 인터페이스 객체를 만들어줌(빈 만듬)

mybatisConfig, sqlSessionFactory, sqlSession, transactionManager 작성

 

- root-context에 xmlns:context 추가(아래꺼로 바꾸기)

<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.xsd">

<import resource="mybatis-context.xml"/>

: mybatis-context만들고 root-context에서 읽어올 수 있게 한다.

<context:property-placeholder location="classpath:driver.properties"/>

: driver.properties 파일의 값들 사용하려고 지정

 

파일 생성할 때 xmlns:context 추가하는 방법

 

 

 

2. MCV 만들기 (컨트롤러, 서비스, 맵퍼)

 

- @Controller, @Service, @Mapper 어노테이션 붙이고 해당 객체들 사용하는 곳에서

@Autowired 붙이면 주입받아서 사용할 수 있다.

 

@Controller MemberController - @Autowired private MemberService service; (서비스 인터페이스)

@Service MemberServiceImpl - @Autowired private MemberMapper mapper;

@Mapper MemberMapper

 

MemberController 

- 화면에서 id, pwd 받는다

- 서비스에게 id, pwd 넘김

- 서비스 결과에 따라 보여주는 화면 지정

MemberServiceImpl 

- 넘겨받은 아이디를 맵퍼에게 넘겨서 DB에 있는 값인지 확인

- 있으면 그 아이디의 비번과 컨드롤러에게 받은 비번이 같은지 확인

MemberMapper

- 넘겨 받은 아이디를 member-mapper의 아이디 찾는 쿼리문에 대입해서 실행

 

 

 

MemberMapper 인터페이스 만들고 @Mapper

- member-mapper.xml namespace에  MemberMapper 인터페이스의 패키지명.클래스이름 넣기

- member-mapper.xml에서 사용할 쿼리문의 id값을 추상메소드로 작성

 

 

 

 

 

 

댓글