mybatis generator 笔记

1,引入插件

 

<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.2</version>
            <configuration>
                <configurationFile>src/main/resources/mybatis-generator/config.xml</configurationFile>
                <verbose>true</verbose>
                <overwrite>true</overwrite>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.38</version>
                </dependency>

                <dependency>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-core</artifactId>
                    <version>1.3.2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

2,config.xml

 

<?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>
    <!-- 引入配置文件 -->
    <properties resource="mybatis-generator/config.properties"/>
    <!--classPathEntry:数据库的JDBC驱动,换成你自己的驱动位置 可选 -->
    <!-- 一个数据库一个context -->
    <!--defaultModelType="flat" 大数据字段,不分表 -->
    <context id="MysqlTables" targetRuntime="MyBatis3" defaultModelType="flat">
        <property name="autoDelimitKeywords" value="true"/>
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>
        <property name="javaFileEncoding" value="utf-8"/>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>

        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>

        <!-- 注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="false"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳-->
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}"
                        password="${jdbc_password}"/>
        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成实体类地址 -->
        <javaModelGenerator targetPackage="com.alibaba.aliyun.dao.model" targetProject="${project}">
            <property name="rootClass" value="com.alibaba.aliyun.model.BaseModel"/>
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成mapxml文件 -->
        <sqlMapGenerator targetPackage="com.alibaba.aliyun.dao" targetProject="${resources}">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
      
        <table tableName="account" enableCountByExample="true" enableUpdateByExample="true"
               enableDeleteByExample="true"
               enableSelectByExample="true" selectByExampleQueryId="true">
            <property name="useActualColumnNames" value="false"/>
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
        </table>
       
    </context>
</generatorConfiguration>
 

config.properties

 

project=src/main/java
resources=src/main/resources
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://anno.coronadb.alibaba.net:3306/BSIC_APP
jdbc_user=BSIC_APP
jdbc_password=135246
 

generator 探寻

所有的方法:

 

int countByExample(DOExample example);
int deleteByExample(DOExample example);

 

 

 

 

 

1.

updateByPrimaryKeySelective会对字段进行判断再更新(如果为Null就忽略更新),如果你只想更新某一字段,可以用这个方法。

 

 

updateByPrimaryKey对你注入的字段全部更新

原理:加selective 表示对更新的model进行为空的判断

 

 

<update id="updateByPrimaryKeySelective" parameterType="com.alibaba.aliyun.dao.model.Account">

  update account
  <set>
    <if test="gmtCreate != null">
      gmt_create = #{gmtCreate,jdbcType=TIMESTAMP},
    </if>
    <if test="gmtModified != null">
      gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},
    </if>
    <if test="name != null">
      `name` = #{name,jdbcType=VARCHAR},
    </if>
    <if test="role != null">
      `role` = #{role,jdbcType=VARCHAR},
    </if>
    <if test="status != null">
      `status` = #{status,jdbcType=VARCHAR},
    </if>
    <if test="isBuc != null">
      is_buc = #{isBuc,jdbcType=VARCHAR},
    </if>
    <if test="channel != null">
      channel = #{channel,jdbcType=BIGINT},
    </if>
    <if test="creator != null">
      creator = #{creator,jdbcType=VARCHAR},
    </if>
    <if test="modifier != null">
      modifier = #{modifier,jdbcType=VARCHAR},
    </if>
    <if test="properties != null">
      properties = #{properties,jdbcType=LONGVARCHAR},
    </if>
  </set>
  where id = #{id,jdbcType=BIGINT}
</update>
2

 

updateByPrimaryKeyWithBLOBs

 

updateByExampleWithBLOBs
selectByExampleWithBLOBs

加BLOBs表示大文本字段

 

<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.alibaba.aliyun.dao.model.Account">
  <result column="properties" jdbcType="LONGVARCHAR" property="properties" />
</resultMap>

 

3,

 

<trim prefix="(" prefixOverrides="and" suffix=")">
</trim>
 

prefix:表示在trim标签内sql语句加上前缀xxx

suffix:表示在trim标签内sql语句加上后缀xxx

suffixOverrides:表示去除最后一个后缀xxx

prefixOverrides:去掉后缀

 

4.Example里面的or查询

 

ViewPsmsgconsultExample example=new ViewPsmsgconsultExample();  
ViewPsmsgconsultExample.Criteria criteria=example.createCriteria();  
criteria.andToidEqualTo(mctid);  
criteria.andStatusEqualTo("0");  
          
ViewPsmsgconsultExample.Criteria criteria2=example.createCriteria();  
criteria2.andToidEqualTo(mctid);  
criteria2.andLaststatusEqualTo("0");  
example.or(criteria2);  
psmsgconsultDao.countByExample(example);

 

 

 

5,时间倒序

example.setOrderByClause(
gmt_modified desc

);6,Like  要加%%

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值