3、Mybatis-Plus 自定义sql语句

1、文档结构

 

 

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、测试结果

 

posted on 2022-08-16 13:50  热爱技术的小郑  阅读(507)  评论(0)    收藏  举报