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;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
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;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

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

2. 新建student微服务

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

其微服务的目录结构如下:

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

3. 修改前端页面

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

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

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

底部是一个分页。

使用的是elementUI,稍微修改一下就ok,很方便。

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

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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

这是我们查询学生信息的代码,可以看到我们传入了很多查询条件所需要的参数: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>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.