前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于MyBatis用mapper.xml文件配置执行任意字符串拼接sql语句

基于MyBatis用mapper.xml文件配置执行任意字符串拼接sql语句

作者头像
麦克劳林
发布2018-09-11 16:48:19
8.8K1
发布2018-09-11 16:48:19
举报

1、背景

由于做的一个小项目里需要联动查询,一想16种情况,因为我是SSM框架写的,这样我就要写16个接口,16个实现,16条sql语句,想想就大头。既然数据库本身接收的就是String类型,那我就直接在implement中写喽,拼接sql语句。

image.png

2、编码内容

1)、mapper.xml中,只需要写一条数据库查询语句即可:

代码语言:javascript
复制
<select id="MapperCommonSelect" resultMap="UserBaseResultMap" parameterType="java.lang.String">
  select 
  u.*,p.*
  from cetc50_user_info u,cetc50_project_info p
  where ${_parameter} u.ProjectId = p.ProjectId
  order by UserId
</select>

${_parameter}就是我要拼接的内容(注意下划线不能少不能少)。

2)、我们再看*impl.java中的文件

代码语言:javascript
复制
  public List<User> selectByProjectIdAndInuseAndApproval(String userName,Integer projectId,Integer inuse,Integer approval){
    List<User> userList = new ArrayList<User>();
    String strsql = "";
    if(userName.equals("")){
        if(projectId > 0){
            if(inuse < 0){
                if(approval < 0){
                    strsql = "u.ProjectId = "+projectId+" and";
                }else{
                    strsql = "u.ProjectId = "+projectId+" and"+" approval = "+approval+" and";
                }
            }else{
                if(approval < 0){
                    strsql = "u.ProjectId = "+projectId+" and"+" inuse = "+inuse+" and";
                }else{
                    strsql = "u.ProjectId = "+projectId+" and"+" approval = "+approval+" and"+" inuse = "+inuse+" and";
                }
            }
        }else{
            if(inuse < 0){
                if(approval < 0){
                    strsql = "";
                }else{
                    strsql = " approval = "+approval+" and";
                }
            }else{
                if(approval < 0){
                    strsql = " inuse = "+inuse+" and";
                }else{
                    strsql = " approval = "+approval+" and"+" inuse = "+inuse+" and";
                }
            }
        }
    }else{
        if(projectId > 0){
            if(inuse < 0){
                if(approval < 0){
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" u.ProjectId = "+projectId+" and";
                }else{
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" u.ProjectId = "+projectId+" and"+" approval = "+approval+" and";
                }
            }else{
                if(approval < 0){
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" u.ProjectId = "+projectId+" and"+" inuse = "+inuse+" and";
                }else{
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" u.ProjectId = "+projectId+" and"+" approval = "+approval+" and"+" inuse = "+inuse+" and";
                }
            }
        }else{
            if(inuse < 0){
                if(approval < 0){
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and";
                }else{
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" approval = "+approval+" and";
                }
            }else{
                if(approval < 0){
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" inuse = "+inuse+" and";
                }else{
                    strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" approval = "+approval+" and"+" inuse = "+inuse+" and";
                }
            }
        }
    }
    
    userList = this.userDao.MapperCommonSelect(strsql);
    return userList;
}

好吧十六种情况,写出来却是有点长。

3、重点问题

说下我遇到的问题吧,这个才是学习的重点。 问题1、但凡写过sql语句的人估计都曾经碰到过类似于Unknown column ‘xxx’ in ‘where clause’的问题。单从字面理解,我们很容易得出列名不存在的结论,但是,很多时候起始并不是由于列名出错造成的。 而是由于拼凑sql语句时对字符类型数据没有用引号引起来造成的。 例如:

代码语言:javascript
复制
strsql = "UserName like CONCAT('%','"+userName+"','%')"+" and"+" inuse = "+inuse+" and";

由于我的userName前面定义的String类型,我一开始没注意,没有增加引号,结果报Unknown column ‘xxx’ in ‘where clause’。

问题2、 报There is no getter for property named 'strsql' in 'class java.lang.String']错误。

代码语言:javascript
复制
where ${strsql} u.ProjectId = p.ProjectId

我一开始是这样写的,因为strsql是我mapper.java里的传值,请教度娘后,将strsql换成了_parameter,

代码语言:javascript
复制
where ${_parameter} u.ProjectId = p.ProjectId

最后就正确啦。数据也就惊奇的出现在我的前端。开心,哈哈哈

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.08.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、背景
  • 2、编码内容
  • 3、重点问题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档