idea中使用MyBatis Generator逆向工程生成代码

一、创建一个mmall_learning,运行如下sql语句

CREATE TABLE `mmall_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户表id',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `password` varchar(50) NOT NULL COMMENT '用户密码,MD5加密',
  `email` varchar(50) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `question` varchar(100) DEFAULT NULL COMMENT '找回密码问题',
  `answer` varchar(100) DEFAULT NULL COMMENT '找回密码答案',
  `role` int(4) NOT NULL COMMENT '角色0-管理员,1-普通用户',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '最后一次更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_name_unique` (`username`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

二、在pom.xml文件下的子标签后添加如下代码

    <plugins>
      <plugin>
        <groupId>org.mybatis.generatorgroupId>
        <artifactId>mybatis-generator-maven-pluginartifactId>
        <version>1.3.2version>
        <dependencies>
          <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
          dependency>
          <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.6version>
          dependency>
          <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
          dependency>
        dependencies>
        <configuration>
          <verbose>trueverbose>
          <overwrite>trueoverwrite>
        configuration>
      plugin>
    plugins>

三、在src/main下创建java,将其改成sources root文件夹,然后在该文件夹下创建几个包,包名分别是com.mybatis.generator.model , com.mybatis.generator.mapper , com.mybatis.generator.dao
四、在src/main/resources下创建generator.properties文件,内容如下

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://192.168.9.248:3306/mmall_learning
jdbc.userId=root
jdbc.password=Mysql@123456

五、在src/main/resources下创建generatorConfig.xml文件,内容如下



<generatorConfiguration>

    
    <properties resource="generator.properties">properties>
    
    
    
    <context id="context" targetRuntime="MyBatis3">
        
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        commentGenerator>

        
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.userId}"
                password="${jdbc.password}"/>
        
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        javaTypeResolver>
        
        
        <javaModelGenerator targetPackage="com.mybatis.generator.model" targetProject="src/main/java">
            
            <property name="enableSubPackages" value="false"/>
            
            <property name="constructorBased" value="true"/>
            
            <property name="trimStrings" value="true"/>
            
            <property name="immutable" value="false"/>
        javaModelGenerator>

        
        
        <sqlMapGenerator targetPackage="com.mybatis.generator.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        sqlMapGenerator>

        
        
        <javaClientGenerator targetPackage="com.mybatis.generator.dao" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        javaClientGenerator>

        
        <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
    context>
generatorConfiguration>

六、点击右侧的maven projects 下的mybatis-generator 运行即可

你可能感兴趣的:(idea中使用MyBatis Generator逆向工程生成代码)