본문 바로가기
CLASS/DATABASE

#6-3/ Oracle+mybatis - selectone/like 사용하는 방식

by hingu 2024. 9. 6.

  👀 검색기능 : like 사용  

- customer_dto.java

package kr.co.sen;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class customer_dto {
	Integer c_idx;
	String c_id,c_name,c_hp,c_email,c_tel,c_post;
	String c_road,c_address,c_level,c_area,c_join;
}

 

 

- mapper.xml

<!-- mybatis + oracle에서 like 사용하는 방식 -->
<select id="customer_one" resultType="kr.co.sen.customer_dto" parameterType="String">	
select * from customer where c_name like '%'||#{c_name}||'%' order by c_idx desc
</select>

=> ⭐ Oracle 에서는 mybatis에서 like 사용시 || 와 함께 사용

           - mysql은 concat('%',#{c_idx},'%')

           - MSSQL 은 like '%'+#{c_idx}+'%'
          

 

- member_repo.java (interface)

package kr.co.sen;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface member_repo {
	//customer data 검색
	List<customer_dto> customer_one(String c_name); 
}

 

 

- member_service.java (interface)

package kr.co.sen;

import java.util.List;

public interface member_service {
	public List<customer_dto> customer_one(String c_name);
}

 

 

- memberServiceImpl.java

package kr.co.sen;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class memberServiceImpl implements member_service{
	@Autowired
	private member_repo member_repo;
	
	//oracle like를 이용한 사용자 이름 검색
	@Override
	public List<customer_dto> customer_one(String c_name) {
		List<customer_dto> all = member_repo.customer_one(c_name);
		return all;
	}
}

 

 

- controller

@GetMapping("/serach_name.do")
public String search_name() {
    String username ="방문석";
    List<customer_dto> all = member_service.customer_one(username);

    System.out.println(all.get(0).getC_email());

    return null;
}

//잘찍힘ㅎ