微服务和VUE入门教程(11): mybatis 动态查询

微服务和VUE入门教程(11): mybatis 动态查询

微服务和VUE入门教程(0): 着手搭建项目
微服务和VUE入门教程(1): 搭建前端登录界面
微服务和VUE入门教程(2): 注册中心
微服务和VUE入门教程(3): user微服务的搭建
微服务和VUE入门教程(4):网关zuul的搭建
微服务和VUE入门教程(5): 前后端交互
微服务和VUE入门教程(6):连接数据库-mybatis
微服务和VUE入门教程(7):配置中心-config
微服务和VUE入门教程(8):前端主页的编写
微服务和VUE入门教程(9): token验证-token后端生成以及前端获取
微服务和VUE入门教程(10): token验证-前端登录拦截以及token过期提醒
微服务和VUE入门教程(11): mybatis 动态查询
微服务和VUE入门教程(12):前端提示搜索框的实现
微服务和VUE入门教程(13): token验证-zuul拦截与验证
微服务和VUE入门教程(14): 热部署
微服务和VUE入门教程(15): 课堂小知识
微服务和VUE入门教程(16): zuul 熔断
微服务和VUE入门教程(17): VUE 响应拦截器
微服务和VUE入门教程(18): 前端接口模块化
微服务和VUE入门教程(19): VUE组件化–子组件向父组件通信
微服务和VUE入门教程(20): VUE组件化–父组件向子组件通信
微服务和VUE入门教程(21): springboot中定时器-Schedule
微服务和VUE入门教程(22): 页面长时间未操作自动退出登录
微服务和VUE入门教程(23): 微服务之间的调用
微服务和VUE入门教程(24): 微服务之断路器
微服务和VUE入门教程(25): 微服务之Hystrix-dashboard
微服务和VUE入门教程(26): 微服务之turbine
微服务和VUE入门教程(27):VUE前端工程打包

1. 新建class表和student表:

CREATE TABLE `class` (
  `class_id` varchar(20)  NOT NULL,
  `class_name` varchar(50)  DEFAULT NULL,
  `headmaster_name` varchar(20)  DEFAULT NULL,
  `headmaster_tel` varchar(20)  DEFAULT NULL,
  PRIMARY KEY (`class_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
CREATE TABLE `student` (
  `stu_no` varchar(20)  NOT NULL,
  `stu_name` varchar(20) DEFAULT NULL,
  `stu_sex` varchar(5) DEFAULT NULL,
  `stu_address` varchar(200) DEFAULT NULL,
  `stu_tel` varchar(20) DEFAULT NULL,
  `class_id` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`stu_no`),
  FOREIGN KEY (class_id) REFERENCES class(class_id)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

因为student表中class_id是class表中的外键,因此需要先新建class表。在插入数据的时候,同样也是先对class表中进行添加数据。student表中的class_id要在class表中的class_id有记录。

2. 新建student微服务

具体过程参考,user微服务构建过程

其微服务的目录结构如下:
在这里插入图片描述

3. 修改前端页面

第一部分根据班级或者年龄来查询

中间部分有一个添加按钮和一个搜索框,搜索框来实现提示搜索功能

主体部分是是一个表格,表格中可以进行查看,修改,删除

底部是一个分页。

使用的是elementUI,稍微修改一下就ok,很方便。
在这里插入图片描述

4. mybatis的动态查询

在实际的开发当中,经常需要根据不同条件拼接SQL语句。于是动态查询显得尤为重要。

<select id="stuSel" resultMap="BaseStuMap">
    select * from student
    <where>
        <if test="stuName != null and stuName != ''">
            stu_name = #{stuName}
        </if>
        <if test="stuNo != null and stuNo != ''">
            and stu_no = #{stuNo}
        </if>
        <if test="classId != null and classId != ''">
            and class_id = #{classId}
        </if>
        <if test="stuAge1 != null and stuAge1 != '' and stuAge2 != null and stuAge2 != '' ">
            and stu_age between #{stuAge1} and #{stuAge2}
        </if>
        <if test="(stuAge1 != null or stuAge1 != '') and stuAge2 != null and stuAge2 != '' ">
            and stu_age between (select min(stu_age) from student) and #{stuAge2}
        </if>
        <if test="(stuAge1 != null and stuAge1 != '') and (stuAge2 != null or stuAge2 != '') ">
            and stu_age between #{stuAge1} and (select max(stu_age) from student)
        </if>
    </where>
    order by stu_no
    limit #{start}, #{pageSize}
</select>

这是我们查询学生信息的代码,可以看到我们传入了很多查询条件所需要的参数:stuName,stuNo,classId,stuAge1,stuAge2 。

其中之所以传入stuAge1,stuAge2 ,是因为我想要查询某一个年龄段的学生。此外,为了满足分页的需求,我们还传入了start和pageSize。start是在从第start个开始查询,pageSize是指一次查询的个数,也就是一页展示的个数。通过一段代码,基本可以满足我们的查询需求。

此外,为了查到相应的学生数量,我们还需要写一个查询数量的sql语句。

<!--学生数量查询-->
<select id="stuCountSel" resultType="int">
    select count(*) from student
    <where>
        <if test="stuName != null and stuName != ''">
            stu_name = #{stuName}
        </if>
        <if test="stuNo != null and stuNo != ''">
            and stu_no = #{stuNo}
        </if>
        <if test="classId != null and classId != ''">
            and class_id = #{classId}
        </if>
        <if test="stuAge1 != null and stuAge1 != '' and stuAge2 != null and stuAge2 != '' ">
            and stu_age between #{stuAge1} and #{stuAge2}
        </if>
        <if test="(stuAge1 != null or stuAge1 != '') and stuAge2 != null and stuAge2 != '' ">
            and stu_age between (select min(stu_age) from student) and #{stuAge2}
        </if>
        <if test="(stu_age1 != null and stu_age1 != '') and (stuAge2 != null or stuAge2 != '') ">
            and stu_age between #{stuAge1} and (select max(stu_age) from student)
        </if>
    </where>
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值