<?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="cn.xdf.wlyy.jxdtest.dao.RoleDao">

<select id="findById" parameterType="int"
resultType="cn.xdf.wlyy.jxdtest.po.Role">
SELECT * from role where id=#{value}
</select>

<select id="findByName" parameterType="String"
resultType="cn.xdf.wlyy.jxdtest.po.Role">
<!-- 模糊查询的两种方法 -->
<!-- SELECT * from role where name like '%${value}%'; -->
SELECT * from role where name like concat(concat('%',#{value}),'%');

</select>

<delete id="deleteById" parameterType="int">
DELETE FROM role WHERE
id=#{value}
</delete>

<insert id="insertRole" parameterType="cn.xdf.wlyy.jxdtest.po.Role">
INSERT into
role(name,u_id) VALUES(#{name},#{u_id})
</insert>

<update id="updateRole" parameterType="cn.xdf.wlyy.jxdtest.po.Role">
UPDATE role set
name=#{name},u_id=#{u_id} where id =#{id}
</update>

<!--查询所有 resultMap返回结果 -->
<resultMap type="cn.xdf.wlyy.jxdtest.po.Role" id="listRoleMap">
<id property="id" column="id" />
<id property="name" column="name" />
<id property="u_id" column="u_id" />
</resultMap>
<select id="listRole" resultMap="listRoleMap">
select r.id,r.name,r.u_id from
role as r;
</select>
<!-- 查询所有 resultType返回结果 -->
<select id="listRoletype" resultType="cn.xdf.wlyy.jxdtest.po.Role">
select r.id,r.name,r.u_id
from role as r;
</select>

<!-- 多表联查 -->
<!-- 关联所需要的列 笛卡尔积 -->
<resultMap type="cn.xdf.wlyy.jxdtest.po.User" id="getUserMap">
<id property="id" column="u_id" />
<result property="name" column="u_name" />
</resultMap>
<resultMap type="cn.xdf.wlyy.jxdtest.po.Role" id="userRoleMap">
<id property="id" column="r_id" />
<result property="name" column="r_name" />
<association property="user" javaType="cn.xdf.wlyy.jxdtest.po.User"
resultMap="getUserMap" />
</resultMap>
<select id="getUserRole" resultMap="userRoleMap">
SELECT r.id r_id,u.id u_id,
r.name r_name,u.name u_name FROM role as r,user as u where r.u_id=u.id
</select>

<!-- 根据参数联查 -->
<resultMap type="cn.xdf.wlyy.jxdtest.po.Role" id="userRoleMapById">
<id property="id" column="r_id" />
<result property="name" column="r_name" />
<!-- 一对一内嵌如查询 colum指向要映射的一方 -->
<!-- <association property="user" column="u_id" select="getUser"></association> -->
<!-- 映射实体一一对应 列名需通过别名映射 负责为空 -->
<association property="user" javaType="cn.xdf.wlyy.jxdtest.po.User" >
<id property="id" column="u_id" />
<result property="name" column="u_name" />
</association>
<!-- 一对多实现的两种方式 -->
<!-- ofType指定集合中的对象类型 -->
<!-- 不嵌套映射 -->
<!-- <collection property="users" ofType="cn.xdf.wlyy.jxdtest.po.User">
映射中的别名必须写别名 <id property="id" column="u_id"/> <result property="name" column="u_name"/>
</collection> -->

<!--一对多嵌套方式column指向一的一方(唯一键) -->
<collection property="users" ofType="cn.xdf.wlyy.jxdtest.po.User"
column="r_id" select="getUsers"></collection>
<!--一对多实现的两种方式 -->

</resultMap>
<select id="getUserRoleByUid" parameterType="int" resultMap="userRoleMapById">
<!-- 多对一查询 -->
<!-- select r.id r_id ,r.name r_name ,u.id u_id,u.name u_name from role
as r ,user as u where r.u_id=u.id and r.u_id=#{value} -->
<!-- 一对多查询 -->
select r.id r_id ,r.name r_name ,u.id u_id,u.name u_name from role as
r ,user as u where r.id=u.r_id and r.id=#{value}
</select>

<!-- 在做单表查询时候 字段为数据表中原有字段 -多对一 -->
<select id="getUser" parameterType="int" resultType="cn.xdf.wlyy.jxdtest.po.User">
select id
,name from user where id=#{value}
</select>
<!-- 一对多查询 -->
<select id="getUsers" parameterType="int"
resultType="cn.xdf.wlyy.jxdtest.po.User">
select id ,name from user where r_id=#{value}
</select>

</mapper>

mybatis基本查询的更多相关文章

  1. Mybatis关联查询和数据库不一致问题分析与解决

    Mybatis关联查询和数据库不一致问题分析与解决 本文的前提是,确定sql语句没有问题,确定在数据库中使用sql和项目中结果不一致. 在使用SpringMVC+Mybatis做多表关联时候,发现也不 ...

  2. myBatis批量查询操作,xml中使用foreach案例

    使用场景:有一个订单表,实体类为OrderBase.java,订单有个状态为status值可能为"1,2,3,4,5,6",现在需要查询状态为"2,3,4"的订 ...

  3. MyBatis关联查询 (association) 时遇到的某些问题/mybatis映射

    先说下问题产生的背景: 最近在做一个用到MyBatis的项目,其中有个业务涉及到关联查询,我是将两个查询分开来写的,即嵌套查询,个人感觉这样更方便重用: 关联的查询使用到了动态sql,在执行查询时就出 ...

  4. MyBatis基础:MyBatis关联查询(4)

    1. MyBatis关联查询简介 MyBatis中级联分为3中:association.collection及discriminator. ◊ association:一对一关联 ◊ collecti ...

  5. MyBatis关联查询,一对多关联查询

    实体关系图,一个国家对应多个城市 一对多关联查询可用三种方式实现: 单步查询,利用collection标签为级联属性赋值: 分步查询: 利用association标签进行分步查询: 利用collect ...

  6. Ibatis/Mybatis模糊查询

    Ibatis/Mybatis模糊查询 根据网络内容整理 Ibatis中 使用$代替#.此种方法就是去掉了类型检查,使用字符串连接,不过可能会有sql注入风险. Sql代码 select * from ...

  7. MyBatis一对一查询

    ---------------------siwuxie095                                 MyBatis 一对一查询         以订单和用户为例,即 相对订 ...

  8. MyBatis高级查询

    -------------------------siwuxie095 MyBatis 高级查询 1.MyBatis 作为一个 ORM 框架,也对 SQL 的高级查询做了支持, MyBatis 高级查 ...

  9. 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案

    1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...

  10. 在数据库中添加数据以后,使用Mybatis进行查询结果为空

    在数据库中添加数据以后,使用Mybatis进行查询结果为空,这是因为数据库中添加数据忘记commit的缘故.

随机推荐

  1. NIO编程模式示例

    1. 服务端 import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.*; im ...

  2. Flask路由之重定向

    Flask框架提供了请求重定向功能,只需要使用 redirect_to即可, 示例代码如下: from flask import Flask, render_template, request, re ...

  3. php红包功能

    最近公司要开发 广告红包功能,这是写好的代码先放到这 https://files.cnblogs.com/files/jxkshu/PHP%E5%B9%BF%E5%91%8A%E7%BA%A2%E5% ...

  4. leetcode 235. 二叉搜索树的最近公共祖先(c++)

    给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...

  5. MongoDB分片配置 优化 不错

    简单注解:mongos 路由进程, 应用程序接入mongos再查询到具体分片,监听端口默认27017config server 路由表服务, 每一台都具有全部chunk的路由信息 shard为数据存储 ...

  6. 身份证验证的js

    function isIdCardNo(num) { num = num.toUpperCase(); //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能 ...

  7. C++学习笔记(二)--基础

    1.浮点型数值不管写成什么样 都是以指数形式保存在内存中 数符|数字部分|指数部分 例:+0.33E10 数字部分的整数部分不能大于1,小数点后面不能是0. 2.字符数据是以整数形式保存在内存中的(A ...

  8. Netty之SubPage级别的内存分配

    SubPage 级别的内存分配: 通过之前的学习我们知道, 如果我们分配一个缓冲区大小远小于page, 则直接在一个page 上进行分配则会造成内存浪费, 所以需要将page 继续进行切分成多个子块进 ...

  9. luoguP1966 火柴排队(NOIP2013)(归并排序)

    luogu P1966 火柴排队 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include ...

  10. Cannot modify header information - headers already sent by出错的原因

    <?php ob_start(); setcookie("username","送家",time()+3600); echo "the user ...