DAY04

  1. where就是我们以前使用where 关键字,自动去除多余逻辑运算符
<where>
</where>
  1. if条件成立,执行if块中的内容
<if test=”属性名!=null and 属性名!=’’ ”>
	and 列名 关系运算符 #{属性名}
</if>
<if test=”属性名=null or 属性名=’’ ”>
	and 列名 关系运算符 #{属性名}
</if>
  1. choose多选一的情况
  2. foreach常用的属性
  • index:表示下标
  • collection:表示遍历的类型 array、list
  • item:表示每次获取的数据
  • separate:分隔符,每次获取数据后添加的符号
  • open:生成sql语句以什么开头
  • close:生成sql语句以什么结尾
  1. set用于修改操作,就是 set关键字

Mybatis中的关联映射查询

  • 通过表链接查询的数据如何使用po类进行存放;使用表链接查询获取的数据肯定来自于多张表,一张表对应一个po

不管数据来自几张表,最终一行数据只能对应一个po对象

  • 思考:
  • 如何把部门表中的数据使用员工类的对象存放
  • 如果我们能把部门类的对象放入到员工类中
public class Dept{
    //存放部门表中的数据
    
}
//存放员工表中的数据
public class Emp{
    private Dept dept;
}
  • 在映射文件中进行配置,指明列与属性的对应关系
  • 如何编写,依据是表与表的主外键关系;在从表对应的类中创建主表对应类的对象即可

关联映射的使用

  1. 编写表连接的sql语句
select
	e.ename, d.dname, s.grade
from emp e, dept d, salgrade s
	where e.deptno = d.deptno
		and e.sal between s.losal and s.hisal
		<if test="loSal != null and loSal != ''">
			and losal >= #{loSal}
		</if>
		<if test="hiSal != null and hiSal != ''">
			and hisal <= #{hiSal}
		</if>
  1. javapo类中添加关联表映射类的对象
  2. 编写映射关联,结果集中的列与类中属性的对应关系
<!-- 配置关联关系 -->
    <resultMap id="empMap" type="com.upc.mybatis_01.po.Emp">
        <id column="empno" property="empno"/>
        <result column="ename" property="ename"/>
        <result column="job" property="job"/>
        <result column="mgr" property="mgr"/>
        <result column="hiredate" property="hiredate"/>
        <result column="sal" property="sal"/>
        <result column="comm" property="comm"/>
        <!-- 其他类中的属性和列以此匹配 -->
        <association property="dept"
                     javaType="com.upc.mybatis_01.po.Dept">
            <id column="deptno" property="deptno"/>
            <result column="dname" property="dname"/>
            <result column="loc" property="loc"/>
        </association>
        <association property="grade"
                     javaType="com.upc.mybatis_01.po.Salgrade">
            <id column="grade" property="grade"/>
            <result column="losal" property="losal"/>
            <result column="hisal" property="hisal"/>
        </association>
    </resultMap>

type:表示结果集中一行数据最终构建的对象

association:表示关联的对象配置信息

  1. select节点中使用resultMap属性关联我们定义的resultMap映射

分页:分页插件

  1. pom文件中添加插件的依赖
  2. java的业务逻辑中添加分页操作
PageInfo.startPage(页数,每页的条数);
  1. 如果需要返回分页的详细信息:总页数、当前页数、总的记录数等,需要进一步设置
    需要使用PageInfo对象对查询的数据进行二次封装
List<Emp> emps = managerMapper.findAll();
//根据查询的数据获取总页数、总条数等
PageInfo<Emp> page = new PageInfo<>(emps);
System.out.println(page);

java 有where吗 javaweb中select语句_java

  1. 同步完成前后端分离的demo

需要开发web项目

  1. 以前使用的技术
  • jsp
  • servlet
  1. Spring mvc
  • 主要功能实现控制器的操作
  • 把页面提交的数据自动封装为对象
  • 把我们后端查询的数据自动转为json对象
  1. 掌握
  • 如何编写控制器
  • 在控制器中编写处理客户端请求的方法
  • 如何启动springbootweb程序
  • 如何访问
  1. 步骤
  1. 创建使用springboot的项目
  2. 配置服务器的相关参数
  • 端口 8080
  • 项目的目录 默认 /
    如果默认配置,服务器启动后
    http://localhost:8080
  1. 创建控制器,接收客户端的请求并且把处理的结果响应给客户端
  • 创建一个普通的类
  • 添加注解**@RestController**
  • 在类中添加方法,返回类型任意,根据需要设置相应的参数
  • 在方法上添加**@RequestMapping**注解,设置对应的访问路径
  • 在方法中实现功能