장점
단점
@Mapper
maven or gradle 설정을 해주고 Mapper Annotation을 사용해주면 문제없이 사용할 수 있다.
프로젝트 구조
src
-main
-java
-com
-example
-user
-UserController
-UserService
-UserDto
-UserMapper
maven
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
gradle
implementation 'org.springframework.boot:spring-boot-starter-web'
Mapper
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import com.example.user.UserDto;
@Mapper
public interface UserMapper {
@Select("SELECT user_idx, user_name, uesr_age FROM user_table ")
List<UserDto> userList(UserDto userDto);
// insert 후 index값 가져오기
@Insert(" INSERT INTO user_table(user_idx, user_name, user_age) "
+ " VALUES (0, #{user_name}, #{user_age}) ")
@Options(useGeneratedKeys = true, keyProperty = "idx")
int userInsert(UserDto userdto);
}
Dynamic SQL
동적 Sql을 사용하고 싶을 때 <script>를 사용하면 동적 Sql을 사용할 수 있다.
@Update({"<script>",
"update Author",
" <set>",
" <if test='username != null'>username=#{username},</if>",
" <if test='password != null'>password=#{password},</if>",
" <if test='email != null'>email=#{email},</if>",
" <if test='bio != null'>bio=#{bio}</if>",
" </set>",
"where id=#{id}",
"</script>"})
void updateAuthorValues(Author author);