目录
1.Mybatis-Generator
可以利用Mybatis-Generator来帮助我们自动生成文件:
(1)自动生成实体类
可以帮助我们针对数据库中的每张表自动生成实体类
(2)自动生成SQL映射文件
可以帮助我们针对每张表自动生成SQL配置文件,配置文件里已经定义好对于该表的增删改查的SQL以及映射
(3)自动生成接口类
可以帮助我们针对每个SQL配置文件自动生成接口类并关联
2.生成代码
1.下载Mybatis-Generator的zip压缩包
网址:https://github.com/mybatis/generator/releases
获取里面的jar包
同时获取mysql驱动jar包
2.jar包拷贝到自定义文件里
这里的src存放生成的代码
3.新建generatorConfig.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>
<classPathEntry location="E:\MybatisGenerator\mysql-connector-java-8.0.28.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
<property name="suppressDate" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3307/pss"
userId="root"
password="123456">
</jdbcConnection>
<javaModelGenerator targetPackage="entity" targetProject="E:\Gener\gener\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="E:\Gener\gener\src">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="E:\Gener\gener\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="purchase" domainObjectName="purchase"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<table tableName="sale" domainObjectName="sale"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<property name="useActualColumnNames" value="true" />
</table>
<!-- 其他表省略 -->
</context>
</generatorConfiguration>
jdbcConnection:修改自己的mysql连接
javaClientGenerator:这个换成自己需要生成的表
更改代码保存位置
4.运行命令生成代码
cmd:java -jar mybatis-generator-core-1.4.2.jar -configfile generatorConfig.xml -overwrite
去src看生成结果