ioc컨테이너에 담긴 객체는 autowired를 통해 불러온다.

@Repository ioc에 담아줘

@Autowired ioc에서 관련 객체 꺼내줘

 

맵핑방식을 xml로 바꾸자~

@Mapper대신 xml구현체를 직접 만들어보자

 

1. https://mybatis.org/mybatis-3/configuration.html#mappers

  NoticeDaoMapper.xml에 매퍼태그를 넣는다.

2. NoticeDao에는 인터페이스이므로 구현할 함수목록만 남긴다.

3. MybatisNoticeDao 생성 (클래스생성시 add눌러서 NoticeDao 인터페이스선택)

현재쓰고있는 마이바티스버전 / resultType = 반환할때의 그릇형식, 반환하게될 객체의 그릇(select의 컬럼)

마이바티스를 스프링에서 사용할때 필요한설정을 갖고있는 라이브러리가 추가되어있다.(자동) 3.5.

 

'%${query}%' 를 쓴이유 ?

#을 쓰면 't'가 들어간다. (값형태)

$는 날것그대로


마이바티스는 동일이름 메소드 중복불가


 

주황:스프링

@controller, @servicem @repository를  ioc에 담는다

@mapper 객체가 바로 ioc에 담긴다

 

그린:마이바티스

마이바티스는 mapper Container를 가지고있다. Mapper객체가 담겨진다.

두 종류로 설정할 수있다.

1. 어노테이션 @Mapper (매퍼객체를 바로씀), 마이바티스에 특화된 인터페이스(제약이생김) , ioc에서 바로 사용가능

2. xml ??Mapper.xml (매퍼컨테이너에 담기만) , 간접적으로쓴다, 자기컨테이너에담는다.Dao는 내가 직접구현할게

두개다 매퍼객체가 매처컨테이너에 담긴다

ioc컨테이너가 매퍼객체를 사용 할 수있게 넣어준다

다오를 구현할때 쓰기위해 객체를 꺼낸다. (MybatisNoticeDao)

 

매퍼개체를 꺼내오는 도구 : sqlSession클래스사용 ( private SqlSession sqlSession)

getMapper()매퍼객체를 가져와

 

마이바티스가 Mapper.xml을 읽으면 Mapper Container 에 Mapper객체를 넣는다.

우리는 그 컨테이너에 있는것을 꺼내서(getMapper) 실제 마이바티스를 구현할때 쓴다. (class MybatisNoticeDao)

무슨 장점 ?

구현체에 대한 내용이 노출되지않는다.

인터페이스는 유지.

쉽게 바꿀수있다. 

독립적인 인터페이스

다오를 직접구현하는게 불편해보여도 정상적인 방법

인터페이스에 @select등 붙이지 않는게 바람직

Jdbc 코드를 2줄코드로 완성

NoticeDao mapper = sqlSession.getMapper(??;

return mapper.getList();

 

모든Mapper의 위치 지정(내가 만든 메퍼정보를 마이바티스에게 알린다) (xml위치지정)

 


getList 인자값이 각각 다른경우 (인자가없거나 하나일떄)

기본값을 넣어준다.

다른데로 sendredirection

마이바티스는 xml의 한줄맵핑으로 ~~

 


디테일페이지

id가 널인 가능성이없으므로 integer말고 int로하기


삭제페이지

update,insert,delete는 반환타입을 주로 int쓴다.

1. 컨트롤러
1
2
3
4
5
6
7
8
9
10
11
12
13
 
@Controller("adminNoticeController")
@RequestMapping("/admin/notice/")//공통분모
public class NoticeController {
 
 @RequestMapping("del")
    public String del(int id) {
 
     service.delete(id);
 
    return"redirect:list";
   }
}
cs

2. 다오매퍼

1
2
3
4
NoticeDaoMapper
<delete id="delete" parameterType="int">
  delete from Notice where id=#{id}
</delete>
cs

3. 서비스인터페이스

1
2
3
public interface NoticeService {
int delete(int id);
}
cs

4. 서비스인터페이스 구현

1
2
3
4
5
6
7
@Service//component
   public class NoticeServiceimp implements NoticeService {
   @Override
   public int delete(int id) {
   return dao.delete(id);
  }
}
cs

5.다오인터페이스

1
2
3
4
public interface NoticeDao {
 
     int delete(int id);
}
cs

6. 다오인터페이스 구현

1
2
3
4
5
6
7
8
9
@Repository//ioc에 MybatisNoticeDao를 담아줘
public class MybatisNoticeDao implements NoticeDao {
 
  @Override
  public int delete(int id) {
 
  return mapper.delete(id);
  }
}
cs

 


 

resultType SELECT문 실행 결과를 담을 객체
parameterType 이 속성에 지정한 객체의 프로퍼티값이 SQL문의 입력 파라미터에 지정된다.(insert,update,delete)

다오 -> 서비스->컨트롤러 순으로 만들어보자

업데이트

1. 다오 인터페이스

1
2
3
4
5
public interface NoticeDao {
    int update(Notice notice);
 
}
 
cs

2. 다오매퍼

1
2
3
4
5
6
7
8
9
10
11
12
    <update id="update" parameterType="com.newlecture.web.entity.Notice">
        update Notice
        SET
        title = #{title},
        writerId = #{writerId},
        content = #{content},
        hit = #{hit: 0},
        files = #{files},
        pub = #{pub}
        where id=#{id}
 
    </update>
cs

3. 마이바티스다오 (다오인터페이스 구현)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Repository//ioc에 MybatisNoticeDao를 담아줘
public class MybatisNoticeDao implements NoticeDao {
    
    //@Autowired//ioc에있는 연관객체를 가져와//mybatis-spring라이브러리:스프링에담아주는역할
    private SqlSession sqlSession;    
    private NoticeDao mapper;
    
    @Autowired
    public MybatisNoticeDao(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
        mapper = sqlSession.getMapper(NoticeDao.class);
    }
 
    @Override
    public int update(Notice notice) {
 
        return mapper.update(notice);
    }
 
}
 
cs

 

ㅇ인서트,업데이트는 인자쓸수잇다

셀렉트는 파라미터타입지정할 수없다.

약속되어있는 param1, param2를 쓴다

 

프로젝트진행 : 디비사용되어야한다 ->데이터베이스구축되어야한다..

https://elfinlas.github.io/2018/02/18/spring-parameter/

https://logical-code.tistory.com/25

+ Recent posts