mybatis逆向工程官方网站:http://www.mybatis.org/generator/quickstart.html
-
准备xml文件。如下generator.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="db.properties"/> <context id="mbg" targetRuntime="MyBatis3"> <jdbcConnection driverClass="${jdbc.driverClass}" connectionURL="${jdbc.url}" userId="${jdbc.user}" password="${jdbc.password}"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <javaModelGenerator targetPackage="com.lw.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="com.lw.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lw.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <table tableName="tk_dg_coupon" domainObjectName="TkDgCoupon" > </table> </context> </generatorConfiguration>
-
上面generator.xml中引入的资源文件db.properties如下:
######mysql##### jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/common jdbc.user=root jdbc.passsord=123456
-
pom文件配置如下,重点注意build标签的配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lw</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <!-- 版本信息 --> <properties> <spring.version>4.3.16.RELEASE</spring.version> <druid.version>1.1.9</druid.version> <junit.version>4.12</junit.version> <mysql.version>5.1.6</mysql.version> <mybatis.version>3.4.1</mybatis.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> </properties> <dependencies> <!-- spring相关依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <!-- ali数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <!-- mysql的依赖,此处必须放一边,不然执行maven命令时,会提示找不到com.mysql.jdbc.Driver错误 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> <configuration> <!-- xml文件的位置 --> <configurationFile>${basedir}/src/main/resources/mybatis/generator.xml</configurationFile> </configuration> </plugin> </plugins> </build> </project>
-
右键项目执行run as---->Maven build ---->写入命令mybatis-generator:generate 即可
Maven构建成功
可能出现的错误提示: Access denied for user ‘root‘@‘localhost‘ 请参看https://www.cnblogs.com/oldwei/p/9108713.html
原文地址:https://www.cnblogs.com/oldwei/p/9108708.html
时间: 2024-11-08 14:35:41