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

给几个能看的网站 贴吧/品牌广告图片

给几个能看的网站 贴吧,品牌广告图片,网页编辑的软件,动漫人物做羞羞事的网站狂神说MyBatis系列连载课程,通俗易懂,基于MyBatis3.5.2版本,欢迎各位狂粉转发关注学习,视频同步文档。未经作者授权,禁止转载动态SQL介绍什么是动态SQL:动态SQL指的是根据不同的查询条件 , 生成不同的Sql语…

狂神说MyBatis系列连载课程,通俗易懂,基于MyBatis3.5.2版本,欢迎各位狂粉转发关注学习,视频同步文档。未经作者授权,禁止转载

97dada315715f9c06c214320fc115c42.gif

动态SQL

介绍

什么是动态SQL:动态SQL指的是根据不同的查询条件 , 生成不同的Sql语句.

官网描述:MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。    -------------------------------    - if    - choose (when, otherwise)    - trim (where, set)    - foreach    -------------------------------

我们之前写的 SQL 语句都比较简单,如果有比较复杂的业务,我们需要写复杂的 SQL 语句,往往需要拼接,而拼接 SQL ,稍微不注意,由于引号,空格等缺失可能都会导致错误。

那么怎么去解决这个问题呢?这就要使用 mybatis 动态SQL,通过 if, choose, when, otherwise, trim, where, set, foreach等标签,可组合成非常灵活的SQL语句,从而在提高 SQL 语句的准确性的同时,也大大提高了开发人员的效率。

搭建环境

新建一个数据库表:blog

字段:id,title,author,create_time,views

CREATE TABLE `blog` (  `id` varchar(50) NOT NULL COMMENT '博客id',  `title` varchar(100) NOT NULL COMMENT '博客标题',  `author` varchar(30) NOT NULL COMMENT '博客作者',  `create_time` datetime NOT NULL COMMENT '创建时间',  `views` int(30) NOT NULL COMMENT '浏览量') ENGINE=InnoDB DEFAULT CHARSET=utf8

1、创建Mybatis基础工程

36c2483a6c76452eb15b81618778e4ba.png

2、IDutil工具类

public class IDUtil {    public static String genId(){        return UUID.randomUUID().toString().replaceAll("-","");    }}

3、实体类编写  【注意set方法作用】

