美文网首页
MyBatis 笔记

MyBatis 笔记

作者: A_1341 | 来源:发表于2018-08-02 10:19 被阅读0次

生成器

  • maven
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
<!-- Specifies whether the mojo writes progress messages to the log.
 -->
<!-- 指定mojo是否将进度消息写入日志。-->
                    <verbose>true</verbose>
<!-- Specifies whether the mojo overwrites existing Java files. Default is false.
Note that XML files are always merged.-->
<!-- 是否覆盖原生成JAVA文件, xml文件永远都是合并 -->
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

 <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
</resources>
  • 生成器配置
<?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>
    <classPathEntry
            location="C:\Users\fenqi\.m2\repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar"/>

    <context id="context" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="false"/>
        </commentGenerator>

        <jdbcConnection userId="root" password="pwd" driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/databaseName?useUnicode=true&amp;characterEncoding=UTF-8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC&amp;nullCatalogMeansCurrent=true"/>

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

        <javaModelGenerator targetPackage="cn.tiantianquan.springant.mybatis.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="cn.tiantianquan.springant.mybatis.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <javaClientGenerator targetPackage="cn.tiantianquan.springant.mybatis.mapper" type="XMLMAPPER" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <table schema="spring_ant_demo" tableName="pm_authority" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        <table schema="spring_ant_demo" tableName="pm_role" enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false"/>
        <table schema="spring_ant_demo" tableName="pm_role_has_pm_authority" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        <table schema="spring_ant_demo" tableName="pm_user" enableCountByExample="true" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
            <columnOverride column="create_time" javaType="java.time.LocalDateTime"/>
            <columnOverride column="update_time" javaType="java.time.LocalDateTime"/>
        </table>
        <table schema="spring_ant_demo" tableName="pm_user_has_pm_role" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
        <table schema="spring_ant_demo" tableName="param_sex" enableCountByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>


    </context>
</generatorConfiguration>
  • <columnOverride column="create_time" javaType="java.time.LocalDateTime"/> 转换数据库时间类型
  • 启用注释生成, 生成器再次生成 会根据 @mbg.generated 标识 来判断是不是要重新生成
  • <columnOverride column="row_version" javaType="java.time.LocalDateTime" isGeneratedAlways="true"/>isGeneratedAlways="true"会忽略数据库自动生成字段的insert,update,通常用于row_version字段
  <commentGenerator>
            <property name="suppressAllComments" value="false"/>
            <property name="suppressDate" value="false"/>
        </commentGenerator>

MyBatis Generator 详解

Mybatis

  • maven
    <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.4</version>
        </dependency>
  • @MapperScan({"cn.tiantianquan.springant.mybatis.modelMapper", "cn.tiantianquan.springant.mybatis.mapper"}) 应用启动程序添加注解,扫描所有mapper注入,相当于在Mapper接口上注解@Mapper
  • <sql> 替换模板
<sql id="table">
        pm_user
    </sql>

    <select id="selectWithParam" resultMap="WithParam">
        select *, ps.name as sex_str
        from  <include refid="table"/> pu
                 left join param_sex ps on pu.sex = ps.id
    </select>
  • ResultMap 继承
<resultMap id="WithParam" type="cn.tiantianquan.springant.model.PmUserModel" extends="cn.tiantianquan.springant.mybatis.mapper.PmUserMapper.BaseResultMap">
        <result column="sex_str" jdbcType="VARCHAR" property="sexStr"/>
    </resultMap>
  • where语句
    使用 where 标签包裹,没有条件会自动删除where
 <select id="select" resultMap="WithParam">
        select *, ps.name as sex_str
        from pm_user pu
        left join param_sex ps on pu.sex = ps.id
        <where>
            <if test="trueName != null">
                and true_name concat( '%',#{trueName},'%')
            </if>
            <if test="loginName != null">
                and login_name concat ('%',#{loginName},'%')
            </if>
        </where>
    </select>
  • 传入参数
    使用 @Param 注解指定传入参数名称
List<PmUserModel> select(
            @Param(value = "loginName") String loginName,
            @Param(value = "trueName") String trueName
    );

  • 自动映射
    application.properties 中设置, 映射数据库的下划线模式到驼峰模式
    自动映射默认开启
    有三种自动映射等级:
    NONE - 禁用自动映射。仅设置手动映射属性。
    PARTIAL - 将自动映射结果除了那些有内部定义内嵌结果映射的(joins).
    FULL - 自动映射所有。
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.auto-mapping-behavior=partial #默认设置

官方文档:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Auto-mapping

  • 防注入
    mybatis中的#和$的区别
  1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id".
  2. 将传入的数据直接显示生成在sql中。如:order byuser_id$,如果传入的值是111,那么解析成sql时的值为order by user_id, 如果传入的值是id,则解析成的sql为order by id.
  3. #方式能够很大程度防止sql注入。
  4. $方式无法防止Sql注入。
  5. $方式一般用于传入数据库对象,例如传入表名.
  6. 一般能用#的就别用$.

https://blog.csdn.net/kakaxi_77/article/details/46007239

相关文章

  • Spring系列 | 小荷才露尖尖角

    MyBatis学习笔记 MyBatis操练 MyBatis源码 SpringMVC学习笔记 SpringMVC...

  • MyBatis缓存

    MyBatis Mybatis笔记连载上篇连接Mybatis简单操作学习 Mybatis笔记连载下篇连接Mybat...

  • Mybatis动态SQL

    MyBatis Mybatis笔记连载上篇连接MyBatis缓存Mybatis笔记连载下篇连接 动态SQL 动态S...

  • Mybatis 学习笔记

    Mybatis 学习笔记 配置 MyBatis 环境 导入MyBaits依赖jar包 要使用 MyBatis, 需...

  • Mybatis学习笔记汇总(包括源码和jar包)

    博客整理 Mybatis学习笔记(一)——对原生jdbc中问题的总结 Mybatis学习笔记(二)——Mybati...

  • Mybatis笔记 一

    Mybatis笔记 一 为什么学Mybatis 目前最主流的持久层框架为hibernate与mybatis,而且国...

  • Mybatis--day01

    非本人总结的笔记,抄点笔记复习复习。感谢传智博客及黑马程序猿成长 什么是Mybatis ​ MyBatis 本...

  • 一,MyBatis应用分析与实践

    之前学习MyBatis整理了一些笔记,笔记分为四个部分:1.MyBatis应用分析与实践[https://www....

  • 初识MyBatis

    MyBatis学习笔记(一) 1、什么是MyBatis2、安装3、第一个mybatis实例 前言 1、ORM:Ob...

  • Spring和MyBatis整合笔记

    对于 Spring 框架和 MyBatis 框架整合,摸爬滚打总结了这篇笔记,MyBatis 用 XML 文件替代...

网友评论

      本文标题:MyBatis 笔记

      本文链接:https://www.haomeiwen.com/subject/bxljvftx.html