MybatisGenerator(MyBatis逆向工程)
1)什么是MyBatis逆向工程
MyBatis Generator简称(MBG)是一个可以用来生成Mybatis dao,entity,Mapper文件的一个工具,在项目的过程中可以省去很多重复的工作,我们只要在MyBatis Generator的配置文件中配置好要生成的表名与包名即可。
官网: http://mybatis.org/generator/
2) MybatisGenerator使用步骤
-
idea添加 mybatis-generator 插件
-
引入工程
这里使用的是maven包管理工具,在pom.xml添加以下配置,以引入mybatis.generator
<!-- mybatis-generator自动生成代码插件 --> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> <!-- MySql --> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> <scope>runtime</scope> </dependency> </dependencies>
-
创建database.properties配置文件
jdbc.driverLocation=D://Maven//LocalWarehouse//mysql//mysql-connector-java//5.1.28//mysql-connector-java-5.1.28.jar driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8 user=root password=root
注意:database.properties里面的jdbc.driverLocation指向是你本地maven库对应mysql-connector地址
-
编写 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> <!--导入属性配置--> <properties resource="database.properties"></properties> <!--指定特定数据库的jdbc驱动jar包的位置(绝对路径)--> <classPathEntry location="${jdbc.driverLocation}"/> <context id="testTable" targetRuntime="MyBatis3"> <commentGenerator> <!--是否去除自动生成的注释 true是:false 否--> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接--> <jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${user}" password="${password}"></jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--targetPackage目标包,生成实体类的位置--> <javaModelGenerator targetPackage="com.st.entity" targetProject="src/main/java"> <!--enableSubPackages,是否让schema作为包的后缀--> <property name="enableSubPackages" value="false"/> <!--从数据库返回的值被清除前后空格--> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--targetProject:mapper映射文件生成的位置--> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false"></property> </sqlMapGenerator> <!--targetPackage:mapper接口生成的位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.st.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!--指定数据库表,要和数据库中进行对应,否则将会出错--> <!--有几个表就要写几个table--> <table tableName="classes" domainObjectName="classes" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="student" domainObjectName="student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>