Mybatis 如何自动生成bean dao xml 配置文件 generatorconfig.xml (mysql)

烈酒焚心 提交于 2020-01-28 22:55:51
1/自动生成的jar包:mybatis-generator-core-1.3.2.jar 

 2/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> 
<classPathEntry location="lib/mysql-connector-5.1.8.jar" />
<context id="MySqlTables" targetRuntime="MyBatis3">
<!--配置数据库链接-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" 
connectionURL="jdbc:mysql://localhost:3306/abcProject?characterEncoding=utf8" userId="root"
password="11112222"> 
</jdbcConnection>
<javaTypeResolver> 
<property name="forceBigDecimals" value="false" /> 
</javaTypeResolver> 
       

<!--配置实体bean-->
<javaModelGenerator targetPackage="xmlParer" 
targetProject="D:\test\xmlParer"> 
<property name="enableSubPackages" value="true" /> 
<property name="trimStrings" value="true" /> 
</javaModelGenerator> 
<!--配置实体map接口(dao)-->   
<sqlMapGenerator targetPackage="xmlParer" 
targetProject="D:\test\xmlParer"> 
<property name="enableSubPackages" value="true" /> 
</sqlMapGenerator> 
    
<!--配置实体bean的sql.xml-->   
<javaClientGenerator type="XMLMAPPER" 
targetPackage="xmlParer"
targetProject="D:\test\xmlParer">
 <property name="enableSubPackages" value="true" /> 
</javaClientGenerator>
<!--配置实体-->  
 <table tableName="abc_agency" domainObjectName="Agency">
 
</table> 
</context> 
</generatorConfiguration>


3.1/  执行命令
java -jar mybatis-generator-core-1.3.0.jar -configfile  D:\test\generatorconfig.xml -overwrite 
这样基本就ok了。


3.2 Main方法执行

 List<String> warnings = new ArrayList<String>();
  boolean overwrite = true;
  File configFile = new File("src/generatorConfig.xml");
  ConfigurationParser cp = new ConfigurationParser(warnings);
  Configuration config;
  try {
   config = cp.parseConfiguration(configFile);
   DefaultShellCallback callback = new DefaultShellCallback(overwrite);
   MyBatisGenerator myBatisGenerator;
   try {
    myBatisGenerator = new MyBatisGenerator(config, callback,
      warnings);
    myBatisGenerator.generate(null);
   } catch (Exception e) {
    e.printStackTrace();
   }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!