Dao를 Spring Boot의 @Mapper를 사용하지않고 직접 구현하기

MyBatisNoticeDao.java

반복적인 코드를 Contract injection 해준다.

MyBatisNoticeDao.java
package com.newlecture.web.dao.mybatis;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.newlecture.web.dao.NoticeDao;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.entity.NoticeView;
@Repository
public class MyBatisNoticeDao implements NoticeDao {
private NoticeDao mapper;
@Autowired
public MyBatisNoticeDao(SqlSession sqlSession) {
mapper = sqlSession.getMapper(NoticeDao.class);
}
@Override
public List<NoticeView> getViewList(int offset, int size, String field, String query, boolean pub) {
return mapper.getViewList(offset, size, field, query, pub);
}
@Override
public int getCount(String field, String query, boolean pub) {
return mapper.getCount(field, query, pub);
}
@Override
public NoticeView getView(int id) {
// TODO Auto-generated method stub
return mapper.getView(id);
}
@Override
public Notice getNext(int id) {
// TODO Auto-generated method stub
return mapper.getNext(id);
}
@Override
public Notice getPrev(int id) {
// TODO Auto-generated method stub
return mapper.getPrev(id);
}
@Override
public int update(Notice notice) {
// TODO Auto-generated method stub
return mapper.update(notice);
}
@Override
public int delete(int id) {
// TODO Auto-generated method stub
return mapper.delete(id);
}
@Override
public int insert(Notice notice) {
// TODO Auto-generated method stub
return mapper.insert(notice);
}
@Override
public int deleteAll(int[] ids) {
// TODO Auto-generated method stub
return mapper.deleteAll(ids);
}
@Override
public int updatePubAll(int[] id, boolean pub) {
// TODO Auto-generated method stub
return mapper.updatePubAll(id, pub);
}
}
NoticeDaoMapper.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="com.newlecture.web.dao.NoticeDao">
<select id="getViewList" resultType="com.newlecture.web.entity.NoticeView">
select * from noticeview
<where>
<if test="query != null or query != ''">
${field} like '%${query}%'
</if>
and pub = #{pub}
</where>
order by regdate desc
limit #{offset}, #{size}
</select>
<select id="getCount" resultType="int">
select count(id) count from notice
<where>
<if test="query != null or query != ''">
${field} like '%${query}%'
</if>
and pub = #{pub}
</where>
</select>
<select id="getView" resultType="com.newlecture.web.entity.NoticeView">
select * from noticeview
where id=#{id]
</select>
<select id="getNext" resultType="com.newlecture.web.entity.Notice">
select * from notice
where regdate > (select regdate from notice where id = #{id})
limit 1
</select>
<select id="getPrev" resultType="com.newlecture.web.entity.Notice">
select * from notice
where regdate < (select regdate from notice where id = #{id})
order by regdate desc
limit 1
</select>
<update id="update" parameterType="com.newlecture.web.entity.Notice">
update
set
title = #{title},
content = #{content},
hit = #{hit},
pub = #{pub}
where id = #{id}
</update>
<insert id="insert" parameterType="com.newlecture.web.entity.Notice">
insert into Notice(title,content,memberId)
values(#{title},#{content},#{memberId})
</insert>
<delete id="delete">
delete from Notice
where id=#{id}
</delete>
<delete id="deleteAll">
delete from Notice
where id in
<foreach item="id" index="index" collection="ids"
open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<update id="updatePubAll">
update Notice
set
pub = #{pub}
where id in
<foreach item="id" index="index" collection="ids"
open="(" separator="," close=")">
#{id}
</foreach>
<!--
<update id="updatePubAll">
update Noitce
set
pub = case id
<foreach item="id" collection="pubIds" >
when #{id} then 1
</foreach>
<foreach item="id" collection="closeIds" >
when #{id} then 0
</foreach>
end
where id in (
<foreach item="id" collection="pubIds" >
#{id}
</foreach>
,
<foreach item="id" collection="closeIds" >
#{id}
</foreach>
)
-->
<!--
update Noitce
set
pub = case id
when 14 then 0
when 15 then 0
when 21 then 1
when 22 then 1
end
where id in (14, 15, 21, 22)
-->
</update>
</mapper>
Service 구현
noticeService.java
package com.newlecture.web.service;
import java.util.List;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.entity.NoticeView;
public interface NoticeService {
// 페이지 요청할 때
List<NoticeView> getViewLsit(boolean pub);
// 검색을 요청할 때
List<NoticeView> getViewLsit(String field, String query, boolean pub);
// 페이지를 요청할 떄
List<NoticeView> getViewList(int page, String field, String query, boolean pub);
int getCount();
int getCount(String field, String query, boolean pub);
// 자세한 페이지를 요청할 경우
NoticeView getView(int id);
Notice getNext(int id);
Notice getPrev(int id);
// 일괄공개를 요청할 때
int updatePubAll(int[] pubIds, int[] closeIds);
// 일괄삭제를 요청할 때
int deleteAll(int[] ids);
// 수정 페이지를 요청할 경우
int update(Notice notice);
int delete(int id);
int insert(Notice notice);
}
NoticeServiceImp.java
package com.newlecture.web.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.newlecture.web.dao.NoticeDao;
import com.newlecture.web.entity.Notice;
import com.newlecture.web.entity.NoticeView;
@Service
public class NoticeServiceImp implements NoticeService{
@Autowired
private NoticeDao noticeDao;
@Override
public List<NoticeView> getViewLsit(boolean pub) {
// TODO Auto-generated method stub
return getViewList(1, "title", "", pub);
}
@Override
public List<NoticeView> getViewLsit(String field, String query, boolean pub) {
// TODO Auto-generated method stub
return getViewList(1, field, query, pub);
}
@Override
public List<NoticeView> getViewList(int page, String field, String query, boolean pub) {
int size = 10;
int offset = 0+(page-1)*size; //page가 1일 경우에 offset은 0, 2->10, 3->20 패턴이 있다.(등차수열) an=a1+(n-1)d -> 0+(page-1)*10
List<NoticeView> list = noticeDao.getViewList(offset, size, field, query, pub);
return list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return getCount("title", "", true);
}
@Override
public int getCount(String field, String query, boolean pub) {
// TODO Auto-generated method stub
return noticeDao.getCount(field, query, pub);
}
@Override
public NoticeView getView(int id) {
NoticeView notice = noticeDao.getView(id);
return notice;
}
@Override
public Notice getNext(int id) {
// TODO Auto-generated method stub
return noticeDao.getNext(id);
}
@Override
public Notice getPrev(int id) {
// TODO Auto-generated method stub
return noticeDao.getPrev(id);
}
@Override
public int updatePubAll(int[] pubIds, int[] closeIds) {
// TODO Auto-generated method stub
int result = 0;
result += noticeDao.updatePubAll(pubIds, true);
result += noticeDao.updatePubAll(closeIds, false);
return result;
}
@Override
public int deleteAll(int[] ids) {
// TODO Auto-generated method stub
return noticeDao.deleteAll(ids);
}
@Override
public int update(Notice notice) {
// TODO Auto-generated method stub
return noticeDao.update(notice);
}
@Override
public int delete(int id) {
// TODO Auto-generated method stub
return noticeDao.delete(id);
}
@Override
public int insert(Notice notice) {
// TODO Auto-generated method stub
return noticeDao.insert(notice);
}
}