import java.util.Date;public class Blog {    private String id;    private String title;    private String author;    private Date createTime;    private int views;    //set,get....}

4、编写Mapper接口及xml文件

public interface BlogMapper {}
<?xml  version="1.0" encoding="UTF-8" ?>/span>        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.kuang.mapper.BlogMapper">mapper>

5、mybatis核心配置文件,下划线驼峰自动转换

<settings>    <setting name="mapUnderscoreToCamelCase" value="true"/>    <setting name="logImpl" value="STDOUT_LOGGING"/>settings><mappers>  <mapper resource="mapper/BlogMapper.xml"/>mappers>

6、插入初始数据

编写接口

//新增一个博客int addBlog(Blog blog);

sql配置文件

<insert id="addBlog" parameterType="blog">    insert into blog (id, title, author, create_time, views)    values (#{id},#{title},#{author},#{createTime},#{views});insert>

初始化博客方法

@Testpublic void addInitBlog(){    SqlSession session = MybatisUtils.getSession();    BlogMapper mapper = session.getMapper(BlogMapper.class);    Blog blog = new Blog();    blog.setId(IDUtil.genId());    blog.setTitle("Mybatis如此简单");    blog.setAuthor("狂神说");    blog.setCreateTime(new Date());    blog.setViews(9999);    mapper.addBlog(blog);    blog.setId(IDUtil.genId());    blog.setTitle("Java如此简单");    mapper.addBlog(blog);    blog.setId(IDUtil.genId());    blog.setTitle("Spring如此简单");    mapper.addBlog(blog);    blog.setId(IDUtil.genId());    blog.setTitle("微服务如此简单");    mapper.addBlog(blog);    session.close();}

初始化数据完毕!

if 语句

需求:根据作者名字和博客名字来查询博客!如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询

1、编写接口类

//需求1List<Blog> queryBlogIf(Map map);

2、编写SQL语句

<select id="queryBlogIf" parameterType="map" resultType="blog">    select * from blog where    <if test="title != null">        title = #{title}    if>    <if test="author != null">        and author = #{author}    if>select>

3、测试

@Testpublic void testQueryBlogIf(){    SqlSession session = MybatisUtils.getSession();    BlogMapper mapper = session.getMapper(BlogMapper.class);    HashMap<String, String> map = new HashMap<String, String>();    map.put("title","Mybatis如此简单");    map.put("author","狂神说");    List<Blog> blogs = mapper.queryBlogIf(map);    System.out.println(blogs);    session.close();}

这样写我们可以看到,如果 author 等于 null,那么查询语句为 select * from user where title=#{title},但是如果title为空呢?那么查询语句为 select * from user where and author=#{author},这是错误的 SQL 语句,如何解决呢?请看下面的 where 语句!

Where

修改上面的SQL语句;

<select id="queryBlogIf" parameterType="map" resultType="blog">    select * from blog    <where>        <if test="title != null">            title = #{title}        if>        <if test="author != null">            and author = #{author}        if>    where>select>

这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

Set

同理,上面的对于查询 SQL 语句包含 where 关键字,如果在进行更新操作的时候,含有 set 关键词,我们怎么处理呢?

1、编写接口方法

int updateBlog(Map map);

2、sql配置文件

<update id="updateBlog" parameterType="map">    update blog      <set>          <if test="title != null">              title = #{title},          if>          <if test="author != null">              author = #{author}          if>      set>    where id = #{id};update>

3、测试

@Testpublic void testUpdateBlog(){    SqlSession session = MybatisUtils.getSession();    BlogMapper mapper = session.getMapper(BlogMapper.class);    HashMap<String, String> map = new HashMap<String, String>();    map.put("title","动态SQL");    map.put("author","秦疆");    map.put("id","9d6a763f5e1347cebda43e2a32687a77");    mapper.updateBlog(map);    session.close();}

choose语句

有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句

1、编写接口方法

List<Blog> queryBlogChoose(Map map);

2、sql配置文件

<select id="queryBlogChoose" parameterType="map" resultType="blog">    select * from blog    <where>        <choose>            <when test="title != null">                 title = #{title}            when>            <when test="author != null">                and author = #{author}            when>            <otherwise>                and views = #{views}            otherwise>        choose>    where>select>

3、测试类

@Testpublic void testQueryBlogChoose(){    SqlSession session = MybatisUtils.getSession();    BlogMapper mapper = session.getMapper(BlogMapper.class);    HashMap<String, Object> map = new HashMap<String, Object>();    map.put("title","Java如此简单");    map.put("author","狂神说");    map.put("views",9999);    List<Blog> blogs = mapper.queryBlogChoose(map);    System.out.println(blogs);    session.close();}

SQL片段

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

提取SQL片段:

<sql id="if-title-author">    <if test="title != null">        title = #{title}    if>    <if test="author != null">        and author = #{author}    if>sql>

引用SQL片段:

<select id="queryBlogIf" parameterType="map" resultType="blog">    select * from blog    <where>                <include refid="if-title-author">include>            where>select>

注意:

①、最好基于 单表来定义 sql 片段,提高片段的可重用性

②、在 sql 片段中不要包括 where

Foreach

将数据库中前三个数据的id修改为1,2,3;

需求:我们需要查询 blog 表中 id 分别为1,2,3的博客信息

1、编写接口

List<Blog> queryBlogForeach(Map map);

2、编写SQL语句

<select id="queryBlogForeach" parameterType="map" resultType="blog">    select * from blog    <where>                <foreach collection="ids"  item="id" open="and (" close=")" separator="or">            id=#{id}        foreach>    where>select>

3、测试

@Testpublic void testQueryBlogForeach(){    SqlSession session = MybatisUtils.getSession();    BlogMapper mapper = session.getMapper(BlogMapper.class);    HashMap map = new HashMap();    List<Integer> ids = new ArrayList<Integer>();    ids.add(1);    ids.add(2);    ids.add(3);    map.put("ids",ids);    List<Blog> blogs = mapper.queryBlogForeach(map);    System.out.println(blogs);    session.close();}

小结:其实动态 sql 语句的编写往往就是一个拼接的问题,为了保证拼接准确,我们最好首先要写原生的 sql 语句出来,然后在通过 mybatis 动态sql 对照着改,防止出错。多在实践中使用才是熟练掌握它的技巧。

动态SQL在开发中大量的使用,一定要熟练掌握!

42074fd0820547d693f85834298c879e.png

end

9fa4a8f3fd78881e5a26dca5b268a1f0.png

视频同步更新,这次一定!

c5310e5d9614fe5961829ba227021c36.png

6514d052223c3fb43544d78e9e8a4f50.png

http://www.lbrq.cn/news/1535941.html

相关文章:

  • 自己电脑做网站 带宽/市场推广怎么做
  • 合肥建设学校网站首页/seo网站有哪些
  • 中国建设银行招标网站/苏州seo网站系统
  • 西安三网合一网站建设/营销方式方案案例
  • 网站排名seo软件/邯郸网站seo
  • 怎么做免费的宣传网站/网络广告的形式有哪些
  • 用asp做网站span/游戏搜索风云榜
  • 手游网站怎么做/seo先上排名后收费
  • 网站一般用什么软件做的/全网营销推广靠谱吗
  • wordpress搭建企业网站/google play三件套
  • 外贸功能网站建设/百度推广业务员电话
  • 生产型或服务型企业网站有哪些/aso推广优化
  • 17网站一起做网店靠谱/网站建设报价单
  • wordpress百度站内搜索/链接交换
  • 民治做网站哪家便宜/报个计算机培训班多少钱
  • 自己做的网站如何兼容ie11/链接网
  • 宜兴城乡建设局网站/重庆seo什么意思
  • 做百度推广一定要有自已网站/seo搜索引擎优化排名报价
  • 镇江优化九一/百度优化培训
  • 网站建设用图/优质的seo快速排名优化
  • 江山网站设计/品牌营销做得好的品牌有哪些
  • 手机wap网站怎么做/免费推广网站入口
  • 给政府做网站报价/深圳市seo上词贵不贵
  • 房建设计图网站/流氓网站
  • 公司网站建设外包/东莞优化网站制作
  • 网站建设美化/百度网站怎么优化排名
  • wordpress 3.8.3中文版/搜索引擎优化的英文缩写是什么
  • 电视剧男女直接做视频网站/磁力多多
  • 微山网站建设多少钱/百度竞价查询
  • 有没有可以直接看的/seo价格查询公司
  • Django ModelForm
  • C++入门自学Day16-- STL容器类型总结
  • DBLens 业界首创AI表结构变更审查,智能评估影响,助力开发效率跃升。
  • 【运维】githubvercel学习使用
  • 【QT入门到晋级】进程间通信(IPC)-socket(包含性能优化案例)
  • 源代码安装部署lamp