欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

maven 工程mybatis自动生成实体类

发布时间:2025/4/9 编程问答 54 豆豆
生活随笔 收集整理的这篇文章主要介绍了 maven 工程mybatis自动生成实体类 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

generatorConfig.xml

[html] view plaincopy
  • <?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

    [html] view plaincopy
  • 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文件

    [html] view plaincopy
  • <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函数

    [html] view plaincopy
  • public class GenerateTest {  
  •      public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException {  
  •         List<Stringlist= 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函数,刷新功能你会看到相关实体类

    转载于:https://www.cnblogs.com/itrena/p/9059179.html

    总结

    以上是生活随笔为你收集整理的maven 工程mybatis自动生成实体类的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。