zoukankan      html  css  js  c++  java
  • Mybatisplus 自定义sql 使用条件构造器

    Mybatisplus 自定义sql 使用条件构造器

    两种方式

    注解方式

    动态查找:
    @Select("select ${ew.SqlSelect} from ${tableName} ${ew.customSqlSegment}")
    List<File> listFileByCondition(@Param("tableName") String tableName, @Param("ew") Wrapper wrapper);
    
    ew.SqlSelect:所需要查找的字段
    
    tableName:使用的是那张表
    
    ew.customSqlSegment:条件
    用法:allFileMapper.listFileByCondition(tableName,Wrappers.query().select("*").in("relation_uuid", uuids));
    结果: select * from tablName where relation_uuid in ()
    
    
    动态修改:
    @Update("update ${tableName} set ${ew.sqlSet} ${ew.customSqlSegment}")
    int updateByCondition(@Param("tableName") String tableName, @Param("ew") Wrapper wrapper);
    
    ew.sqlSet:修改的字段
    
    tableName:使用的是那张表
    
    ew.customSqlSegment:条件
    
    用法:
    mapper.updateByCondition(tableName, Wrappers.update().set("state", "初始状态").in("id", ids));
    结果: update tableName set state = '初始状态' where id in ()
    

    xml方式

    查找:
    <select id="listFileByCondition" resultType="com.example.entity.File">
    	SELECT ${ew.SqlSelect} FROM ${tableName} ${ew.customSqlSegment}
    </select>
    
    
    修改:
    <update id="listFileByCondition" >
    	update ${tableName} ${ew.SqlSelect}  ${ew.customSqlSegment}
    </update>
    

    查找带分页

    xml用法:
    Page<File> selectPage(Page page, @Param("tableName") String tableName, @Param("ew") Wrapper wrapper);
    
    <select id="selectPage" resultType="com.example.entity.File">
            select * from ${tableName} ${ew.customSqlSegment}
    </select>
    
    注解分页:



    /**
         * 
         * @param rowBounds 分页对象 直接传入page即可
         * @param wrapper 条件构造器
         * @return
         */
        List<User> selectUserWrapper(RowBounds rowBounds, @Param("ew") Wrapper<User> wrapper);
    

    UserMapper.xml加入对应的xml节点:

        <!-- 条件构造器形式 -->
        <select id="selectUserWrapper" resultType="user">
            SELECT
            <include refid="Base_Column_List" />
            FROM USER
            <where>
                ${ew.sqlSegment}
            </where>
        </select>
    

    测试类:

    @Test
        public void testCustomSql() {
            User user = new User();
            user.setCode("703");
            user.setName("okong-condition");
            user.insert();
            
            EntityWrapper<User> qryWrapper = new EntityWrapper<>();
            qryWrapper.eq(User.CODE, user.getCode());
            
            Page<User> pageUser = new Page<>();
            pageUser.setCurrent(1);
            pageUser.setSize(10);
            
            List<User> userlist = userDao.selectUserWrapper(pageUser, qryWrapper);
            System.out.println(userlist.get(0));
            log.info("自定义sql结束");
        }



    自定义SQL语句

    在一些需要多表关联时,条件构造器和通用CURD都无法满足时,还可以自行手写sql语句进行扩展。注意:这都是mybatis的用法。

    以下两种方式都是改造UserDao接口。

    注解形式

    @Select("SELECT * FROM USER WHERE CODE = #{userCode}")
        List<User> selectUserCustomParamsByAnno(@Param("userCode")String userCode);
    

    xml形式

    List<User> selectUserCustomParamsByXml(@Param("userCode")String userCode);
    

    同时,UserMapper.xml新增一个节点:

        <!-- 由于设置了别名:typeAliasesPackage=cn.lqdev.learning.mybatisplus.samples.biz.entity,所以resultType可以不写全路径了。 -->
        <select id="selectUserCustomParamsByXml" resultType="user">
            SELECT 
            <include refid="Base_Column_List"/> 
            FROM USER 
           WHERE CODE = #{userCode}
        </select>

     

  • 相关阅读:
    django静态资源转移
    QT5 内置Multimedia开发音乐播放器
    Qt Creator 设置编码格式为 UTF-8
    QT 出错 moc_mainwindow.obj:-1: error: LNK2019: 无法解析的外部符号 " 中被引用...
    linux 安装node, 添加软链接,更改npm安装源
    django.template.exceptions.TemplateDoesNotExist: index.html
    centos下使用virtualenv建立python虚拟环境
    win7上 nginx 出现 403 Forbidden
    django安装xadmin中出现的报错汇总
    centos安装mysql57
  • 原文地址:https://www.cnblogs.com/lyf906522290/p/13224568.html
Copyright © 2011-2022 走看看