MyBatisPlus对MyBatis只做增强,并不改变原有的功能。

仍然可以采用自定义查询:

 

第一步:配置映射文件的位置

 

 

 

映射文件格式:

<?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="">

</mapper> 

 

 

第二步:创建mapper层文件,因为主类已经加了@MapperScan扫描,所以mapper层可以不加@Mapper

public interface UserMapper extends BaseMapper<User> {
    @MapKey("id")
    Map<String,Object> selectMapById(@Param("ID") Long id);
}

 

第三部:创建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.zhy.mapper.UserMapper">

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

 

测试代码:

 @Test
    public void test(){
        Map<String,Object> map = userMapper.selectMapById(1L);
        System.out.println(map);
    }

 

输出结果:

 

 

项目结构:

 

posted on 2022-05-23 15:54  西凉#  阅读(327)  评论(0编辑  收藏  举报