스프링부트+jsp로 배달사이트 만들기-06 회원가입 02 MYBATIS 사용하기

2021. 12. 5. 19:23스프링부트

데이터베이스 연결을 위해 라이브러리를 추가합니다

		<!-- MYBATIS -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.4</version>
		</dependency>
		
		<!-- 오라클 -->
		<dependency>
			<groupId>com.oracle.database.jdbc</groupId>
			<artifactId>ojdbc8</artifactId>
			<scope>runtime</scope>
		</dependency>

 

 

application.properties에 설정을 추가합니다

 

spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521
spring.datasource.username=SUMIN
spring.datasource.password=QWE123

mybatis.configuration.map-underscore-to-camel-case=true
mybatis.mapper-locations=classpath:com/baemin/mybatis/*.xml
mybatis.type-aliases-package=com.baemin.dto

 

 

joinProc 메서드를 수정하고 overlapCheck 메서드를 추가합니다

	@PostMapping("/join")
	public String joinProc(@Valid Join join, BindingResult bindingResult, Model model) {
		if(bindingResult.hasErrors()) {
			List<FieldError> list = bindingResult.getFieldErrors();
			Map<String, String> errorMsg = new HashMap<>();
			for(int i=0;i<list.size();i++) {
				String field = list.get(i).getField(); 
				String message = list.get(i).getDefaultMessage(); 
				errorMsg.put(field, message);
			}
			model.addAttribute("errorMsg", errorMsg);
			return "user/join";
		}
		
		userService.join(join);
		
		return "redirect:/login";
	}
	
	
	
	
	@ResponseBody
	@GetMapping("/overlapCheck")
	public int overlapCheck(String value, String valueType) {
//		value = 중복체크할 값
//		valeuType = username, nickname
		System.out.println(value);
		System.out.println(valueType);
		int count = userService.overlapCheck(value, valueType);
		
		System.out.println(count);
		return count;
	}

 

 

userService를 주입받기 위해 아래코드를 추가합니다

@Autowired
private UserService userService;

 

 

 

해당 위치에 DAO와 Service를 추가합니다

public interface UserService {
	void join(Join join);

	int overlapCheck(String value, String valueType);
}

 

@Service
public class UserServiceImp implements UserService {

	@Autowired
	private UserDAO userDAO;
	
	@Override
	public void join(Join join) {
		userDAO.join(join);
	}
	

	@Override
	public int overlapCheck(String value, String valueType) {
		return userDAO.overlapCheck(value, valueType);
	}

}

 

public interface UserDAO {
	void join(Join join);
	int overlapCheck(String value, String valueType);
}

 

@Repository
public class UserDAOImp implements UserDAO {

	@Autowired
	private SqlSession sql;
	
	@Override
	public void join(Join join) {
		sql.insert("user.join" , join);	
	}
	
	@Override
	public int overlapCheck(String value, String valueType) {
		Map<String, String> map = new HashMap<>();
		map.put("value", value);
		map.put("valueType", valueType);
		
		return sql.selectOne("user.overlapCheck" ,map);
	}

}

 

 

 mybatis 패키지를 생성하고 쿼리를 작성할 UserMapper.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="user">

	<select id="overlapCheck" resultType="int">
		SELECT COUNT(*) FROM BM_USER WHERE ${valueType } = #{value }
	</select>
	
	
	
	<insert id="join">
		INSERT INTO BM_USER (
			ID,
			USERNAME,
			PASSWORD,
			EMAIL,
			NICKNAME,
			PHONE
		) VALUES (
			USER_ID_SEQ.NEXTVAL,
			#{username },
			#{password },
			#{email },
			#{nickname },
			#{phone }
		)
	</insert>
	
	
	
	
</mapper>

 

mapper의 namespace + 쿼리의 id로 호출해야합니다

 

 

 

join.js의 overlapCheck함수의 ajax부분 주석을 풀고 실행해보면 정상적으로 작동됩니다

https://sumin2.tistory.com/6

 

Ajax 사용방법

// 방법1 예전방식 $.ajax({ url: "/send", // 데이터를 요청할 url type: "GET", // GET, POST, DELETE, PATCH, PUT 등 data : data, // 보내고 싶은 데이터를 JSON 형식으로 작성 success: function(result){ //..

sumin2.tistory.com

 

 

 

 

 

 

db에도 값이 잘들어가는걸 확인할 수 있습니다