### mysql 연동 후 dao/dto를 만들 클래스 구성

<br/>

### 쿼리 매퍼를 작성 (ex. UserMapper.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="kia.com.mybatistest.model.dao.UserMapper">
    <select id="getAllUserDataList" resultType="kia.com.mybatistest.model.dto.UserDto">
        select * FROM users
    </select>
</mapper>



- select * FROM {실제 데이터베이스 테이블 명}
- namespace : Mapper interface 위치
- select resultType : Mapping 될 dto 위치



### 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>
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>



### application.yml에 mybatis 설정 추가

mybatis:
  config: mybatis-config.xml // config 위치 : static 바로 아래
  type-aliases-package: kia.com.mybatistest.model // dao,dto가 위치한 곳
  mapper-locations: mybatis/mapper/*.xml // mapper를 위한 xml 파일이 위치한 곳 ( static 아래가 아닌 resources 아래 )




### Mapper interface 작성

@Repository
@Mapper
public interface UserMapper {
    List<UserDto> getAllUserDataList();
}



### Dto 구성

@Getter
@Setter
public class UserDto {
    private Long user_id;
    private String user_name;
    private String user_email;
    private String user_password;
    private LocalDateTime create_at;
    private LocalDateTime modified_at;

    public UserDto() {

    }

    public UserDto(String user_name, String user_email, String user_password, LocalDateTime create_at, LocalDateTime modified_at) {
        this.user_name = user_name;
        this.user_email = user_email;
        this.user_password = user_password;
        this.create_at = create_at;
        this.modified_at = modified_at;
    }

    public UserDto of(String user_name, String user_email, String user_password, LocalDateTime create_at, LocalDateTime modified_at) {
        return new UserDto(user_name, user_email, user_password, create_at, modified_at);
    }
}




### Service interface 작성

public interface UserServiceInterface {

    public List<UserDto> getAllUserDataList();
}



### Service implement 작성

@Service
@RequiredArgsConstructor
public class UserService implements UserServiceInterface {

    private final UserMapper userMapper;

    @Override
    public List<UserDto> getAllUserDataList() {
        return userMapper.getAllUserDataList();
    }
}



### Test Controller 구성

@RequiredArgsConstructor
@RestController
public class MemberTestController {

    private final UserService userService;

    @GetMapping("/user/test")
    public List<UserDto> getAllDataList() {
        return userService.getAllUserDataList();
    }
}

 

 

* github : https://github.com/tjdwns4537/mybatis-practice

 

GitHub - tjdwns4537/mybatis-practice: 실습 및 테스트

실습 및 테스트. Contribute to tjdwns4537/mybatis-practice development by creating an account on GitHub.

github.com

 

+ Recent posts