MyBatis
📚 MyBatisは
MyBatisとはSQL Mapping Frameworkのことです.JavaコードからSQL文を分離して管理し、パラメータ設定とクエリー結果を読み取るコードを削除することで、コードの簡潔性により生産性が向上し、メンテナンスが容易になります.
MyBasisをコードで表示
📚 ボードテーブルの作成
create table board (
bno int auto_increment primary key,
title varchar(30) not null,
content text not null,
writer varchar(30) not null,
view_cnt int default 0 null,
comment_cnt int default 0 null,
reg_date datetime null
);
📚 Mapper XML
boardテーブルにCRUD用のSQL文を記述します.parameterTypeとresultTypeを区別して書くべきです.select文では、bnoとしてパブリッシュ記事を選択してBoardDtoオブジェクトを返すため、parameterTypeはbnoタイプのint、resultTypeはBoardToオブジェクトとして指定します.
<mapper namespace="com.project.dao.BoardMapper">
<select id="select" parameterType="int" resultType="BoardDto">
select bno,title,content,
writer,view_cnt,comment_cnt,
reg_date,up_date
from board
where bno = #{bno}
</select>
<delete id="delete" parameterType="int">
delete from board where bno = #{bno} and writer = #{writer}
</delete>
<delete id="deleteAll">
delete from board
</delete>
<insert id="insert" parameterType="BoardDto">
insert into board (title,content,writer)
values ('title','content','jane')
</insert>
<update id="update" parameterType="BoardDto">
update board
set title = #{title},
content = #{content},
up_date = now()
where bno = #{bno}
</update>
</mapper>
📚 typeAliasの指定
Mapper xmlでresultTypeを直接オブジェクト名として指定できるのは、mybatis設定でtypeAliasをオブジェクト名として指定しているからです.次の例に示すように、typeAliasタグのtypeにパッケージ名を入力し、aliasに別名を指定するには、typeAliasを使用します.
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="BoardDto" type="com.fastcampus.ch4.domain.BoardDto"/>
</typeAliases>
</configuration>
📚 DTO(Data Transfer Object)
レイヤ間でデータを交換するオブジェクト
BoardDto
public class BoardDto {
private Integer bno;
private String title;
private String content;
private String writer;
private int view_cnt;
private int comment_cnt; //댓글 개수
private Date reg_date;
private Date up_date;
public BoardDto(){} //기본생성자
public BoardDto(String title, String content, String writer){
this.title = title;
this.content = content;
this.writer = writer;
}
public Integer getBno() {
return bno;
}
public void setBno(Integer bno) {
this.bno = bno;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public int getView_cnt() {
return view_cnt;
}
public void setView_cnt(int view_cnt) {
this.view_cnt = view_cnt;
}
public int getComment_cnt() {
return comment_cnt;
}
public void setComment_cnt(int comment_cnt) {
this.comment_cnt = comment_cnt;
}
public Date getReg_date() {
return reg_date;
}
public void setReg_date(Date reg_date) {
this.reg_date = reg_date;
}
public Date getUp_date() {
return up_date;
}
public void setUp_date(Date up_date) {
this.up_date = up_date;
}
@Override
public String toString() {
return "BoardDto{" +
"bno=" + bno +
", title='" + title + '\'' +
", content='" + content + '\'' +
", writer='" + writer + '\'' +
", view_cnt=" + view_cnt +
", comment_cnt=" + comment_cnt +
", reg_date=" + reg_date +
", up_date=" + up_date +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BoardDto boardDto = (BoardDto) o;
return Objects.equals(bno, boardDto.bno) && Objects.equals(title, boardDto.title) && Objects.equals(content, boardDto.content) && Objects.equals(writer, boardDto.writer);
}
@Override
public int hashCode() {
return Objects.hash(bno, title, content, writer);
}
}
boardテーブルのデータをBoardtoオブジェクトに読み込みます.掲示板に文章を書くときは、BoardToオブジェクトに値を入れ、データベースのBoardテーブルに格納することができます.📚 DAOインタフェース
DAOインタフェースは省略でき、Mapperを作成した直後にDAO実装クラスを作成できます.
public interface BoardDao {
BoardDto select(Integer bno) throws Exception;
int delete(Integer bno) throws Exception;
int insert(BoardDto dto) throws Exception;
int update(BoardDto dto) throws Exception;
int increaseViewCnt(Integer bno) throws Exception;
}
📚 DAOインタフェースの実装
@RepositoryとDAOオブジェクト
DAOオブジェクトを作成し@Repositoryプレゼンテーションを貼り付けた後、SpringはSpring Binとして登録し、Data Access Layerを認識します.
@Repository
public class BoardDaoImpl implements BoardDao {
@Autowired
private SqlSession session;
private static String namespace="com.project.dao.BoardMapper";
public BoardDto select(Integer bno) throws Exception {
return session.selectOne(namespace + "select", bno);
}
}
Board MapperBoardDao
📚 DAOテストコードの作成
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"})
public class BoardDaoImplTest {
@Autowired
BoardDao boardDao;
@Test
public void select() throws Exception {
assertTrue(boardDao != null);
System.out.println("boardDao = " + boardDao);
BoardDto boardDto = boardDao.select(1);
System.out.println("boardDto: " + boardDto);
assertTrue(boardDto.getBno().equals(1));
}
}
この文章は南宮星のばね受け話を聞いて整理したものです.Reference
この問題について(MyBatis), 我々は、より多くの情報をここで見つけました https://velog.io/@jooog/MyBatisテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol