✅ 공통코드
MemberDto
package com.joo.usedmarket.dto;
import java.sql.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class MemberDto { // 롬복을 이용해서 getter setter 함수 자동 완성
private String userId;
private String password;
private String userName;
private int age;
private String email;
private String phone;
private String address;
private String hobby;
private Date enrollDate;
}
MemberController
@Controller // 어노테이션 필수
@RequestMapping("/member") // 이 클래스의 매핑주소들은 모두 /members로 시작함
public class MemberController {
private MemberService service;
private MemberController(MemberService service) { // MemberController 기본생성자 -> service 호출
this.service = service;
}
@GetMapping("/memberAll")
public String selectMemberAll(Model m) {
List<MemberDto> members = service.selectMemberAll();
// MemberService의 selectMemberAll 메소드 호출
System.out.println(members);
return null;
/*
* m.addAttribute("members",service.selectMemberAll()); return
* "member/memberList";
*/
}
}
MemberService
와 MemberServiceImpl
// memberService
public interface MemberService {
List<MemberDto> selectMemberAll();
int insertMember(MemberDto m);
}
// memberServiceImpl
@Service // 어노테이션 필수
public class MemberServiceImpl implements MemberService{
private MemberDao dao;
private SqlSession session; // DAO를 구현하려면 SqlSession 객체가 필요
private MemberServiceImpl(MemberDao dao, SqlSession session) {
this.dao = dao;
this.session = session;
}
@Override
public List<MemberDto> selectMemberAll() {
return dao.selectMemberAll(session); // MemberDao의 selectMemberAll 메서드 실행
}
}
MemberDao
와 MemberDaoImpl
// MemberDao
public interface MemberDao {
List<MemberDto> selectMemberAll(SqlSession session);
int insertMember(SqlSession session, MemberDto m);
}
// MemberDaoImpl
@Repository
public class MemberDaoImpl implements MemberDao {
@Override
public List<MemberDto> selectMemberAll(SqlSession session) {
return session.selectList("member.selectMemberAll"); // session 객체를 통해 매퍼 진입
// ("네임스페이스값.id값")
}
}
✅ mapper 파일에서 2가지 방법으로 데이터 연동
(1) : config.xml
파일에서 typeAlias
키워드 사용
이 방법은 config.xml 파일에서 별칭을 정한 후
mapper.xml 파일에서 resultType
에 넣는 방식이다
// mybatis-config.xml 파일임
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.joo.usedmarket.dto.MemberDto" alias="memberDto"/>
</typeAliases>
</configuration>
// member-mapper.xml 파일임
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
<select id="selectMemberAll" resultType="memberDto">
SELECT * FROM MEMBER
</select>
</mapper>
(2) mapper.xml
파일만 생성해서 resulutMap
을 생성해서 넣는 방식
이 방법은 mapper안에 resultMap을 생성해서 해당 필드에 맞도록 넣어줘야한다
여기선 resultType
이 아닌 resultMap
으로 받아야한다
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
<resultMap id="memberMap" type="com.joo.usedmarket.dto.MemberDto">
<id property="userId" column="user_id"/>
<result property="password" column="password"/>
<result property="userName" column="uesrname"/>
<result property="age" column="age"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
<result property="address" column="address"/>
<result property="hobby" column="hobby"/>
<result property="enrollDate" column="enroll_date"/>
</resultMap>
<select id="selectMemberAll" resultMap="memberMap">
SELECT * FROM MEMBER
</select>
</mapper>