maven 工程mybatis自动生成实体类
Posted rianbowymail
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven 工程mybatis自动生成实体类相关的知识,希望对你有一定的参考价值。
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>
- <context id="testTables" targetRuntime="MyBatis3">
- <commentGenerator>
- <!-- 是否去除自动生成的注释 true:是 : false:否 -->
- <property name="suppressAllComments" value="true" />
- </commentGenerator>
- <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"
- password="root">
- </jdbcConnection>
- <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
- NUMERIC 类型解析为java.math.BigDecimal -->
- <javaTypeResolver>
- <property name="forceBigDecimals" value="false" />
- </javaTypeResolver>
- <!-- targetProject:生成PO类的位置 -->
- <javaModelGenerator targetPackage="com.test.mybatis"
- targetProject="src\main\java">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- <!-- 从数据库返回的值被清理前后的空格 -->
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <!-- targetProject:mapper映射文件生成的位置 -->
- <sqlMapGenerator targetPackage="com.test.mapper"
- targetProject="src\main\java">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- </sqlMapGenerator>
- <!-- targetPackage:mapper接口生成的位置 -->
- <javaClientGenerator type="XMLMAPPER"
- targetPackage="com.test.mapper"
- targetProject="src\main\java">
- <!-- enableSubPackages:是否让schema作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- </javaClientGenerator>
- <!-- 指定数据库表 -->
- <table schema="" tableName="sys_user"></table>
- <table schema="" tableName="sys_role"></table>
- <table schema="" tableName="country"></table>
- <table schema="" tableName="sys_role_user"></table>
- <table schema="" tableName="sys_role_privilege"></table>
- <table schema="" tableName="sys_privilege"></table>
- </context>
- </generatorConfiguration>
log4j
- log4j.rootLogger=DEBUG, Console
- #Console
- log4j.appender.Console=org.apache.log4j.ConsoleAppender
- log4j.appender.Console.layout=org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
- log4j.logger.java.sql.ResultSet=INFO
- log4j.logger.org.apache=INFO
- log4j.logger.java.sql.Connection=DEBUG
- log4j.logger.java.sql.Statement=DEBUG
- log4j.logger.java.sql.PreparedStatement=DEBUG
pom文件
- <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.testmybatisgenerator</groupId>
- <artifactId>mybatisgenerator</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <dependencies>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.14</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.3.0</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.21</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-core</artifactId>
- <version>1.3.3</version>
- </dependency>
- </dependencies>
- </project>
main函数
- public class GenerateTest {
- public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException {
- List<String> list= new ArrayList<>();
- boolean overWrite =true;
- File configFile =new File("generatorConfig.xml");
- ConfigurationParser parser = new ConfigurationParser(list);
- org.mybatis.generator.config.Configuration configuration =parser.parseConfiguration(configFile);
- DefaultShellCallback callback = new DefaultShellCallback(overWrite);
- MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration,
- callback, list);
- try {
- myBatisGenerator.generate(null);
- } catch (SQLException | InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
给出工程目录截图
主要是再config中配置你的数据库连接,当然如果你不是mysql,那么你应该换成对应的驱动,然后运行main函数,刷新功能你会看到相关实体类
以上是关于maven 工程mybatis自动生成实体类的主要内容,如果未能解决你的问题,请参考以下文章