아이디 중복확인을 확인하기 위해서는 아이디를 DB에서 검색해야 하므로
public interface UserMapper {
@Select("select user_name " + "from user_table "+ "where user_id = #{user_id}")
String checkUserIdExist(String user_id);
}
@Bean
public MapperFactoryBean<UserMapper> getUserMapper(SqlSessionFactory factory) throws Exception{
MapperFactoryBean<UserMapper> factoryBean = new MapperFactoryBean<UserMapper>(UserMapper.class);
factoryBean.setSqlSessionFactory(factory);
return factoryBean;
}
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
}
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public boolean checkuserIdExist(String user_id) {
String user_name = userMapper.checkUserIdExist(user_id);
if(user_name == null) {
return true;
}else {
return false;
}
}
}
@RestController
public class RestApiController {
@Autowired
private UserService userService;
@GetMapping("/user/check/{user_id}")
public String checkUserIdExist(@PathVariable String user_id) {
//유저 아이디가 없으면 true 있으면 false
boolean check = userService.checkuserIdExist(user_id);
return check + "";
}
}
<div class="form-group">
<form:label path="user_id">아이디</form:label>
<div class="input-group">
<form:input path="user_id" class="form-control" />
<div class="input-group-append">
<button onclick="checkUserIdExist()" type="button" class="btn btn-primary">중복확인</button>
</div>
</div>
<form:errors path="user_id" style="color:coral"/>
</div>
<div class="form-group" id="idCheck">
<form:hidden path="userIdChecked"/>
<form:errors path="userIdChecked" style="color:coral"/>
</div>
<!--생략 ... -->
<script type="text/javascript" >
function checkUserIdExist(){
const user_id = $("#user_id").val()
if(user_id.length == 0){
alert("아이디를 입력해주세요.")
return
}
$.ajax({
url : '${root}user/check' + user_id,
type: 'get',
dataType : 'text',
success : function(result){
if(result.trim() == 'true'){
alert('사용할 수 있는 아이디입니다')
$("#userIdChecked").val('true')
$("#joinUserBean #idCheck span").text('')
} else {
alert('사용할 수 없는 아이디 입니다')
$("#userIdChecked").val('false')
}
}
})
}
function resetUserIdExist() {
$("#userIdChecked").val('false')
}
</script>