码迷,mamicode.com
首页 > 其他好文 > 详细

maven 工程mybatis自动生成实体类

时间:2018-05-19 10:42:11      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:csdn   use   html   logger   lex   mysq   sdn   data   技术分享   

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="src\main\java">  
  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="src\main\java">  
  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="src\main\java">  
  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函数,刷新功能你会看到相关实体类

maven 工程mybatis自动生成实体类

标签:csdn   use   html   logger   lex   mysq   sdn   data   技术分享   

原文地址:https://www.cnblogs.com/zhengguangaa/p/9059205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!