当前位置: 首页 > news >正文

南京做网站建设的公司哪家好百度搜索热度查询

南京做网站建设的公司哪家好,百度搜索热度查询,越秀重点场所,高校网站如何建设论文简单介绍: 在我们之前使用where关键字进行查询的时候,总是会在后面添加一个11恒等式,并且在每一个可能拼接的SQL语句前面都加上一个and关键字,防止当后续的所有条件都不满足的时候,where关键字在最后直接跟and的时候也…

简单介绍:

在我们之前使用where关键字进行查询的时候,总是会在后面添加一个1=1恒等式,并且在每一个可能拼接的SQL语句前面都加上一个and关键字,防止当后续的所有条件都不满足的时候,where关键字在最后直接跟and的时候也能不报错。在本章节的学习中,我么将要学习一个新的标签<where>可以帮助我们在我们拼接SQL语句的时候i,灵活的添加或者不添加where关键字。

使用方法:

<select id="唯一标识" resultType="结果集映射的实体类">

        select * from student

        <where>

                <if test="判断条件">

                        and 需要拼接的SQL语句

                </if>

                <if test="判断条件">

                        and 需要拼接的SQL语句

                </if>

        </where>

</select>

代码实现:

SQL映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mappers.dynamicSql"><select id="selectByIdOrName" parameterType="student" resultType="student">select * from student where 1=1
#                             当id的值不等于null并且id的值不是空字符的时候,就会拼接这个SQL语句<if test="id != null and id != ''">and id = #{id}</if>
#                             当name的值不等于null的时候并且name的值不是空字符串的时候,就会拼接这个SQL语句<if test="name != null and name != ''">
#                             注意这个地方是使用了一个concat函数将模糊匹配的百分号和参数进行拼接,在使用的时候注意这个地方不要写错and name like concat ('%',#{name},'%')</if></select><select id="selectAll" resultType="student">select * from student;</select>
<!--    动态SQL中的choose元素-->
<!--    当查询的条件满足第一个when的时候,就拼接第一个when里面的SQL语句-->
<!--    当查询的条件满足第二个when的时候,就拼接第二个when里面的SQL语句-->
<!--    当所有的when都不满足的时候,就拼接otherwise里面的SQL语句-->
<!--    当有多个when里面的条件都满足的时候,就拼接最靠上的一条SQL语句,并且不会执行其他when里面的语句--><select id="selectStudentByIdAndName" resultType="student" >select * from student where 1=1<choose><when test="name != null and name != ''">and name like concat('%',#{name},'%')</when><when test="id != null and id != ''">and id = #{id}</when><otherwise>and password is not null</otherwise></choose></select>
<!--    使用<where>来动态的处理where关键字是否添加--><select id="selectByIdAndWhere" resultType="student">select * from student<where><if test="name != null and name !=''">and name like concat('%',#{name},'%')</if><if test="id != null and id !=''">and id = #{id}</if></where></select>
</mapper>

接口文件:

package Mappers;import com.mybatis.POJO.student;import java.util.List;public interface dynamicSql {List<student> selectByIdOrName(student s);List<student> selectStudentByIdAndName(student s);List<student> selectByIdAndWhere(student s);
}

测试类:

package Mappers;import com.mybatis.POJO.Tools.createSqlSession;
import com.mybatis.POJO.student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.List;public class dynamicSqlTest {@Testpublic void selectByIdOrName(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();s.setId(1);s.setName("张三");List<student> stu = sqlSession.selectList("Mappers.dynamicSql.selectByIdOrName", s);for(student student : stu){System.out.println(student.toString());}}@Testpublic void selectStudentByIdAndName(){dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s =new student();
//        s.setId(1);
//        s.setName("张三");for (student student : dynamicSql.selectStudentByIdAndName(s)) {System.out.println(student);}}@Testpublic void selectAll(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();List<student> list = sqlSession.selectList("Mappers.dynamicSql.selectAll");for(student student : list){System.out.println(student.toString());}}@Testpublic void selectByIdAndWhere(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();
//        s.setId(1);s.setName("张三");for (student student : dynamicSql.selectByIdAndWhere(s)) {System.out.println(student.toString());}}
}

运行结果:

当我们使用where标签的时候,即使SQL语句中没有where关键字,MyBatis也会自动的帮助我们进行添加,同样的,如果我们的where关键字后面没有任何的查询语句,MyBatis会帮我们删除多余的where关键字。

当我们正常进行查询的时候:

可以看到我们的查询语句后面是没有where关键字的,接着我们来运行程序:

 

可以看到是可以正常的查询的,说明MyBatis在我们的查询语句后面插入了一个where关键字帮我们构建出了一个完整的SQL语句

如果我们if条件都达不到的时候,也就是说where后面没有任何需要拼接的时候,where还会不会自动添加?:

运行结果:

 

结果依然正常的显示了,说明where关键字并没有添加,这就证明了<where>标签可以动态的自动识别是否应该添加where关键字。 

注意点:

在这个案例中,需要注意的就是<where>标签的位置和<select>的嵌套关系,以及在注意在拼接SQL关键字的时候需要遗漏and关键字。如果出现了运行报错的情况,优先考虑是否是<where>的位置和SQL拼接的问题。

如果出现了查询结果不符合猜想的情况优先考虑是否是因为在SQL拼接的时候遗漏了某些关键字或者是条件判断的错误

http://www.dinnco.com/news/5555.html

相关文章:

  • 荆州公司做网站百度seo排名优化联系方式
  • 响应式网站建设服务提供商网上推广平台有哪些
  • 回民区建设局网站互动营销案例100
  • 成都保洁公司某个网站seo分析实例
  • 郑州市东区建设环保局官方网站爱站关键词搜索
  • 扬州市建设厅网站刷粉网站推广便宜
  • 沈阳市官网信息流优化师是干什么的
  • 做教育的网站网络推广培训课程内容
  • 网站建设与管理计划书南宁seo内部优化
  • 做热血钓鱼网站竞价账户托管的公司有哪些
  • 武汉建设厅官网seo优化分析
  • 网站运营数据周报表怎么做百度地图人工客服电话
  • asp.net做购物网站黑龙江新闻头条最新消息
  • 宁波英文网站建设珠海百度关键字优化
  • 网站建设的概念郴州网站推广
  • 台州网站建设解决方案站长工具seo综合查询引流
  • 撤销网站备案表填写后网站建设的流程是什么
  • 西安淘宝网站建设公司全国疫情又严重了
  • 银行门户网站是什么意思长沙网站包年优化
  • 网站后台域名百度竞价返点一般多少
  • 网站建立的百度招商客服电话
  • 深圳企业网站建设公司头条新闻最新消息
  • 网站如何做滚动效果图搜索引擎优化的核心是
  • 专业优定软件网站建设可以直接进入的舆情网站
  • 临朐网站建设哪家好bt磁力猪
  • 网站开发财务费用谷歌搜索优化
  • 南京医院网站建设方案简述seo的优化流程
  • 天地做网站免费培训机构管理系统
  • app开发框架微博seo排名优化
  • 这么做网站原型图品牌营销策划