mybatis注解开发动态sql

项目中需要用到mybatis注解开发。所以需要把若依生成的mybatis的xml文件,改写成注解形式
有几个的改写遇到了麻烦。

首先是批量删除,若依生成的批量删除和按id删除,是在一个方法里面的。所以要对前端传回来的id进行操作

// 数据源按id删除
    @DeleteMapping(value = "/deletesourcebyid/{dsids}")
    public String deletesourcebyid(@PathVariable String dsids){
        Responemsg responemsg = new Responemsg();
        if (dsids.length() == 1){
            if(datasourceMapper.deleteDatasourceById(dsids)>0){
                responemsg.setCode("200");
                responemsg.setMsg("操作成功");
            }
        }else if(dsids.length() > 1){
            if(datasourceMapper.deleteDatasourceByIds(dsids)>0){
                responemsg.setCode("200");
                responemsg.setMsg("操作成功");
            }
        }
        return new Gson().toJson(responemsg);
    }

因为sql是不能接收数组类型的,所以只能后台接收前端数据,然后构造成一个字符串传给sql
这里是我没合并前的批量删除代码

// 数据源按id数组批量删除
    @RequestMapping(value = "/deletesourcebyids", method = {RequestMethod.GET, RequestMethod.POST})
    public String deletesourcebyids(HttpServletRequest request, @RequestBody String bodyin1){
        Responemsg responemsg = new Responemsg();
        logmsg.printerrormsg("传入:"+bodyin1);

        JSONObject jsonObject = JSONObject.fromObject(bodyin1);
        String array = jsonObject.getJSONObject("array").getString("dsids").replace("[", "").replace("]", "");

        System.err.println(array);

        if (datasourceMapper.deleteDatasourceByIds(array) > 0){
            responemsg.setCode("200");
            responemsg.setMsg("操作成功");
        }

        return new Gson().toJson(responemsg);
    }

这里要用${},而不是用#{}。因为#{}会预编译,也就是会多加''

// 批量按数据源主键删除报表
    @Delete("delete from xxx.datasource where dsid in (${array})")
    int deleteDatasourceByIds(@Param("array") String array);

然后是若依生成的模糊查询,这里一开始一直提示我元素类型 "if" 必须后跟属性规范 ">" 或 "/>"。,因为我一开始用!='',判断参数为空字符串,然后试了<![CDATA[]]>用来免编译没有效果,后来改成了!=\"\"
然后对着mybatis的官网看,发现普通的where<where>的效果是不一样的,所以就把where从select那句末尾中拿出来,写成标签。

// 查询数据源简介列表
    @Select("<script>"
            +"select dsid, dsname, dstype, dsip, dsport, dname, dsuser, dspassword from xxx.datasource"
            +"<where>"
            + "<if test='dsname != null  and dsname !=\"\" '> and dsname like concat('%', #{dsname}, '%')</if>"
            + "<if test='dstype != null '> and dstype = #{dstype}</if>"
            + "<if test='dsip != null  and dsip != \"\" '>and dsip = #{dsip}</if>"
            + "<if test='dsport != null  and dsport != \"\" '> and dsport = #{dsport}</if>"
            + "<if test='dname != null  and dname != \"\" '>and dname like concat('%', #{dname}, '%')</if>"
            + "<if test='dsuser != null  and dsuser != \"\" '> and dsuser = #{dsuser}</if>"
            + "<if test='dspassword != null  and dspassword != \"\" '>and dspassword = #{dspassword}</if>"
            + "</where>"
            + "</script>")
    List<Datasource> getDatasourceList();

回到公司,把昨晚回家写的代码复制过来,发现还是出问题了。
大佬跟我讲搜一下,springboot整合mybatis显示执行的sql。
搜索结果,在配置文件application.properties中添加一句

logging.level.com.XXX.XXX.XXX.XXX=debug // XXX都是包名

打印出来,发现是SQL是错误的select dsid, dsname, dstype, dsip, dsport, dname, dsuser, dspassword from xxx.datasource WHERE datasource.dstype = ?
在第二个if标签中添加一句datasource.dstype != 0,因为这是整型,需要判断。

@Select("<script>"
            +"select dsid, dsname, dstype, dsip, dsport, dname, dsuser, dspassword from xxx.datasource"
            +"<where>"
            + "<if test='datasource.dsname != null  and datasource.dsname !=\"\" '> and dsname like concat('%', #{datasource.dsname}, '%')</if>"
            + "<if test='datasource.dstype != null and datasource.dstype != 0 '> and datasource.dstype = #{datasource.dstype}</if>"
            + "<if test='datasource.dsip != null  and datasource.dsip != \"\" '> and dsip = #{datasource.dsip}</if>"
            + "<if test='datasource.dsport != null  and datasource.dsport != \"\" '> and dsport = #{datasource.dsport}</if>"
            + "<if test='datasource.dname != null  and datasource.dname != \"\" '> and dname like concat('%', #{datasource.dname}, '%')</if>"
            + "<if test='datasource.dsuser != null  and datasource.dsuser != \"\" '> and dsuser = #{datasource.dsuser}</if>"
            + "<if test='datasource.dspassword != null  and datasource.dspassword != \"\" '> and dspassword = #{datasource.dspassword}</if>"
            + "</where>"
            + "</script>")
    List<Datasource> getDatasourceList(@Param("datasource")  Datasource datasource);

因为若依生成的模糊查询和普通全部查询是写在一起的,所以要传进一个实体类参数。
这里if标签里的字段是方法中传进来的。所以最好添加param注解@Param("datasource")并且用datasource.dsname这种方式
这段里面只有and dsname like concat才是原本的sql

posted @ 2021-04-07 01:16  张三丰学Java  阅读(568)  评论(0编辑  收藏  举报