zoukankan      html  css  js  c++  java
  • mybatis返回map操作

    <?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);
    }

  • 相关阅读:
    python的多线程问题
    python 调用函数时使用星号 *, **
    python正则中的贪婪与非贪婪
    python中文处理
    Python 模块之 ConfigParser: 用 Python 解析配置文件
    substr使用注意
    [转]互联网后台服务的协议设计
    Java设计模式从精通到入门四 工厂方法模式
    logback中logger详解
    logback实践笔记
  • 原文地址:https://www.cnblogs.com/laiyongqing/p/10188057.html
Copyright © 2011-2022 走看看