<?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.itheima.mybatis.mapper.UserMapper">
<!-- 根据用户ID查询用户信息 -->
<select id="findUserById" parameterType="int" resultType="User">
SELECT
* FROM USER WHERE id =#{id}
</select>

<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.itheima.mybatis.po.User">
<selectKey keyProperty="id" resultType="int" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>

INSERT INTO USER
(username,birthday,sex,address)
VALUES(#{username},#{birthday},#{sex},#{address})
</insert>

<!-- 定义sql片段 -->
<!-- sql片段内,可以定义sql语句中任何部分 -->
<!-- sql片段内,最好不用将where和select关键字声明在内 -->
<sql id="whereClause">
<!-- if标签:可以对输入的参数进行判断 -->
<!-- test:指定判断表达式 -->
<if test="user != null">
<if test="user.username != null and user.username != ''">
AND username LIKE '%${user.username}%'
</if>
<if test="user.sex != null and user.sex != ''">
AND sex = #{user.sex}
</if>
</if>

<if test="idList != null">
<!-- AND id IN (#{id},#{id},#{id}) -->

<!-- collection:表示pojo中集合属性的属性名称 -->
<!-- item:为遍历出的结果声明一个变量名称 -->
<!-- open:遍历开始时,需要拼接的字符串 -->
<!-- close:遍历结束时,需要拼接的字符串 -->
<!-- separator:遍历中间需要拼接的连接符 -->
AND id IN
<foreach collection="idList" item="id" open="(" close=")"
separator=",">
#{id}
</foreach>
</if>
</sql>

<!-- 综合查询,查询用户列表 -->
<select id="findUserList" parameterType="com.itheima.mybatis.po.UserQueryVO"
resultType="user">
SELECT * FROM user
<!-- where标签:默认去掉后面第一个AND,如果没有参数,则把自己干掉 -->
<where>
<!-- 引入sql片段 -->
<include refid="whereClause" />
</where>
</select>

<!-- 综合查询用户总数 -->
<select id="findUserCount" parameterType="com.itheima.mybatis.po.UserQueryVO"
resultType="int">
SELECT count(*) FROM user
<!-- where标签:默认去掉后面第一个AND,如果没有参数,则把自己干掉 -->
<where>
<!-- 引入sql片段 -->
<include refid="whereClause" />
</where>
</select>

<!-- resultMap入门 -->
<!-- id标签:专门为查询结果中唯一列映射 -->
<!-- result标签:映射查询结果中的普通列 -->
<resultMap type="user" id="UserRstMap">
<id column="id_" property="id" />
<result column="username_" property="username" />
<result column="sex_" property="sex" />
</resultMap>

<select id="findUserRstMap" parameterType="int" resultMap="UserRstMap">
Select id id_,username username_,sex sex_ from user where id = #{id}
</select>
</mapper>

public interface UserMapper {
// 1、 根据用户ID查询用户信息
public User findUserById(int id) throws Exception;

// 3、 添加用户
public void insertUser(User user) throws Exception;

//综合查询
public List<User> findUserList(UserQueryVO vo);

//综合查询用户总数
public int findUserCount(UserQueryVO vo);

//resultMap入门
public User findUserRstMap(int id);
}

转载于:https://www.cnblogs.com/laiyongqing/p/10188057.html

mybatis返回map操作相关推荐

  1. 12、mybatis返回map单条及多条记录

    文章目录 1.mybatis返回map单条记录 1).EmployeeMapper接口 2).EmployeeMapper.xml 3).Test 4).测试结果 2.mybatis返回map多条记录 ...

  2. java mybatis 返回map_mybatis返回map集合的格式是什么?mybatis返回map集合实例

    Mybatis因为会与数据库交互,所以经常会有返回map集合的场景,那一般mybatis返回map集合的格式是什么呢?下面小编就用一些实例与你分享分享吧. 例1:返回key不定:返回key为学员id, ...

  3. Mybatis返回Map的一种实现

    Mybatis返回Map的一种实现 前言 在使用Mybatis进行系统开发的时候,有时候我们会有这么一种需求:我们希望通过Mybatis查询某一个表返回的结果是一个Map,而这个Map的Key是表的一 ...

  4. Mybatis返回Map

    返回一条记录的map resultType="map"   key就是列名,值就是对应的值 多条记录封装成一个map Select返回类型中是返回Map时,是对方法中是否存在注解@ ...

  5. mybatis返回map键值对_mybatis返回map结果集怎么配置

    匿名用户 1级 2018-06-10 回答 一.概述 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返 ...

  6. mybatis返回map键值对_mybatis返回map key怎么指定

    展开全部 一.概述 MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是 ...

  7. 若依mybatis返回map将下划线命名转为驼峰式命名

    默认情况,若依不支持这个配置,需要我们重写配置类才能到达需求. 目录 一.需求情景复现 二.重写配置类 (1)导入guava依赖 (2)  编写配置类

  8. mybatis 字段名自动转小写_mybatis 返回Map类型key改为小写的操作

    默认情况下,当resultType="java.util.Map"时,返回的key值都是大写的. 现在想key改成自己想要的,只需为查询出来的字段增加个别名即可. 如: selec ...

  9. 【mybatis】mybatis中 返回map集合

    关于mybatis返回map集合的操作: 1.mapper.xml中写一个查询返回map的sql <select id="findMap" parameterType=&qu ...

最新文章

  1. [bat] 使用bat文件保证指定程序运行
  2. 青岛市教师职称计算机考试成绩查询系统,吴春雷
  3. matlab 邻近度 离群点_Matlab基础学习第一弹!干货来啦!
  4. 5G NR 同步过程
  5. WebView::drawExtras
  6. linux 安装软件_Linux:其它软件安装方式
  7. DOS命令行数据乱码解决
  8. event.keyCode列表
  9. Python爬虫的基本原理
  10. [Ajax] jQuery中的Ajax -- 04-异步提交表单
  11. 6个技术问题及解决方案
  12. Python面向对象程序设计中属性的作用与用法
  13. C语言函数多个返回值
  14. 02333软件工程_202010_试卷+答案
  15. python中iter_深入了解Python iter() 方法的用法
  16. 关于菜鸡学习时服务器购买的注意点
  17. narx神经网络 matlab,神经网络 NARX
  18. 自制F1C200S demo板(一、电路)
  19. 三维人脸重建 (一)
  20. kmp总结(相关例题1. Simpsons’ Hidden Talents 2.Oulipo)

热门文章

  1. 今天抽点时间来说一个C#里的关键字及它们的原型
  2. 《精通 ASP.NET MVC 5》----1.8 本书所需的软件
  3. 产品观,来自微信张小龙的
  4. 【2008】奥运门票4月15日开始预定 四种购买方式供选
  5. .NET开发系列工具之NDoc:让NDoc支持简体中文!(含修改了的源代码)
  6. Java反射机制和动态代理实例
  7. [笔记] 大家一起来测试,benchmark起来(MySQL下的TPC-C,TPC-H,TPC-W)
  8. I2C死锁原因及解决方法
  9. 华为p40论坛_花粉俱乐部网友:华为P40千万别上双扬声器,小米10方向错了
  10. Microbit-高温报警系统