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

<!-- namespace表示命名空间  保证它是唯一   cn.itsource.mybatis.dao.impl.ProductDaoImpl + id="getUserById"-->
<mapper namespace="_02_highquery.EmployeeMapper">
    <!-- id名称和ProductMapper接口方法一样-->
    <select id="query" resultType="_02_highquery.Employee" parameterType="_02_highquery.EmployeeQuery">
        select * from employee
        <where>
            <include refid="whereSql"></include>
        </where>
    </select>
    <!--抽取sql通过include refid="whereSql"引用-->
    <sql id="whereSql">
        <if test="keywords != null">
            <!-- /* and name like %${keywords}% or password like %${keywords}%*/ -->
            <!-- (1)不行-->
            <!--  and name like %#{keywords}% or password like %#{keywords}% -->
            <!-- (2)可以使用 拼接字符串 存在sql注入-->
            <!-- and name like ‘%${keywords}%‘ or password like ‘%${keywords}%‘ -->
            <!-- (3) concat函数-->
              and name like concat("%",#{keywords},"%")
        </if>
        <if test="minAge != null">
            and age >= #{minAge}
        </if>
        <!-- 转义1)-->

        <!--<if test="maxAge != null">
            and age &lt;= #{maxAge}
        </if>-->
        <!-- 写法(2)CDATA XML-->
        <if test="maxAge != null">
            <![CDATA[
                 and age <= #{maxAge}
              ]]>
        </if>
    </sql>

</mapper>

原文地址:https://www.cnblogs.com/xiaoruirui/p/11776283.html

时间: 2024-08-30 05:33:54

MyBatis高级查询的相关文章

Mybatis 高级查询的小整理

高级查询的整理 // resutlType无法帮助我们自动的去完成映射,所以只有使用resultMap手动的进行映射 resultMap: type 结果集对应的数据类型 id 唯一标识,被引用的时候,进行指定 autoMapping 开启自动映射 extends 继承 子标签: association:配置对一的映射 property 定义对象的属性名 javaType 属性的类型 autoMapping 开启自动映射 collection:配置对多的映射 property 定义对象的属性名

Mybatis高级查询之关联查询

3 关联查询 做查询之前,先修改几个配置.mapper.xml是在mybatis-config.xml中指定,那么我们每增加一个mapper都要增加一个配置,很麻烦.为了简化配置.需要将mapper接口和mapper.xml放到同一个文件下,并且接口和xml文件命名一致.使用mybatis的自动扫描:.这样,当我们新增接口的时候,直接创建接口和对应xml文件就可以了: <mappers> <!--<mapper resource="com.test.mapper.dao/

MyBatis高级查询 一对多映射

数据库表在一对一映射中. 在数据库sys_user_role中新增一条记录 一个用户可以有多个角色.查询出所有用户和所对应的角色. 1.collection集合的嵌套结果映射 <!-- SysUserMapper.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN&q

MyBatis高级查询 存储过程

1.第一个存储过程  根据用户id查询用户其他信息 #第一个存储过程 #根据用户id查询用户其他信息 DROP PROCEDURE IF EXISTS `select_user_by_id`; DELIMITER ;; CREATE PROCEDURE `select_user_by_id` ( IN userId BIGINT, OUT userName VARCHAR (50), OUT userPassword VARCHAR (50), OUT userEmail VARCHAR (50

MyBatis高级查询 一对一映射

drop database if exists simple; create database simple; use simple; drop table if exists sys_user; create table sys_user ( id bigint not null auto_increment comment '用户ID', user_name varchar(50) comment '用户名', user_password varchar(50) comment '密码',

Mybatis学习记录(四)--高级查询和缓存

这些都是连贯的学习笔记,所以有的地方因为之前都说过,所以也就没怎么写详细了,看不太明白的可以看看之前的笔记. 一.高级查询 高级查询主要是一对一查询,一对多查询,多对多查询 1.一对一查询 有用户和订单两个表,用户对订单是1对1查询.也就是订单中有一个外键是指向用户的. 先创建实体类: User.java public class User { private int id; private String username; private String password; private St

MyBatis高级映射查询(3)

一.数据库数据和项目搭建过程 1.主要要四张表,分别为user用户信息表.items商品表.orderdetail订单明细表.orders订单表.表的结构和数据如下: 表结构 CREATE DATABASE mybatis DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci CREATE TABLE `items` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(32) NOT NUL

MyBatis(7)高级查询

高级查询: 对于整体的工程是时候增加一点文件了: 具体用到那个类再去说明类的内容 一对一查询: 1.resultType进行实现: 执行的sql语句: 查询的主表:订单表 查询的关联表:用户表 orders表有一个外键 select orders.*,user.username,user.sex,user.address  from orders ,user where orders.user_id = user.id; ordersCustomer.java public class Orde

MyBatis和elementui中的高级查询和分页

前台 我们需要发送请求和带入参数 有了这两个属性过后需要在data中添加 这个属性是和方法平级的 整个页面 <template> <section> <!--工具条--> <el-col :span="24" class="toolbar" style="padding-bottom: 0px;"> <el-form :inline="true" :model="