记一次IDEA利用mybatis-generator自动生成dao和mapper

注:基于SpringBoot下

第一步:pom文件

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.thinkbar.Hello</groupId> <artifactId>Hello</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> </plugin> </plugins> </build></project>

第二步: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  6 <generatorConfiguration> 7 <!--数据库驱动mysql jar 这个地方要注意确保jar的位置正确,要区分在IDEA中jar在.m2中还是自定义的文件夹中--> 8 <classPathEntry 9 location="D:\repository\mysql\mysql-connector-java\5.1.45\mysql-connector-java-5.1.45.jar"/>10 <!--数据库驱动oracle jar -->11 <!--12  <classPathEntry13  location="E:\app\Acer\product\11.2.0\client_2\jdbc\lib\ojdbc5.jar"/>-->14 <context id="Tables" targetRuntime="MyBatis3">15 <!--去除注释 -->16 17 <commentGenerator>18 <property name="suppressAllComments" value="true"/>19 </commentGenerator>20 21 <!--mysql数据库连接 -->22 23 <jdbcConnection driverClass="com.mysql.jdbc.Driver"24  connectionURL="jdbc:mysql://localhost:3306/user"25  userId="root"26  password="root">27 </jdbcConnection>28 <!--oracle数据库连接 -->29 <!--30  <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"31  connectionURL="jdbc:oracle:thin:@172.16.36.23:1521/FOKF"32  userId="YCGCE" password="YCGCE">33  <property name="remarksReporting" value="true"></property>34  </jdbcConnection>-->35 36 <!--默认false Java type resolver will always use java.math.BigDecimal if37  the database column is of type DECIMAL or NUMERIC. -->38 <javaTypeResolver>39 <property name="forceBigDecimals" value="false"/>40 </javaTypeResolver>41 42 <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->43 <javaModelGenerator targetPackage="com.thinkbar"44  targetProject="E:\development_tools\javaProject\Hello\src\main\java">45 <property name="enableSubPackages" value="true"/>46 <property name="trimStrings" value="true"/>47 </javaModelGenerator>48 <!--生成SQLMAP文件 -->49 <sqlMapGenerator targetPackage="mapper"50  targetProject="E:\development_tools\javaProject\Hello\src\main\resources">51 <property name="enableSubPackages" value="false"/>52 </sqlMapGenerator>53 <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->54 <javaClientGenerator type="XMLMAPPER"55  targetPackage="com.dao"56  targetProject="E:\development_tools\javaProject\Hello\src\main\java">57 <property name="enableSubPackages" value="false"/>58 </javaClientGenerator>59 60 <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->61 <table tableName="user" domainObjectName="User"62  enableCountByExample="false" enableUpdateByExample="false"63  enableDeleteByExample="false" enableSelectByExample="false"64  selectByExampleQueryId="false"/>65 66 67 </context>68 </generatorConfiguration>

第三步:右侧插件中找到 generatorConfig双击运行即可,注意的时当报不存在时,该插件不会自动创建

技术图片

 

相关文章