zoukankan      html  css  js  c++  java
  • [mybatis]Example的用法

    转自:https://blog.csdn.net/zhemeban/article/details/71901759

    Example类是什么?
    Example类指定如何构建一个动态的where子句. 表中的每个non-BLOB列可以被包括在where子句中. 例子是展示此类用法的最好方式.

    Example类可以用来生成一个几乎无限的where子句.

    Example类包含一个内部静态类 Criteria 包含一个用 anded 组合在where子句中的条件列表. Example类包含一个 List 属性,所有内部类Criteria中的子句会用 ored组合在一起. 使用不同属性的 Criteria 类允许您生成无限类型的where子句.

    创建 Criteria 对象 可以使用Example类中的 createCriteria() 或者 or() . 如果 Criteria 对象是用 createCriteria() 创建的,它会自动为 List 属性添加一个 Criteria 对象 - 这使得它更容易写一个简单的where子句, 如果您不需要 or 或者其他几个子句组合的话. 用 or(Criteria criteria) 方法创建 Criteria 对象, 方法里的 criteria 对象会被添加进 Criteria 对象的列表中.

    重要 我们推荐您只使用 or() 方法创建 Criteria 对象. 我们相信这种方法使代码更有可读性.

    如何生成Example类?
    mybatis的的配置文件可以使用mybatis-generator工具生成,它就可以帮我们生成example类。
    根据 Mybatis 代码生成工具文档,需要一个配置文件,这里命名为:mbgConfiguration.xml放在 src 目录下. 配置文件内容如下:

    <?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">

    <generatorConfiguration>

    <!-- 配置mysql 驱动jar包路径.用了绝对路径 -->
    <classPathEntry location="D:WorkJavaeclipseworkspacemyBatisGeneratorWebContentWEB-INFlibmysql-connector-java-5.1.22-bin.jar" />

    <context id="yihaomen_mysql_tables" targetRuntime="MyBatis3">

    <!-- 为了防止生成的代码中有很多注释,比较难看,加入下面的配置控制 -->
    <commentGenerator>
    <property name="suppressAllComments" value="true" />
    <property name="suppressDate" value="true" />
    </commentGenerator>
    <!-- 注释控制完毕 -->

    <!-- 数据库连接 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    connectionURL="jdbc:mysql://127.0.0.1:3306/mybatis?characterEncoding=utf8"
    userId="root"
    password="password">
    </jdbcConnection>

    <javaTypeResolver >
    <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

    <!-- 数据表对应的model 层 -->
    <javaModelGenerator targetPackage="com.yihaomen.model" targetProject="src">
    <property name="enableSubPackages" value="true" />
    <property name="trimStrings" value="true" />
    </javaModelGenerator>

    <!-- sql mapper 隐射配置文件 -->
    <sqlMapGenerator targetPackage="com.yihaomen.mapper" targetProject="src">
    <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

    <!-- 在ibatis2 中是dao层,但在mybatis3中,其实就是mapper接口 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.yihaomen.inter" targetProject="src">
    <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

    <!-- 要对那些数据表进行生成操作,必须要有一个. -->
    <table schema="mybatis" tableName="category" domainObjectName="Category"
    enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    selectByExampleQueryId="false">
    </table>

    </context>
    </generatorConfiguration>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    当我们需要生成example类的时候,需要table里面去掉

    enableCountByExample="false" enableUpdateByExample="false"
    enableDeleteByExample="false" enableSelectByExample="false"
    selectByExampleQueryId="false"
    1
    2
    3
    example如何使用?
    简单查询
    这个例子展示了如何用生成后的Example类去生成一个简单的where子句:

    TestTableExample example = new TestTableExample();

    example.createCriteria().andField1EqualTo(5);
    作为另一种选择, 下面的方式也是可以的:

    TestTableExample example = new TestTableExample();

    example.or().andField1EqualTo(5);
    在上面的例子中, 动态生成的where子句是:

    where field1 = 5

    下面的例子展示了如何用生成后的Example类去生成一个复杂的where子句 (用到了 JSE 5.0 的泛型):

    TestTableExample example = new TestTableExample();

    example.or()
    .andField1EqualTo(5)
    .andField2IsNull();

    example.or()
    .andField3NotEqualTo(9)
    .andField4IsNotNull();

    List field5Values = new ArrayList();
    field5Values.add(8);
    field5Values.add(11);
    field5Values.add(14);
    field5Values.add(22);

    example.or()
    .andField5In(field5Values);

    example.or()
    .andField6Between(3, 7);

    在上面的例子中, 动态生成的where子句是:

    where (field1 = 5 and field2 is null)
    or (field3 <> 9 and field4 is not null)
    or (field5 in (8, 11, 14, 22))
    or (field6 between 3 and 7)
    将会返回满足这些条件的记录结果.

    去重复查询
    您可以在所有的Example类中调用 setDistinct(true) 方法进行强制去重复查询.

    Criteria类
    Criteria 内部类的每个属性都包含 andXXX 方法,以及如下的标准的SQL查询方法:

    IS NULL - 指相关的列必须为NULL
    IS NOT NULL - 指相关的列必须不为NULL
    = (equal) - 指相关的列必须等于方法参数中的值
    <> (not equal) - 指相关的列必须不等于方法参数中的值

    (greater than) - 指相关的列必须大于方法参数中的值
    = (greater than or equal) - 指相关的列必须大于等于方法参数中的值
    < (less than) - 指相关的列必须小于于方法参数中的值
    <= (less than or equal) - 指相关的列必须小于等于方法参数中的值
    LIKE - 指相关的列必须 “like” 方法参数中的值. 这个方法不用必须加入 ‘%’, 您必须设置方法参数中的值.
    NOT LIKE - 指相关的列必须 “not like” 方法参数中的值. 这个方法不用必须加入 ‘%’, 您必须设置方法参数中的值.
    BETWEEN - 指相关的列必须在 “between” 方法参数中的两个值之间.
    NOT BETWEEN - 指相关的列必须不在 “not between” 方法参数中的两个值之间.
    IN - 指相关的列必须在传入的方法参数的list中.
    NOT IN - 指相关的列必须不在传入的方法参数的list中.
    ---------------------
    作者:盖丽男
    来源:CSDN
    原文:https://blog.csdn.net/zhemeban/article/details/71901759
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    JQuery
    C#基础知识
    CSS
    学习.NET
    Grunt常见问题
    一个小型的类库
    SQL字符串处理!
    安装Java8以后,Eclipse运行异常解决方案
    谈薪四式让你谈好薪
    使用js实现input输入框的增加
  • 原文地址:https://www.cnblogs.com/sharpest/p/10296884.html
Copyright © 2011-2022 走看看