• Mybatis之Plus


    一、如何正确配置MP?

     1、在pom配置文件加入以下依赖,不要再加其他mybatis依赖避免冲突

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.2</version>
    </dependency>

     2、在springboot启动项上加注解

    @SpringBootApplication
    @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    3、如何配置分页插件?

    @Configuration
    public class MybatisPlusConfig {
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
            // paginationInterceptor.setOverflow(false);
            // 设置最大单页限制数量,默认 500 条,-1 不受限制
            // paginationInterceptor.setLimit(500);
            // 开启 count 的 join 优化,只针对部分 left join
            paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
            return paginationInterceptor;
        }
    }

    二、如何多表连接分页查询?

    在Mapper文件中新建一个接口#
    Mapper文件应该已经继承了BaseMapper
    public interface UserMapper extends BaseMapper<UserVo> {
        IPage<User> pageUser(IPage<UserVO> page);
    }
    对应的XML中添加相应的SQL语句,这里SQL语句按你之前的用法写即可,不需要考虑分页
    <select id="selectPageUser" resultType="cn.com">
        select name,unit from user,unit where user.unitno = unit.unitno
    </select>
    service层使用#
    像使用单表一样使用分页,返回类型依然是Ipage
    public IPage<UserVO> pageUseVo(int pageno, int pagesize) {
        Page<UserVO> page = new Page<>(pageno, pagesize);
        return userMapper.pageUser(page);
    }

     三、mapper接口和文件如何正确对应?

    1、mapper接口名要和mapper文件名一致。
    2、mapper文件的namespace等于mapper接口所在的路径,前面不能有空格。
    3、mapper接口的方法名和mapper配置文件的id保持一致。

     四、如何传递多个不同类型的参数?

    偶然碰到一个需要给xml传一个String类型和一个Integer类型的需求,当时心想用map感觉有点太浪费,所以专门研究了下各种方式。
    方法一:不需要写parameterType参数
    public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
    <
    select id="getXXXBeanList" resultType="XXBean">   select t.* from tableName where id = #{0} and name = #{1} </select> 由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始
    方法二:基于注解(最简单)
    public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code); <select id="getXXXBeanList" resultType="XXBean">   select t.* from tableName where id = #{id} and name = #{code} </select> 由于是多参数那么就不能使用parameterType, 这里用@Param来指定哪一个
    方法三:Map封装
    public List<XXXBean> getXXXBeanList(HashMap map); <select id="getXXXBeanList" parameterType="hashmap" resultType="XXBean">   select 字段... from XXX where id=#{xxId} code = #{xxCode} </select> 其中hashmap是mybatis自己配置好的直接使用就行。map中key的名字是那个就在#{}使用那个,map如何封装就不用了我说了吧。
    方法四:List封装
    public List<XXXBean> getXXXBeanList(List<String> list); <select id="getXXXBeanList" resultType="XXBean">   select 字段... from XXX where id in   <foreach item="item" index="index" collection="list" open="(" separator="," close=")">     #{item}   </foreach> </select> 总结 传递list和map在资源消耗上肯定远大于方法一和方法二,但是有一些特殊的情形需要传递list,比如你需要传递一个id集合并批量对id进行sql操作然后再返回等等。所以都需要了解。

     五、mybatis有哪些常用标签?

    1、定义sql语句的标签有哪些?
    select、insert、delete、update
    2、如何配置java对象属性与查询结果集列名对应关系?
    <resultMap id="BaseResultMap" type="com.online.charge.platform.student.model.Student">
    <result column="BIRTHDAY" property="birthday" /> <result column="AGE" property="age" />
    </resultMap>
    3、如何避免sql注入?
    尽量使用预编译#{},排序动态参数时只能为${}
    4、如何动态sql拼接?
    4.1 if 标签
    if标签通常用于WHERE语句、UPDATE语句、INSERT语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值。
    <if test="name != null and name != ''">
         and NAME = #{name}
    </if>
    4.2 foreach 标签 foreach标签主要用于构建in条件,可在sql中对集合进行迭代。也常用到批量删除、添加等操作中。 <!-- in查询所有,不分页 --> <select id="selectIn" resultMap="BaseResultMap"> select name,hobby  from student where id in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select> 属性介绍: collection:collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合。 item :表示在迭代过程中每一个元素的别名 index :表示在迭代过程中每次迭代到的位置(下标) open :前缀 close :后缀 separator :分隔符,表示迭代时每个元素之间以什么分隔 4.3 choose标签  有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。
    当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。 if是与(and)的关系,而choose是或(or)的关系。
    <select id="getStudentListChoose" parameterType="Student" resultMap="BaseResultMap"> SELECT * from STUDENT WHERE 1=1 <where> <choose> <when test="Name!=null and student!='' "> AND name LIKE CONCAT(CONCAT('%', #{student}),'%') </when> <when test="hobby!= null and hobby!= '' "> AND hobby = #{hobby} </when> <otherwise> AND AGE = 15 </otherwise> </choose> </where> </select>

    5、如何格式化输出?

      5.1、当name值为null时,查询语句会出现 “WHERE AND” 的情况,解决该情况除了将"WHERE"改为“WHERE 1=1”之外,还可以利用where标签。

      这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。

    <select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap">     
        SELECT * from STUDENT      
           <where>   
             <if test="name!=null and name!='' ">     
                NAME LIKE CONCAT(CONCAT('%', #{name}),'%')      
             </if>     
             <if test="hobby!= null and hobby!= '' ">     
                AND hobby = #{hobby}      
             </if>  
           </where>        
    </select>  

      5.2、没有使用if标签时,如果有一个参数为null,都会导致错误。当在update语句中使用if标签时,如果最后的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置set关键字,和剔除追加到条件末尾的任何不相关的逗号。使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。

    <update id="updateStudent" parameterType="Object">     
        UPDATE STUDENT      
        <set>     
            <if test="name!=null and name!='' ">     
                NAME = #{name},      
            </if>     
            <if test="hobby!=null and hobby!='' ">     
                MAJOR = #{major},      
            </if> 
            <if test="hobby!=null and hobby!='' ">     
                HOBBY = #{hobby}    
            </if>     
        </set>     
        WHERE ID = #{id};      
    </update>  

      5.3、trim标签,格式化输出,也可以通过trim标签设定或忽略前后缀来实现

    <update id="updateByPrimaryKey" parameterType="Object">
            update student set 
      <trim  suffixOverrides="," > 
        <if test="name != null  ">
            NAME=#{name},
        </if>
        <if test="hobby != null  ">
            HOBBY=#{hobby},
        </if>
      </trim> where id=#{id}
    </update>

    6、如何配置关联关系?

    对多关联映射:collection
    对一关联映射:association 
    此外注意:一对多使用的都是lazy(懒加载)。
    https://www.cnblogs.com/zjfjava/p/8911238.htm

    7、如何定义常量及引用?

       当多种类型的查询语句的查询字段或者查询条件相同时,可以将其定义为常量,方便调用。为求<select>结构清晰也可将sql语句分解。

       sql标签和include标签

    8、对于<,>如何转义?

      <![CDATA[ > ]]>

  • 相关阅读:
    root用户Linux 环境变量的配置解决(-bash: jps: command not found)有关问题
    Linux Crontab内环境变量与Shell环境变量的关系及解决问题的办法
    RocketMQ os.sh 系统优化(CentOS)
    Spring Boot修改内置Tomcat端口号
    SpringBoot多跨域请求的支持(JSONP)
    [译]Spring Boot 构建一个RESTful Web服务
    delphi怎样把子窗体显示在pagecontrol的tabsheet
    delphi从TRichEdit获得RTF格式文本(PC版本)
    数据类型表(DELPHI、C++)
    对程序进行注释
  • 原文地址:https://www.cnblogs.com/772933011qq/p/13448622.html
Copyright © 2020-2023  润新知