java 逆向工程
正向工程:通过java类的设置来生成对应的数据库表,正向;
1.使用maven导入依赖
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <resources> <resource> <directory>${basedir}/src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
2.mybatis-config.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> <!--加载资源文件--> <properties resource="generator.properties"></properties> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!--是否去除自动生成的注释 true是:false 否--> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接--> <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.userId}" password="${jdbc.pwd}"></jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--targetPackage目标包,生成实体类的位置--> <javaModelGenerator targetPackage="com.smbms.pojo" targetProject="src/main/java"> <!--enableSubPackages,是否让schema作为包的后缀--> <property name="enableSubPackages" value="false"/> <!--从数据库返回的值被清除前后空格--> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--targetProject:mapper映射文件生成的位置--> <sqlMapGenerator targetPackage="com.smbms.dao" targetProject="./src/main/java"> <property name="enableSubPackages" value="false"></property> </sqlMapGenerator> <!--targetPackage:mapper接口生成的位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.smbms.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--指定数据库表,要和数据库中进行对应,否则将会出错--> <table tableName="smbms_user" domainObjectName="User"></table> <table tableName="smbms_role" domainObjectName="Role"></table> <table tableName="smbms_bill" domainObjectName="Bill"></table> <table tableName="smbms_address" domainObjectName="Address"></table> <table tableName="smbms_provider" domainObjectName="Provider"></table> </context> </generatorConfiguration>
3.创建入口
import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class GeneratorTest { public void testGenerator() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException { List<String> warnings=new ArrayList<String>(); boolean overWriter=true; File configFile=new File(GeneratorTest.class.getResource("mybatis-config.xml").getFile()); ConfigurationParser cp=new ConfigurationParser(warnings); Configuration config=cp.parseConfiguration(configFile); DefaultShellCallback callback=new DefaultShellCallback(overWriter); MyBatisGenerator myBatisGenerator=new MyBatisGenerator(config,callback,warnings); myBatisGenerator.generate(null); } public static void main(String[] args)throws Exception { GeneratorTest generatorTest=new GeneratorTest(); generatorTest.testGenerator(); } }
4.运行结果