zoukankan      html  css  js  c++  java
  • Mybaiis(3)

    本文大纲:

     1.1动态sql语句

     1.2逆向工程(generator)

     1.3分页助手(pagehelper)

    正文:

      1.1动态sql语句  

        if语句:用于解决给定的值是空,而导致查询或执行其他操作的结果为空

     

        where语句+if语句:用于解决当条件有多个,且此时第二个及以上条件满足而,第一个条件不满足时导致sql出错,"and" 或 ”or“ 无法正常使用时where语句将自动取舍"and" 或 ”or“ 

      

        where语句+if语句:测试

        set语句+if语句:用于修改时防止被修改的字段没有进行新的设置而导致将其字段在方法结束时变成null(可以理解为没有修改的字段保留原来的内容)

     

         set语句+if语句测试:

     choos+when+otherwise+模糊查询:choos+when+otherwise类似于Java中的switch,而想使用模糊查询需要用到 函数concat()

      choos+when+otherwise+模糊查询测试:

    trim语句:可以当作where语句使用

    trim语句测试:

    trim语句:可以当作set语句使用

    trim语句测试:

    sql片段:用于将重复可用的sql语句单独取出,方便需要使用的时候引用

     

    foreach 标签遍历:用于当多个条件的字段是同一个时,可以使用循环

    foreach 标签遍历测试:

    使用 or 

    测试

    1.2逆向工程(generator)

    1.进入Mybatis 官网:http://www.mybatis.org/generator/index.html

    2.下载jar包 generator

     

    3. 创建一个generator 的配置(xml)文件

    4. 进行详细情况更改:

    <?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="E:/jar/bigdata/mysql-connector-java-5.1.47.jar" />
    <!-- 数据源信息 -->
      <context id="DB2Tables" targetRuntime="MyBatis3">
    <!--  运行结束后静止产生注释-->
          <commentGenerator>
              <property name="suppressAllComments" value="true"/>
          </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/mybatis"
            userId="root"
            password="root">
        </jdbcConnection>
    
        <javaTypeResolver >
          <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
    <!-- 生成的实体类所在的位置 -->
        <javaModelGenerator targetPackage="com.zhiyou100.ydb.bean" targetProject="./src">
          <property name="enableSubPackages" value="true" />
          <property name="trimStrings" value="true" />
        </javaModelGenerator>
    <!-- 生成的映射文件所在的位置 -->
        <sqlMapGenerator targetPackage="com.zhiyou100.ydb.mapper"  targetProject="./rescources">
          <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
    <!-- 生成的dao文件所在的位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.zhiyou100.ydb.dao"  targetProject="./src">
          <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
    <!--
         某张表于实体类的对象关系
        schema:该表所在的数据库
        tableName:表名
        domainObjectName:实体类名
     -->
        <table schema="mybatis" tableName="class" domainObjectName="clazz"
         enableDeleteByExample="false" enableCountByExample="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>

     5.创建一个类用来运行generator

      5.1获取运行的代码:

       5.2放入到创建的类中运行(如果使用的时eclipse可以使用Ctrl + Shift + o 快捷键快速将需要导的包完成)

     小技巧(1)将原本generator中复杂的方法去除只保留基本的zsgc 

     

     小技巧(2)运行结束后静止产生注释

     

     1.3分页助手(pagehelper)

     1.进入pagehelper官网

     2.找到并进入中文版文档

    3.找到如何使用分页插件

     4.引入相关jar包

     推荐使用iSQL parser-2.0jar 和 pagehelper-5.1.10jar(如果使用其他版本时要注意可能出项jar包版本冲突导致报错)

     5.配置拦截器(以Mybatis为例)

     6.将拦截器放到合适的位置(不合适会报错)

     

     7.编写查询所有字段的dao方法

     8.在映射文件中也写好查询所有的sql语句

     9.测试

     注:本次使用的是pageinfo

  • 相关阅读:
    【鬼脸原创】github搭建动态网站
    WebStorm配置(2016/11/18更新)
    前端学习入门
    css笔记
    c# 将文本中的数据快速导入到数据库(200万左右的数据量)
    3.数据库单多表查询
    2.数据库表的增删改
    1.数据库建表
    浏览器的兼容性测试
    python-路径处理path
  • 原文地址:https://www.cnblogs.com/Kuriyama-Mirai/p/11437709.html
Copyright © 2011-2022 走看看