ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

mapper.xml

2019-07-12 09:42:53  阅读:135  来源: 互联网

标签:xml mapper sex username user address id SELECT


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN">

-<mapper namespace="com.gyf.mapper.UserMapper">

<!-- ===================第一天内容=======================-->


<insert parameterType="user" id="save">INSERT INTO user (username,sex,birthday,address)VALUE (#{username},#{sex},#{birthday},#{address}) </insert>

<select parameterType="int" id="findUserById" resultType="user">SELECT * FROM user WHERE id = #{id} </select>

<!--通过包装类查询用户-->


<select parameterType="userQueryVO" id="findUserByUserQueryVo" resultType="user">SELECT u.* FROM user u WHERE u.id = #{user.id} </select>

<!--通过Map查询数据-->


<select parameterType="hashmap" id="findUserByMap" resultType="user">SELECT u.* FROM user u WHERE username LIKE '%${username}%' AND sex = #{sex} </select>

<!-- ==================第二天内容======================-->


<!--1 设置返回数据为基本类型,int,double,long,string....-->


<!-- 查询用户的个数 -->


<select parameterType="userQueryVO" id="findUserCount" resultType="int">SELECT COUNT(*) FROM user WHERE sex = #{user.sex} </select>

<!--2.设置返回数据为resultMap -->



-<resultMap id="userResultMap" type="user">

<id column="id_" property="id"/>

<result column="username_" property="username"/>

<result column="sex_" property="sex"/>

<result column="birthday_" property="birthday"/>

<result column="address_" property="address"/>

</resultMap>

<select parameterType="int" id="findUserByIdResultMap" resultMap="userResultMap">SELECTid id_,username username_,sex sex_,birthday birthday_,address address_FROM user WHERE id = #{id} </select>

<!-- 3.if和where的使用-->



-<sql id="select_user_where">


-<if test="user != null">

<if test="user.sex != null and user.sex != ''">sex = #{user.sex} </if>

<if test="user.username != null and user.username != ''">and username LIKE '%${user.username}%' </if>

<if test="user.address != null and user.address != ''">and address LIKE '%${user.address}%' </if>

</if>

</sql>


-<select parameterType="userQueryVO" id="findUserList" resultType="user">
/*性别和名字*/ SELECT * FROM user 

-<where>

<include refid="select_user_where"/>

</where>

</select>

<!-- 4.foreac使用讲解-->



-<select parameterType="userQueryVO" id="findUserByIds" resultType="user">

<!--性别和名字 SELECT * FROM user WHERE id in (1,2,3) -->

SELECT * FROM user 

-<where>


-<if test="ids != null and ids.size > 0">

<!--collection:集合,写集合属性 item:遍历接收变量 open:遍历开始 close:遍历结束 separator:拼接格式 for(Integer id : ids){} -->


<foreach separator="," close=")" open="id in(" item="id" collection="ids">${id} </foreach>

</if>

</where>

</select>

<!-- 5.参数是数组如果参数是数组的话,parameterType可以写全名【java.util.List】,也可以写别名遍历或者判断的时候,都用list变量 -->



-<select parameterType="list" id="findUserByIds2" resultType="user">

<!--性别和名字 SELECT * FROM user WHERE id in (1,2,3) -->

SELECT * FROM user 

-<where>


-<if test="list != null and list.size > 0">

<foreach separator="," close=")" open="id in(" item="id" collection="list">${id} </foreach>

</if>

</where>

</select>

<!-- ==============查询用户信息及用户购买的商品信息============-->



-<resultMap id="userRslMap" type="user">

<!-- 1.匹配user属性 -->


<id column="id" property="id"/>

<result column="username" property="username"/>

<result column="password" property="password"/>

<!--2.匹配user的orderList-->



-<collection property="orderList" ofType="orders">

<id column="order_id" property="id"/>

<result column="number" property="number"/>

<result column="createtime" property="createtime"/>

<result column="note" property="note"/>

<!-- 3.匹配Orders里有orderDetails-->



-<collection property="orderDetails" ofType="orderDetail">

<id column="detail_id" property="id"/>

<result column="items_id" property="itemsId"/>

<result column="items_num" property="itemsNum"/>

<!-- 4.配置定单详情的商品信息-->



-<association property="items" javaType="items">

<id column="items_id" property="id"/>

<result column="name" property="name"/>

<result column="price" property="price"/>

<result column="detail" property="detail"/>

</association>

</collection>

</collection>

</resultMap>

<select id="findUserAndOrderInfo" resultMap="userRslMap">SELECTu.id,u.username,u.address,o.id order_id,o.number,o.createtime,o.note,od.id detail_id,od.items_id,od.items_num,it.name,it.price,it.detailFROMuser u,orders o,orderdetail od,items itWHEREo.user_id = u.idAND o.id = od.orders_idAND od.items_id = it.id </select>

</mapper>

标签:xml,mapper,sex,username,user,address,id,SELECT
来源: https://blog.csdn.net/Hello_MAOSONG/article/details/95589275

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有