动态sql语句、逆向工程(generator)、分页助手(pagehelper)

1.动态sql语句

  1. if

  2. if where 配合使用

    <select id="selectByWhere" resultType="com.alibaba.wlq.bean.Users">
         select * from users
         <where>
             <if test="name!=null and name !=''">
                 and name = #{name}
             </if>
             <if test="sex!=null and sex!=''">
                 and sex = #{sex}
             </if>
         </where>
    </select>

    where的功能是在满足条件的第一个sql语句前面添加where,如果第一个满足条件的sql语句前面有and或者or,那么where标签的功能就是替换掉它

  3. if set配合使用

    <update id="updateUsers">
         update users 
         <set>
             <if test="name!=null and name !=''">
                 name = #{name},
             </if>
             <if test="age>0">
                 age = #{age},
             </if>
             <if test="sex!=null and sex!=''">
                 sex = #{sex},
             </if>
         </set>
         where id = #{id}
    </update>

    set标签我理解的功能就是在第一个满足if条件的sql语句前面增加set并且会将最后一个满足if条件的sql语句的逗号给去掉防止sql语句出错

  4. if + trim代替where/set标签
    prefix

    prefixOverrides

    suffix
    suffixOverrides

  5. choose(when otherwise)
    有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为true,就会执行if标签中的条件。MyBatis提供了choose 元素。if标签是与(and)的关系,而choose比傲天是或(or)的关系。

    ​ choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,我们使用where将choose包起来,放置关键字多于错误。

  6. sql字段

    <sql id="selectByAgesColumns">
         id,name,age,sex
    </sql>
    
    
    <select id="selectByAges" resultType="com.alibaba.wlq.bean.Users">
         select <include refid="selectByAgesColumns"/> from users 
         <if test="ages.size()>0">
             <where>
                 <foreach collection="ages" open="age in(" close=")" separator="," item="age" >
                     #{age}
                 </foreach>
             </where>
         </if>   
    </select>
  7. foreach(同上)
    对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。List 实例将使用“list”做为键,数组实例以“array” 做为键。

    foreach元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。

    注意:你可以传递一个List实例或者数组作为参数对象传给MyBatis。当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。List实例将会以“list”作为键,而数组实例将会以“array”作为键。

    这个部分是对关于XML配置文件和XML映射文件的而讨论的。下一部分将详细讨论Java API,所以你可以得到你已经创建的最有效的映射。

2.逆向工程(generator)

由表帮我们生成bean、dao、xml文件
http://www.mybatis.org/generator/index.html

  1. 引入mybatis-generator的jar包

  2. 创建generator的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE generatorConfiguration
      PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
      "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    <!-- generatorjar包的路径 -->
    <generatorConfiguration>
      <classPathEntry location="/E:/generator/mybatis-generator-core-1.3.5.jar" />
    <!-- 加载驱动 -->
      <context id="DB2Tables" targetRuntime="MyBatis3">
         <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true" />
     </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis"
            userId="root"
            password="123456">
        </jdbcConnection>
    <!-- 是否允许大数字 -->
        <javaTypeResolver >
          <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
    <!-- 实体类文件配置 -->
        <javaModelGenerator targetPackage="com.alibaba.wlq.bean" targetProject="./src">
          <property name="enableSubPackages" value="true" />
          <property name="trimStrings" value="true" />
        </javaModelGenerator>
    <!-- 映射文件配置 -->
        <sqlMapGenerator targetPackage="com.alibaba.wlq.mapper"  targetProject="./resources">
          <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
    <!--  dao接口配置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.alibaba.wlq.dao"  targetProject="./src">
          <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
    <!--  -->
        <table schema="mybatis" tableName="users" domainObjectName="Users" enableCountByExample="false"
             enableDeleteByExample="false" enableSelectByExample="false"
             enableUpdateByExample="false">
          <property name="useActualColumnNames" value="true"/>
          <generatedKey column="ID" sqlStatement="DB2" identity="true" />
          <columnOverride column="DATE_FIELD" property="startDate" />
          <ignoreColumn column="FRED" />
          <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        </table>
      </context>
    
    </generatorConfiguration>
  3. 运行generator

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class generatormain {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        List<String> warnings = new ArrayList<String>();
           boolean overwrite = true;
           File configFile = new File("resources/generator.xml");
           ConfigurationParser cp = new ConfigurationParser(warnings);
           Configuration config = cp.parseConfiguration(configFile);
           DefaultShellCallback callback = new DefaultShellCallback(overwrite);
           MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
           myBatisGenerator.generate(null);
    }

}

3. PageHelper

https://pagehelper.github.io/

猜你喜欢

转载自www.cnblogs.com/wuliqqq/p/11437200.html