zoukankan      html  css  js  c++  java
  • maven 工程mybatis自动生成实体类

    generatorConfig.xml

    [html] view plain copy
     
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE generatorConfiguration  
    3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
    4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
    5.   
    6. <generatorConfiguration>  
    7.     <context id="testTables" targetRuntime="MyBatis3">  
    8.         <commentGenerator>  
    9.             <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
    10.             <property name="suppressAllComments" value="true" />  
    11.         </commentGenerator>  
    12.         <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->  
    13.         <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
    14.             connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root"  
    15.             password="root">  
    16.         </jdbcConnection>  
    17.         <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和   
    18.             NUMERIC 类型解析为java.math.BigDecimal -->  
    19.         <javaTypeResolver>  
    20.             <property name="forceBigDecimals" value="false" />  
    21.         </javaTypeResolver>  
    22.   
    23.         <!-- targetProject:生成PO类的位置 -->  
    24.         <javaModelGenerator targetPackage="com.test.mybatis"  
    25.             targetProject="srcmainjava">  
    26.             <!-- enableSubPackages:是否让schema作为包的后缀 -->  
    27.             <property name="enableSubPackages" value="false" />  
    28.             <!-- 从数据库返回的值被清理前后的空格 -->  
    29.             <property name="trimStrings" value="true" />  
    30.         </javaModelGenerator>  
    31.         <!-- targetProject:mapper映射文件生成的位置 -->  
    32.         <sqlMapGenerator targetPackage="com.test.mapper"   
    33.             targetProject="srcmainjava">  
    34.             <!-- enableSubPackages:是否让schema作为包的后缀 -->  
    35.             <property name="enableSubPackages" value="false" />  
    36.         </sqlMapGenerator>  
    37.         <!-- targetPackage:mapper接口生成的位置 -->  
    38.         <javaClientGenerator type="XMLMAPPER"  
    39.             targetPackage="com.test.mapper"   
    40.             targetProject="srcmainjava">  
    41.             <!-- enableSubPackages:是否让schema作为包的后缀 -->  
    42.             <property name="enableSubPackages" value="false" />  
    43.         </javaClientGenerator>  
    44.         <!-- 指定数据库表 -->  
    45.         <table schema="" tableName="sys_user"></table>  
    46.         <table schema="" tableName="sys_role"></table>  
    47.         <table schema="" tableName="country"></table>  
    48.         <table schema="" tableName="sys_role_user"></table>  
    49.         <table schema="" tableName="sys_role_privilege"></table>  
    50.         <table schema="" tableName="sys_privilege"></table>  
    51.   
    52.     </context>  
    53. </generatorConfiguration>  

    log4j

    [html] view plain copy
     
    1. log4j.rootLogger=DEBUG, Console  
    2. #Console  
    3. log4j.appender.Console=org.apache.log4j.ConsoleAppender  
    4. log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
    5. log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
    6. log4j.logger.java.sql.ResultSet=INFO  
    7. log4j.logger.org.apache=INFO  
    8. log4j.logger.java.sql.Connection=DEBUG  
    9. log4j.logger.java.sql.Statement=DEBUG  
    10. log4j.logger.java.sql.PreparedStatement=DEBUG  

    pom文件

    [html] view plain copy
     
    1. <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">  
    2.   <modelVersion>4.0.0</modelVersion>  
    3.   <groupId>com.testmybatisgenerator</groupId>  
    4.   <artifactId>mybatisgenerator</artifactId>  
    5.   <version>0.0.1-SNAPSHOT</version>  
    6.     
    7.   <dependencies>  
    8.     <dependency>  
    9.         <groupId>log4j</groupId>  
    10.         <artifactId>log4j</artifactId>  
    11.         <version>1.2.14</version>  
    12.     </dependency>  
    13.     <dependency>  
    14.         <groupId>org.mybatis</groupId>  
    15.         <artifactId>mybatis</artifactId>  
    16.         <version>3.3.0</version>  
    17.     </dependency>  
    18.     <dependency>  
    19.         <groupId>mysql</groupId>  
    20.         <artifactId>mysql-connector-java</artifactId>  
    21.         <version>5.1.21</version>  
    22.     </dependency>  
    23.     <dependency>  
    24.     <groupId>org.mybatis.generator</groupId>  
    25.     <artifactId>mybatis-generator-core</artifactId>  
    26.     <version>1.3.3</version>  
    27.     </dependency>  
    28.   </dependencies>  
    29. </project>  

    main函数

    [html] view plain copy
     
    1. public class GenerateTest {  
    2.      public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException {  
    3.         List<Stringlist= new  ArrayList<>();  
    4.         boolean overWrite =true;  
    5.         File configFile =new File("generatorConfig.xml");  
    6.         ConfigurationParser parser = new ConfigurationParser(list);  
    7.         org.mybatis.generator.config.Configuration configuration =parser.parseConfiguration(configFile);  
    8.         DefaultShellCallback callback = new DefaultShellCallback(overWrite);  
    9.         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration,  
    10.                 callback, list);  
    11.         try {  
    12.             myBatisGenerator.generate(null);  
    13.         } catch (SQLException | InterruptedException e) {  
    14.             // TODO Auto-generated catch block  
    15.             e.printStackTrace();  
    16.         }  
    17.     }  
    18. }  

    给出工程目录截图

    主要是再config中配置你的数据库连接,当然如果你不是mysql,那么你应该换成对应的驱动,然后运行main函数,刷新功能你会看到相关实体类

  • 相关阅读:
    restful接口设计规范总结
    SSM框架中,配置数据库连接的问题
    Java通过mysql-connector-java-8.0.11连接MySQL Server 8.0遇到的几个问题
    浅谈Spring的PropertyPlaceholderConfigurer
    过滤器与拦截器的区别
    SpringMVC的拦截器(Interceptor)和过滤器(Filter)的区别与联系
    SpringMVC中使用Interceptor拦截器
    mybatis中模糊查询的使用以及一些细节问题的注意事项
    mybatis generator(MyBatis的逆向工程)
    mybatis-generator生成逆向工程两种方式
  • 原文地址:https://www.cnblogs.com/rianbowymail/p/9059191.html
Copyright © 2011-2022 走看看