IDEA通过MyBatis generator生成数据库表的对象
1. File-->New-->Module-->Maven-->Next-->输入GroupId、ArtifactId和Version-->Next-->输入Module name、Content root和Module file location-->Finish,完成创建。
2. Run/Debug Configurations --> "+"--->Maven--->输入Name、Working directory和Command line(mybatis-generator:generate)--->"OK"
3. 在resource目录下编辑generatorConfig.xml文件如下:
<!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="C:\Users\xxx\.m2\repository\mysql\mysql-connector-java\5.1.22\mysql-connector-java-5.1.22.jar"/> <!--导入属性配置 --> <context id="default" targetRuntime="MyBatis3"> <property name="javaFileEncoding" value="UTF-8"/> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:8080/union?useUnicode=true&characterEncoding=UTF-8" userId="root" password="root" /> <!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类 targetPackage 指定生成的model生成所在的包名 targetProject 指定在该项目下所在的路径 --> <javaModelGenerator targetPackage="com.guwz.union.entity" targetProject="src/main/java"> <!-- 是否对model添加 构造函数 --> <property name="enableSubPackages" value="true" /> <!-- 给Model添加一个父类 --> <!--<property name="rootClass" value="com.itfsw.base"/>--> </javaModelGenerator> <!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 --> <sqlMapGenerator targetPackage="mappings" targetProject="src/main/resources" /> <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码 type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 --> <javaClientGenerator targetPackage="com.guwz.union.dao" targetProject="src/main/java" type="XMLMAPPER"/> <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> <table tableName="table"> <generatedKey column="id" sqlStatement="MySql" identity="true"/> </table> </context> </generatorConfiguration>
4. run generator配置即可