Skip to content

Instantly share code, notes, and snippets.

@DaeAkin
Created May 28, 2019 08:09
Show Gist options
  • Save DaeAkin/be8bc7a0b2c696044dbf90a1d52d96a2 to your computer and use it in GitHub Desktop.
Save DaeAkin/be8bc7a0b2c696044dbf90a1d52d96a2 to your computer and use it in GitHub Desktop.
Java String Mybatis Libaray Basic CRUD Template
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.www.homedoc.dto.BoardDto;
import com.www.homedoc.dto.PaginationDto;
import com.www.homedoc.dto.QuoDto;
@Repository("boardDao")
public class BoardDaoImpl extends CRUDDaoImpl<BoardDto, Integer>{
// namespace of mapper
private static final String mappingName =
"com.www.homedoc.dao.BoardDaoImpl";
public BoardDaoImpl() {
super(mappingName);
}
}
<?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.www.homedoc.dao.BoardDaoImpl"><!-- Class be looked -->
<!-- CRUD Basic SQLs -->
<insert
id="insert"
parameterType="com.www.homedoc.dto.BoardDto">
insert into board(
title,
writer,
datetime,
category,
content,
thumbnail
)values(
#{title},
#{writer},
now(),
#{category},
#{content},
#{thumbnail}
)
</insert>
<update id="update" parameterType="com.www.homedoc.dto.BoardDto">
update
board
set
title = #{title},
datetime = #{datetime},
category = #{category},
content = #{content}
where
no = #{no}
</update>
<delete id="delete" parameterType="com.www.homedoc.dto.BoardDto">
delete
from
board
where
no = #{no}
</delete>
<select id="selectAll"
resultType="com.www.homedoc.dto.BoardDto">
select
*
from
board
</select>
<select id="selectByNo"
resultType="com.www.homedoc.dto.BoardDto">
select
*
from
board
where
no = #{no}
</select>
<delete id="deleteAll">
delete
from
board
</delete>
</mapper>
import java.util.List;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
public abstract class CRUDDaoImpl<T, PK> implements CRUDDao<T, PK> {
@Inject
SqlSession sqlSession;
//mappernamespace to be look
private String mappingName;
public CRUDDaoImpl(String mappingName) {
this.mappingName = mappingName;
}
public static final String MAPPING_INSERT = "insert";
//mapping key "update" must be defined in mapping file
public static final String MAPPING_UPDATE = "update";
//mapping key "delete" must be defined in mapping file
public static final String MAPPING_DELETE = "delete";
//mapping key "selectAll" must be defined in mapping file
public static final String MAPPING_SELECT_ALL = "selectAll";
//mapping key "selectById" must be defined in mapping file
public static final String MAPPING_SELECT_BY_NO = "selectByNo";
public static final String MAPPING_DELETE_ALL = "deleteAll";
@Override
public int insert(T dto) {
// TODO Auto-generated method stub
return sqlSession.insert(mappingName +"." + MAPPING_INSERT,dto);
}
@Override
public int update(T dto) {
// TODO Auto-generated method stub
return sqlSession.update(mappingName + "." + MAPPING_UPDATE , dto);
}
public int deleteByNo(PK no) {
return sqlSession.delete(mappingName + "." +MAPPING_DELETE , no);
}
@Override
public List<T> selectAll() {
// TODO Auto-generated method stub
return sqlSession.selectList(mappingName + "." +MAPPING_SELECT_ALL);
}
@Override
public T selectByNo(PK no) {
// TODO Auto-generated method stub
return sqlSession.selectOne(mappingName +"." + MAPPING_SELECT_BY_NO, no);
}
@Override
public void deleteAll() {
sqlSession.delete(mappingName + "." + MAPPING_DELETE_ALL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment