흠 요즘 jpa만 쓰다보니까 MyBatis가 가물가물하다..
하지만 까먹으면 안되니까 다시 설명을 써보자
<!-- Spring Boot Starter for MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.2.0'
아무리 생각해도 Gradle이 편해...
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.model.User;
@Mapper
public interface UserMapper {
User findUserById(int id);
}
<?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.example.demo.mapper.UserMapper">
<select id="findUserById" resultType="com.example.demo.model.User">
SELECT * FROM users WHERE id = #{id}
</select>
</mapper>
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(int id) {
return userMapper.findUserById(id);
}
}
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users/{id}")
public User getUserById(@PathVariable int id) {
return userService.getUserById(id);
}
}
여기서 뭐.. 문제라기보단 다른 점은 폴더 구조를 어떻게 할지
나는 보통
controller
dao
dto
service
serviceImpl
repository
repositoryImpl
구조로 하긴 하는데 흠 이건 사람마다 다르니까.. 아무튼간 포스팅 끝!