Mybatis逆向工程配置使用
文章目录
前言
idea+mybatis+SQL server实现逆向工程
一、 Resources
添加generatorConfig.xml和jdbc.properties文件。
1.1generatorConfig.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="jdbc.properties"></properties>
<!-- 驱动路径 -->
<!-- <classPathEntry-->
<!-- location="${druid.posa-north.driverLocation}" />-->
<context id="context1">
<!-- 是否生成注释 去除自动生成的注释 -->
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库连接配置 -->
<jdbcConnection
driverClass="${driverClassName}"
connectionURL="${url}"
userId="${username}"
password="${password}" />
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="true" />
</javaTypeResolver>
<!--配置生成的实体包 targetPackage:生成的实体类类全路径,默认存放在src目录下。 targetProject:生成实体类在项目中的存放位置 -->
<javaModelGenerator targetPackage="aia.cn.posa.bean.po"
targetProject=".\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 映射文件mapper.xml位置及名称,默认存放在src目录下 -->
<sqlMapGenerator targetPackage="resources.mapper"
targetProject=".\src\main">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--生成mapper类存放位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="aia.cn.posa.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--生成对应表及类名 不可以重复生成-->
<!-- 配置表 schema:不用填写 tableName: 表名 enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:
去除自动生成的例子 -->
<table schema="" tableName="nposdinput"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false"
selectByExampleQueryId="false" enableDeleteByPrimaryKey="true"
enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" />
</context>
</generatorConfiguration>
1.2jdbc.properties文件
driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://chot3r8csts02:1433;DatabaseName=GZPOSAUTO
username=nposst20
password=wpf9vUiBYm
二、添加maven依赖
2.1pom.xml
<!-- mybatis逆向工程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>${mybatis.generator.version}</version>
</dependency>
插件引入
<!-- mybatis逆向工程插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${mybatis.generator.version}</version>
<!--需要在逆向工程中引入数据库驱动,不然不能创建-->
<dependencies>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.2.2.jre8</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
三、使用步骤
3.1配置maven启动项
3.2使用
总结
重要:启动之前查看generatorConfig.xml文件,填写要生成的表名,不可以重复生成,会报错