天天看點

3、Mybatis-Plus 自定義sql語句

文章目錄

  • ​​1、文檔結構​​
  • ​​2、編寫的mapper檔案​​
  • ​​3、mapper.xml檔案的解釋說明​​
  • ​​4、在mapper接口中定義方法​​
  • ​​5、在mapper.xml檔案中實作接口方法的sql語句,如2​​
  • ​​6、在單元測試中測試自定義的sql語句​​
  • ​​7、測試結果​​

1、文檔結構

3、Mybatis-Plus 自定義sql語句

2、編寫的mapper檔案

<?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.zyz.mybatisplus.mapper.UserMapper">

    <!--    Map<String,Object> selectMapById(Long id);-->
    <select id="selectMapById" resultType="map">
        select id,name,age,email from mybatis_plus.user where id =#{id}
    </select>

</mapper>      

3、mapper.xml檔案的解釋說明

4、在mapper接口中定義方法

@Repository
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根據id查詢使用者資訊為map集合
     * @param id
     * @return
     */
  Map<String,Object> selectMapById(Long id);

}      

5、在mapper.xml檔案中實作接口方法的sql語句,如2

6、在單元測試中測試自定義的sql語句

//自定義sql語句查詢使用者資訊
Map<String,Object> map = userMapper.selectMapById(1L);
System.out.println(map);      

7、測試結果

繼續閱讀