栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis实现动态SQL

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis实现动态SQL

注:本文所有内容都是建立在上篇文章的基础上

https://blog.csdn.net/liyuuhuvnjjv/article/details/122276943?spm=1001.2014.3001.5501

动态sql,就是根据不同的条件生成不同的sql语句

传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误。Mybatis的动态SQL功能正是为了解决这种问题,其通过 if, choose, when, otherwise, trim, where, set,foreach标签,可组合成非常灵活的SQL语句,从而提高开发人员的效率。

if 1.BlogMapper接口
package org.dao;

import org.pojo.Blog;

import java.util.List;
import java.util.Map;

public interface BlogMapper {
    //插入数据
    int addBlog(Blog blog);

    //查询博客
    List queryBlogIF(Map map);
}

2.BlogMapper.XML文件



    
        insert into mybatis.blog (id,title,author,create_time,views) values (#{id},#{title},#{author},#{createTime},#{views})
    

    
        select * from mybatis.blog where 1=1
        
            and title = #{title}
        
        
            and author = #{author}
        
    

3.测试类
import org.apache.ibatis.session.SqlSession;
import org.dao.BlogMapper;
import org.junit.Test;
import org.pojo.Blog;
import org.utils.IDutils;
import org.utils.MybatisUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class MyTest {
    @Test
    public void queryBlogIF(){
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtils.getSqlSession();
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            HashMap map = new HashMap();
            map.put("title","Java如此简单");
            map.put("author","lyh");
            List blogList = mapper.queryBlogIF(map);
            for (Blog blog : blogList) {
                System.out.println(blog);
            }
        }catch(Exception e){
            e.printStackTrace();
        }catch(Error e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
}

where 1.举例:

        select * from mybatis.blog
        
            
                
                    title=#{id}
                
                
                    and author=#{author}
                
                
                    and views=#{views}
                
            
        
    
    @Test
    public void queryBlogChoose(){
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtils.getSqlSession();
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            HashMap map = new HashMap();
            map.put("title","Java如此简单");
            map.put("author","lyh");
            map.put("views",9999);
            List blogList = mapper.queryBlogChoose(map);
            for (Blog blog : blogList) {
                System.out.println(blog);
            }
        }catch(Exception e){
            e.printStackTrace();
        }catch(Error e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
注:只能有一个条件满足,类似于Java中的swhich-case set
	//更新博客
    int updateBlog(Map map);

        update mybatis.blog
        
            
                title=#{title},
            
            
                author=#{author}
            
        
        where id=#{id}
    
	@Test
    public void updateBlog(){
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtils.getSqlSession();
            BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
            HashMap map = new HashMap();
            map.put("title","Java如此简单!!!");
            map.put("author","lyh");
            map.put("id","1093918391");
            mapper.updateBlog(map);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
        }catch(Error e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }

类似于使用动态更新语句的解决方案set,set元素可以用于动态包含需要更新的列,而舍弃其他的列。set元素会动态的前置SET关键字,同时也会删除无关的逗号。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/692526.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号