mybatis generator代码生成

大家好,我是IT修真院上海分院第5期学员,一枚正直善良的JAVA程序员。

今天给大家分享一下,修真院官网JAVA任务1中的深度思考,mybatis generator代码生成?

一、背景介绍

使用MyBatis Generator可以生成dao,model,mapper和xxxByExample文件。generator-plug1.3.6版本可以生成支持动态SQL的文件。1.3.5及以下只支持动态where子句。

这里用的是1.3.5,所以版本是mybatis的动态where子句。而where子句是利用xxxByExample文件。

 

二.知识剖析

首先是pom.xml文件配置。

        <plugin>
		<groupId>org.mybatis.generator</groupId>
		<artifactId>mybatis-generator-maven-plugin</artifactId>
		<version>1.3.5</version>
                <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.5</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generator Mybatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--允许移动生成的文件-->
                    <verbose>true</verbose>
                    <!--是否覆盖-->
                    <overwrite>false</overwrite>
                    <!--自动生成配置-->
                    <!--<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>-->
                </configuration>
            </plugin>

接着是mybatis generator的配置文件。配置文件默认名是generatorConfig.xml。可以指定其他名称,但是需要用上面的configurationFile元素指定。

<?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>
    <context id="DB2Tables" targetRuntime="Mybatis3">
        <!-- 注释 -->
        <commentGenerator>
            <!-- 是否生成注释代时间戳 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库连接地址、账户、密码-->
        <jdbcConnection
                driverClass="com.mysql.jdbc.Driver"
                connectionURL="jdbc:mysql://127.0.0.1:3306/testx?characterEncoding=utf-8"
                userId="root"
                password="123">
        </jdbcConnection>
        <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
         NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!--生成model类存放位置-->
        <javaModelGenerator targetPackage="com.cx.model" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--生成dao类存放地址-->
        <!--客户端代码,生成易于使用的针对Model对象和xml配置文件的代码
            type="ANNOTATEDMAPPER",生成model和基于注解的Mapper对象
            type="MIXEDMAPPER",生成基于注解的Java model 和相应的Mapper对象
            type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.cx.dao" targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <!-- 配置表信息 -->
        <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample
            是否生成 example类 -->
        <table tableName="task4_1"
               domainObjectName="Student">
        </table>
        <!--<table tableName="users"-->
               <!--domainObjectName="User2"-->
               <!--enableCountByExample="false"-->
               <!--enableUpdateByExample="false"-->
               <!--enableDeleteByExample="false"-->
               <!--enableSelectByExample="false"-->
               <!--selectByExampleQueryId="false">-->
        <!--</table>-->
    </context>
</generatorConfiguration>

启动方式:命令提示符:mvn mybatis-generator:generate

                 idea右边maven工具按钮找到plugs下面的mybatis-generator点开下拉菜单双击第一个mybatis-generator:generate.

                 还可以配置java启动类启动。

方式还有很多种。

使用方式就是看配置文件里面javaClientGenerator相关的配置文件。

两个测试方法。

添加动态where子句是利用xxxByExample类实现的。Example会管理一个Criteria 的list。每一个Criteria代表一个where子句的条件。每个条件都提供有大于,大于等于,小于,小于等于,包含于,不包含于,等于,不等于,非空,空的规范。直接调用。

生成Criteria的方式有两种:调用creatCriteria()方法或者or()方法。

两种方式有区别。但我不了解,帖一段官方解释吧。

http://www.mybatis.org/generator/generatedobjects/exampleClassUsage.html

Criteria objects can be created with the either the createCriteria method or the or method in the example class. When the first Criteria object is created with the createCriteria method it is automatically added to the list of Criteria objects - this makes it easy to write a simple where clause if you don't need to or several other clauses together. When using the or method, the Criteria class is added to the list in all instances.

Important We recommend that you only use the or method for creating Criteria classes. We believe that this method makes for more readable code.

public class StudentsMapperTest {

    @Resource
    StudentsMapper studentsMapper;

    StudentsExample studentsExample = new StudentsExample();
    @Test
    public void countByExample() {
        StudentsExample studentsExample = new StudentsExample();
        studentsExample= null;
        long number = studentsMapper.countByExample(studentsExample);
        System.out.println("t_students表中包含记录总数:"+number);
    }

    @Test
    public void deleteByExample() {
        studentsExample.createCriteria().andCreatedAtIsNull().andIdBetween(49026L, 49050L);
        System.out.println(studentsMapper.selectByExample(studentsExample));
        studentsExample.setOrderByClause("id desc");
        System.out.println(studentsMapper.selectByExample(studentsExample));
        studentsExample.or();
    }
}

三、常见问题

1.mybatis generator和mybatis-plus的区别?

四.解决方案

1.答:mybatis generato是mybatis的插件,作用的代码生成。mybatis-plus(没找到具体的开发作者)mybatis改进版,它的官方文档说是,对mybatis只做增强,不做修改。

五。编码实战

六、参考文献

MyBatis-17MyBatis代码生成器(逆向工程)MBG使用

https://blog.csdn.net/yangshangwei/article/details/80115361

http://www.mybatis.org/generator/generatedobjects/exampleClassUsage.html

8.更多讨论

1.什么是Example,什么是Criteria?

答:Example类被用来生成任何where子句。它包含一个内部静态类Criteria。这个Criteriz维护一个条件列表,这些条件会被 anded到where子句。
2.createCriteria()方法和or()方法的区别?

答:两个方法都可以用来创建Criteria对象。区别在于首个被crateCriteria方法创建的Criterier对象将自动添加到list of criteria。如果是被or()方法创建,则是添加到the list in all instance。

3.动态sql用起来有什么问题吗?

答:这里只做了一个简单的例子。因为方法名的原因,用起来比较吃力,但是如果以后公司没有代码生成的话,可以学习下。

鸣谢

感谢观看,如有出错,恳请指正

七.更多讨论

详见视频。ppt不详细。

腾讯视频

PPT

感谢大家观看

今天的分享就到这里啦,欢迎大家点赞、转发、留言、拍砖~

我的邀请码链接:http://www.jnshu.com/login/1/20535344

使用我的优惠码优惠多多!

我的邀请码:20535344

技能树.IT修真院

“我们相信人人都可以成为一个工程师,现在开始,找个师兄,带你入门,掌控自己学习的节奏,学习的路上不再迷茫”。

这里是技能树.IT修真院,成千上万的师兄在这里找到了自己的学习路线,学习透明化,成长可见化,师兄1对1免费指导。快来与我一起学习吧~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值