technology-integration(三)---使用My
2018-08-23 本文已影响896人
海苔胖胖
Mybatis-Generator是什么
Mybatis-Generator是一款根据xml配置文件自动生成数据库对应的Java Bean(实体类)和Mybatis所需的Mapper接口以及Mapper.xml文件,这能很大程度上减少使用Mybatis框架的繁琐操作。
使用Mybatis-Generator生成相关文件
在项目根目录下你可以找到一个名为mybatis-genrator的文件夹,将其下载下来,导入到你所创建项目的根目录(或者你电脑的固定路径也可以)。(可以在码云项目根目录下的SQL文件夹找到数据库脚本,导入到MySQL数据库中)
在这里我们可以看到总共包含了四个文件,
- generatorConfig.xml:这里面配置着数据库的连接信息,以及生成实体类、Mapper接口、Mapper.xml文件的相关设置
- mybatis-generator-core-1.3.5.jar:生成文件的核心jar包
- mysql-connector-java-5.146.jar:MySQL数据库的相关驱动,如果你使用的不是MySQL数据库只需要将驱动jar包换成对应数据库的驱动即可。
- readme.txt:我会告诉你这个是专门用来复制命令的文件?
generatorConfig.xml
<generatorConfiguration>
<!--指定特定数据库的jdbc驱动jar包的位置-->
<classPathEntry location="mysql-connector-java-5.1.46.jar"/>
<context id="default" targetRuntime="MyBatis3">
<!-- optional,旨在创建class时,对注释进行控制 -->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的数据库连接 -->
<jdbcConnection
driverClass="com.mysql.jdbc.Driver"
<!--修改成对应的数据库连接-->
connectionURL="jdbc:mysql://127.0.0.1:3306/technology-integration"
userId="root"
password="123456">
</jdbcConnection>
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
targetPackage 指定生成的model生成所在的包名
targetProject 指定在该项目下所在的路径
-->
<javaModelGenerator targetPackage="com.viu.technology.po"
targetProject="../src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="true"/>
<!-- 是否对model添加 构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否对类CHAR类型的列的数据进行trim操作 -->
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
<sqlMapGenerator targetPackage="mapping"
targetProject="../src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
-->
<javaClientGenerator targetPackage="com.viu.technology.mapper"
targetProject="../src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--数据库表,需要工具生成哪个数据库表相关的文件就写多少次table
tableName---表名
domainObjectName---实体类类名
-->
<table tableName="t_product" domainObjectName="Product"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="t_message" domainObjectName="Message"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="t_order" domainObjectName="Order"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="t_product_statis" domainObjectName="ProductStatis"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="t_role" domainObjectName="Role"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="t_user" domainObjectName="User"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
修改好generatorConfig.xml后,用cmd命令打开mybatis-generator文件夹的目录(可以直接在idea中使用alt+f12打开Terminal,相对来说更加方便),然后打开readme.txt把里面的命令复制到命令行中运行即可,命令 java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite,看到命令行中有successfully代表执行成功
image.png
刷新一下项目,可以看到项目结构中多处红框中的packpage,以及xml配置所需要生成的对应内容
image.png
结束
mybatis-generator生成器的使用虽然比较简单,但每次创建新项目都需要修改generatorConfig.xml文件还是略微有点麻烦的。温馨提示,Spring JPA可以全自动生成,只需要在application.yml配置文件中配置一下即可。