XML 로 Mybatis 설정하기

main class 작성

@MapperScan("com.blake.demo.mapper")
@SpringBootApplication
public class XmlMaybatisApplication {

	public static void main(String[] args) {
		SpringApplication.run(XmlMaybatisApplication.class, args);
	}

}

Mapper interface 작성

public interface UserMapper {

    User findByName(@Param("name") String name);

    int insert(@Param("name") String name, @Param("age") Integer age);

}

.properties 혹은 .yml 파일에 설정 추가

mybatis.mapper-locations=classpath:mapper/*.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="com.blake.demo.mapper.UserMapper">
    <select id="findByName" resultType="com.blake.demo.entity.User">
        SELECT * FROM USER WHERE NAME = #{name}
    </select>

    <insert id="insert">
        INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})
    </insert>
</mapper>

끝!

Last updated