动态SQL语句标签
1.if 用于根据条件判断是否包括某段 SQL 代码
<if test="checktext !=null and check !=''">
2.<choose>
, <when>
, <otherwise>
类似于 Java 的 switch
语句,用于在多个条件中选择一个。
<select id="getStaffBySalary" resultType="com.easy.bean.Staff">
select * from staff
<where>
<!--参数名 salarytext -->
<choose>
<when test='salarytext=="低"'>
salary <= 5000
</when>
<when test='salarytext=="中"'>
salary >5000 and salary <=10000
</when>
<otherwise>
salary >10000
</otherwise>
</choose>
</where>
</select>
3.<foreach>
用于遍历集合并生成多个 SQL 片段。
<insert id="addList">
insert into staff(code,name,salary,username,userpass)
values
<foreach collection ="list" item="item" separator=",">
(#{item.code},#{item.name},#{item.salary},#{item.username},#{item.userpass})
</foreach>
</insert>
4.<set>
用于在更新 SQL 中动态生成 SET
子句。
<set>
<if test='name!=null and name!=""'>
name =#{name},
</if>
<if test="salary!=null">
salary=#{salary},
</if>
</set>
5.<bind>
标签用于创建新的属性,并将其与现有的参数值关联。
<!-- 重新定义参数内容-->
<bind name="liketext" value="'%'+checktext+'%'"></bind>
6.<where>
标签用于自动处理 SQL 查询中的 WHERE
子句。这对于动态生成 WHERE
子句特别有用,尤其是当你需要处理条件可能为空的复杂查询时。<where>
标签可以自动添加 WHERE
关键字,并处理不必要的 AND
或 OR
连接符,从而简化 SQL 查询的编写。
<select id="getStaff" resultType="com.easy.bean.Staff">
select * from staff
<where>
<!--编写条件语句 如果有内容,会自动添加where关键字 -->
<if test="checktext !=null and check !=''">
<!-- 重新定义参数内容-->
<bind name="liketext" value="'%'+checktext+'%'"></bind>
name like #{liketext}
</if>
</where>
</select>
一对一查询和一对多查询 resultMap
1.在一对一映射中,通常有两个表(例如 users
和 user_profiles
),每个表中的一行数据与另一个表中的一行数据相关联。需要使用 resultMap
的 association
元素来实现这个映射关系。
<resultMap id="staffAndDep" type="com.easy.bean.Staff">
<association select="getStaffDep" column="dept_id" property="dep"></association>
</resultMap>
<select id="getStaffDep" resultType="com.easy.bean.Department">
select * from department where id = #{dept_id}
</select>
<select id="getStaffAndDep" resultMap="staffAndDep">
select * from staff
</select>
上述代码使用resultMap通过Staff表中的dept_id和Department表中的id关联两表。
2.在一对多映射中,一个表中的一行数据与另一个表中的多行数据相关联。例如,一个用户可以有多个订单。可以使用 resultMap
的 collection
元素来实现这个映射关系。
<resultMap id="departmentAndStaff" type="com.easy.bean.Department">
<!-- <id column="id" property="depid"></id>-->
<!-- <result column="name" property="dename"></result>-->
<result column="id" property="id"></result>
<collection column="id" select="getDepStaff" property="staffList"></collection>
</resultMap>
<select id="getDepStaff" resultType="com.easy.bean.Staff">
select * from staff where dept_id=#{id}
</select>
<select id="getDep" resultMap="departmentAndStaff">
select * from department
</select>
题目整理
SQL执行的顺序是:
1. from
2. join
3. where
4. group by
5. 聚合函数 COUNT(),SUM(),AVG(),MIN(),MAX()
6. having
7. select
8. distinct
9. order by | asc, desc, 默认asc
10. limit n, m(n下标0开始,取m条数据)
between…and…语句用于查询数据范围,遵循全闭合的原则,包括左侧和右侧数据。
order by在排序时,可以使用字段名,也可以使用字段下标,下标从1开始。
Servlet生命周期方法有: init,service,destroy
JSP中的九大内置对象有:request,response,session,application,page,pageContext,config,out,exception
delete与truncate的区别?
1- 功能不同:delete用于删除表中的记录,truncate删除表再重建
2- 影响自增:delete自增不会重置,truncate会重置
3- 事务支持:delete可以回滚,truncate不能回滚
4- 效率:delete慢,truncate快
请求转发与重定向的区别?
1- 发生位置:请求转发发生在服务器内部,是服务器行为;重定向是服务器指挥浏览器,发生在浏览器中。
2- 请求次数:请求转发只有一次请求;重定向要发送两次请求。
3- 浏览器地址:请求转发浏览器地址不变;重定向浏览器会指向重定向后的地址。
4- 请求对象:请求转发多页面共享一对request和response,可以使用request共享数据;重定向每次请求创建不同的request和response,不能使用request共享数据。
5- 范围:请求转发只能在当前项目内进行跳转;重定向可以跨站跳转
Get与Post请求的区别?
1)Get请求参数会使用?和&拼接到url上。Post请求参数放在请求体的Form Data数据域中。
2) Get请求参数必须是字符串。Post请求参数除了可以传递字符串类型的,也可以传递二进制。
3) Get请求参数的长度会受到url长度的限制。Post请求参数长度不会受到客户端浏览器的限制,只要服务器允许。
4) Get安全性低。Post相对安全。