逆向代理:在数据表结构的基础上,自动生成对应的Pojo.java,Mapper.java和Mapper.xml文件。
实现步骤:
1.在pom.xml中添加dependence
1 <!--mybatis.generator--> 2 <dependency> 3 <groupId>org.mybatis.generator</groupId> 4 <artifactId>mybatis-generator-core</artifactId> 5 <version>1.3.5</version> 6 </dependency>
2.在util包下创建OverIsMergeablePlugin.java
MybatisGenerator插件是Mybatis官方提供的,这个插件存在一个固有的Bug,即当第一次生成了Mapper.xml之后,再次运行会导致Mapper.xml生成重复内容,而影响正常的运行。故创建一插件类来解决此问题。
1 public class OverIsMergeablePlugin extends PluginAdapter { 2 @Override 3 public boolean validate(List<String> warnings) { 4 return true; 5 } 6 7 @Override 8 public boolean sqlMapGenerated(GeneratedXmlFile sqlMap, IntrospectedTable introspectedTable) { 9 try { 10 Field field = sqlMap.getClass().getDeclaredField("isMergeable"); 11 field.setAccessible(true); 12 field.setBoolean(sqlMap, false); 13 } catch (Exception e) { 14 e.printStackTrace(); 15 } 16 return true; 17 } 18 }
3.在resouces下创建generatorConfig.xml文件
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 <generatorConfiguration> 6 7 <context id="DB2Tables" targetRuntime="MyBatis3"> 8 <!--避免生成重复代码的插件--> 9 <plugin type="com.juzileon.util.OverIsMergeablePlugin"/> 10 11 <!--是否在代码中显示注释--> 12 <commentGenerator> 13 <property name="suppressDate" value="true"/> 14 <property name="suppressAllComments" value="true"/> 15 </commentGenerator> 16 17 <!--数据库链接地址账号密码--> 18 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/yourdatabase" 19 userId="root" password="root"> 20 </jdbcConnection> 21 22 <javaTypeResolver> 23 <property name="forceBigDecimals" value="false"/> 24 </javaTypeResolver> 25 26 <!--生成pojo类存放位置--> 27 <javaModelGenerator targetPackage="com.juzileon.pojo" targetProject="src/main/java"> 28 <property name="enableSubPackages" value="true"/> 29 <property name="trimStrings" value="true"/> 30 </javaModelGenerator> 31 32 <!--生成xml映射文件存放位置--> 33 <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> 34 <property name="enableSubPackages" value="true"/> 35 </sqlMapGenerator> 36 37 <!--生成mapper类存放位置--> 38 <javaClientGenerator type="XMLMAPPER" targetPackage="com.juzileon.mapper" targetProject="src/main/java"> 39 <property name="enableSubPackages" value="true"/> 40 </javaClientGenerator> 41 42 <!--生成对应表及类名--> 43 <table tableName="user_" domainObjectName="User" enableCountByExample="false" 44 enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="true" 45 selectByExampleQueryId="false"> 46 <property name="my.isgen.usekeys" value="true"/> 47 <property name="useActualColumnNames" value="true"/> 48 <generatedKey column="id" sqlStatement="JDBC"/> 49 </table> 50 51 </context> 52 </generatorConfiguration>
4.生成MybatisGenerator
1 public class MybatisGenerator { 2 3 public static void main(String[] args) throws Exception { 4 String today = "2018-05-23"; 5 6 SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd"); 7 Date now =sdf.parse(today); 8 Date d = new Date(); 9 10 if(d.getTime()>now.getTime()+1000*60*60*24){ 11 System.err.println("——————未成成功运行——————"); 12 System.err.println("——————未成成功运行——————"); 13 System.err.println("本程序具有破坏作用,应该只运行一次,如果必须要再运行,需要修改today变量为今天,如:" + sdf.format(new Date())); 14 return; 15 } 16 17 if(false) 18 return; 19 List<String> warnings = new ArrayList<String>(); 20 boolean overwrite = true; 21 InputStream is= MybatisGenerator.class.getClassLoader().getResource("generatorConfig.xml").openStream(); 22 ConfigurationParser cp = new ConfigurationParser(warnings); 23 Configuration config = cp.parseConfiguration(is); 24 is.close(); 25 DefaultShellCallback callback = new DefaultShellCallback(overwrite); 26 MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); 27 myBatisGenerator.generate(null); 28 29 System.out.println("生成代码成功,只能执行一次,以后执行会覆盖掉mapper,pojo,xml 等文件上做的修改"); 30 31 } 32 }
5.运行MybatisGenerator
执行会覆盖掉mapper,pojo,xml 等文件上做的修改。必须把today变量修改为今天才可以执行,以免不小心运行了MybatisGenerator 导致原有代码被覆盖。