springboot下的mybatis逆向工程
1.添加jar和plugin
1.1.在pom.xml文件的添加-数据库连接和mybatis依赖:
1.2添加mybatis逆向工程插件:
2.配置逆向工程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="application.yml"/>
<!--指定数据库连接的jar包位置,也可以把jar包放在项目中引用,需要注意版本兼容问题-->
<classPathEntry location="D:/freemarker/maven/apache-maven-3.6.1/repository/mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar"/>
<context id="Mysql" targetRuntime="MyBatis3">
<!-- 注释 -->
<commentGenerator>
<!-- 是否生成注释时间戳 -->
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- JDBC连接,
driverClass:在mysql8中为com.mysql.cj.jdbc.Driver,
mysql5为com.mysql.jdbc.Driver;
connectionURL:端口号后面请指定自己的数据库名称,还有需要注意添加时区;
userId和password为数据库连接的账号,密码;-->
<jdbcConnection
driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=Asia/Shanghai"
userId="root"
password="123456">
</jdbcConnection>
<!-- 下列配置默认为false,用于数据库字段类型和java类型的强转匹配-->
<!--<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>-->
<!-- 生成实体类地址 ,如果不存在实体类的包,会自动创建-->
<javaModelGenerator targetPackage="com.redis.testredis.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成mapper xml文件 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成mapper xml对应的mapper接口文件-->
<javaClientGenerator targetPackage="com.redis.testredis.mapper" targetProject="src/main/java"
type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 配置表信息,可以配置多个表 -->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--mysql 配置-->
<generatedKey column="id" sqlStatement="Mysql"/>
<!--oracle 配置-->
<!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
</table>
<table tableName="provider" domainObjectName="Provider" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--mysql 配置-->
<generatedKey column="pid" sqlStatement="Mysql"/>
</table>
<table tableName="bill" domainObjectName="Bill" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--mysql 配置-->
<generatedKey column="bid" sqlStatement="Mysql"/>
</table>
</context>
</generatorConfiguration>
3.edit configuration 中添加一个新的configuration
依照图片的顺序进行操作:
1.点击edit configuration
2.点击加号选择 maven
3.指定项目根目录,添加命令参数:mybatis-generator:generate -e,然后保存
4.启动刚才我们添加的maven工程:
关于generatorConfig.xml中属性的作用请参考:
https://blog.net/isea533/article/details/42102297