IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> 动态sql以及Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException -> 正文阅读

[大数据]动态sql以及Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

1.数据库表:

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;

BlogMapper接口代码


 //插入数据
    int addBlog(Blog blog);
//查询博客
    List<Blog> queryBlogIF(Map map);

BlogMapper.xml 代码

<!--插入-->
<insert id="addBlog" parameterType="blog">
    insert into mybatis.blog(id,title,author,create_time,views)
    values (#{id},#{title},#{author},#{createTime},#{views});
</insert>

测试类:

@Test
    public void addInitBlog(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);

        Blog blog=new Blog();
        blog.setId(IDutils.getId());
        blog.setTitle("Mybatis 如此简单");
        blog.setAuthor("浮川@");
        blog.setCreateTime(new Date());
        blog.setViews(9999);
        mapper.addBlog(blog);

        blog.setId(IDutils.getId());
        blog.setTitle("java 如此简单");
        mapper.addBlog(blog);

        blog.setId(IDutils.getId());
        blog.setTitle("String 如此简单");
        mapper.addBlog(blog);

        blog.setId(IDutils.getId());
        blog.setTitle("微服务 如此简单");
        mapper.addBlog(blog);

        sqlSession.close();
    }

IF语句的使用
当我们的查询语句为

<!--查询-->
<select id="queryBlogIF" parameterType="map" resultType="blog">
       select * from mybatis.blog where

           <if test="title !=null">
               title=#{title}
           </if>
           <if test="author !=null">
              and author=#{author}
           </if>

   </select>

 @Test
    public void queryBlogIF() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        hashMap.put("title", "java如此简单");
        //hashMap.put("author", "浮川@");

        List<Blog> blogs = mapper.queryBlogIF(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

输出的语句是正确的,where后面加条件

==> Preparing: select * from mybatis.blog where title=?

当我只查author,这么写时就会报错

==>  Preparing: select * from mybatis.blog where and author=?
==> Parameters: 浮川@(String)

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and author='浮川@'' at line 5
### The error may exist in com/sophy/dao/BlogMapper.xml
### The error may involve com.sophy.dao.BlogMapper.queryBlogIF-Inline
### The error occurred while setting parameters
### SQL: select * from mybatis.blog where                                             and author=?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'and author='浮川@'' at line 5

相当于 select * from mybatis.blog where and and author=#{author} 肯定要报错

@Test
    public void queryBlogIF() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
//    hashMap.put("title", "java如此简单");
        hashMap.put("author", "浮川@");

        List<Blog> blogs = mapper.queryBlogIF(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

结果:

==> Preparing: select * from mybatis.blog where and author=?
==> Parameters: 浮川@(String)

org.apache.ibatis.exceptions.PersistenceException:

Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘and author=‘浮川@’’ at line 5

The error may exist in com/sophy/dao/BlogMapper.xml

The error may involve com.sophy.dao.BlogMapper.queryBlogIF-Inline

The error occurred while setting parameters

SQL: select * from mybatis.blog where and author=?

Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘and author=‘浮川@’’ at line 5

当将where作为标签使用之后:
BlogMapper.xml

 <!--查询-->
    <select id="queryBlogIF" parameterType="map" resultType="blog">
        select * from mybatis.blog

           <where>
               <if test="title !=null">
                   title=#{title}
               </if>
               <if test="author !=null">
                   and author=#{author}
               </if>
           </where>


    </select>

测试类:


    @Test
    public void queryBlogIF() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        //hashMap.put("title", "java 如此简单");
        hashMap.put("author", "浮川@");

        List<Blog> blogs = mapper.queryBlogIF(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

结果
Preparing: select * from mybatis.blog WHERE author=?
==> Parameters: 浮川@(String)

 <if test="author !=null">
                   and author=#{author}
               </if>
<where></where>
<!--标签可以自动检查语句,如果条件是第一个,会自定将and去掉-->

当需要时and又会自动加上:

  @Test
    public void queryBlogIF() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        hashMap.put("title", "java 如此简单");
        hashMap.put("author", "浮川@");

        List<Blog> blogs = mapper.queryBlogIF(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

结果:

Preparing: select * from mybatis.blog WHERE title=? and author=?
> Parameters: java 如此简单(String), 浮川@(String)
<
Columns: id, title, author, create_time, views
<== Row: 06bfde70d1ca4875b309ba02b656139f, java 如此简单, 浮川@, 2022-02-03 23:59:24.0, 9999
<== Row: 6839c883e7084a169bbecbe1e67dee37, java 如此简单, 浮川@, 2022-02-04 10:39:54.0, 9999
<== Row: ec35cd5812e348d2a7edeca1d660a5de, java 如此简单, 浮川@, 2022-02-04 10:40:41.0, 9999
<== Row: 58514c0dd3f74a75ad0b704c46bbfcf1, java 如此简单, 浮川@, 2022-02-04 10:41:58.0, 9999
<== Total: 4

最后,当什么都不传的时候,会将where自动去掉:

 @Test
    public void queryBlogIF() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
       // hashMap.put("title", "java 如此简单");
      //  hashMap.put("author", "浮川@");

        List<Blog> blogs = mapper.queryBlogIF(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

空的hashMap,看sql语句:select * from mybatis.blog
where 已经自动去掉了

在这里插入图片描述

choose,when,otherwise的使用
接口:

List<Blog> queryBlogChoose(Map map);

BlogMapper.xml

 <select id="queryBlogChoose" parameterType="map" resultType="blog">
        select * from mybatis.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>

测试类


    @Test
    public void queryBlogChoose() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        /*hashMap.put("title", "java 如此简单");
        hashMap.put("author", "浮川@");
        hashMap.put("views", "9999");
*/
        List<Blog> blogs = mapper.queryBlogChoose(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

什么都不传: Preparing: select * from mybatis.blog WHERE views=?
==> Parameters: null

当传递views的参数时:


    @Test
    public void queryBlogChoose() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        hashMap.put("title", "java 如此简单");
        hashMap.put("author", "浮川@");
        hashMap.put("views", "9999");

        List<Blog> blogs = mapper.queryBlogChoose(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

Preparing: select * from mybatis.blog WHERE views=?
==> Parameters: 9999(String)
会自动去掉and
在这里插入图片描述
查出了所有views为9999的blog

当传递的参数为:

 public void queryBlogChoose() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        hashMap.put("title", "java 如此简单");
       // hashMap.put("author", "浮川@");
        hashMap.put("views", "9999");

        List<Blog> blogs = mapper.queryBlogChoose(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

满足第一个条件了,查出了四条,因为是choose,只能选择其中一个,由于第一个已经满足了,不会再选择其它的
Preparing: select * from mybatis.blog WHERE title=?
==> Parameters: java 如此简单(String)
在这里插入图片描述

@Test
    public void queryBlogChoose() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        //hashMap.put("title", "java 如此简单");
         hashMap.put("author", "浮川@");
         hashMap.put("views", "9999");

        List<Blog> blogs = mapper.queryBlogChoose(hashMap);
        for (Blog blog : blogs) {
            System.out.println(blog);
        }
        sqlSession.close();
    }

当第一个条件不满足,或者没有写第一个条件满足,第二个条件满足,会自动去掉and

在这里插入图片描述

Preparing: select * from mybatis.blog WHERE author=?
==> Parameters: 浮川@(String)

set 的使用:
接口

  //更新博客
    int updateBlog(Map map);

BlogMapper.xml的查询语句

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

测试:

@Test
    public void updateBlog() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        hashMap.put("title", "java 如此简单2");
       // hashMap.put("author", "浮川@@");
     //  hashMap.put("views", "9999");
        hashMap.put("id", "c3a0c6098b0940c48cf46d58b20019d5");
        mapper.updateBlog(hashMap);


        sqlSession.close();
    }

更新成功:
在这里插入图片描述当为第二个条件满足时,会自动去掉BlogMapper.xml里面的更新语句里面的" , "

@Test
    public void updateBlog() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        //hashMap.put("title", "java 如此简单2");
        hashMap.put("author", "浮川@@");
     //  hashMap.put("views", "9999");
        hashMap.put("id", "c3a0c6098b0940c48cf46d58b20019d5");
        mapper.updateBlog(hashMap);


        sqlSession.close();
    }

在这里插入图片描述当只写id时:

@Test
    public void updateBlog() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        HashMap hashMap = new HashMap();
        //hashMap.put("title", "java 如此简单2");
        //hashMap.put("author", "浮川@@");
     //  hashMap.put("views", "9999");
        hashMap.put("id", "c3a0c6098b0940c48cf46d58b20019d5");
        mapper.updateBlog(hashMap);


        sqlSession.close();
    }

报错 语句不完整 set条件也不会有
SQL: update mybatis.blog where id=? 这个语句不完整
在这里插入图片描述

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-02-05 21:48:17  更:2022-02-05 21:48:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 13:58:36-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码