Mybatis使用Mybatis-generator插件
首先在POM.xml文件添加架包,我这里用的是SpringBoot,所以用的也是SpringBoot架包,最少要mybatis,generator,mysql
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2 </version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> <!--最好添加一个线程池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.3</version> </dependency>
导入插件依赖包后,在plugins内配置
<!--配置mybatis自动生成代码generator--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> </dependency> </dependencies> <executions> <execution> <id>mybatis generator</id> <phase>package</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!--允许移动生成文件--> <verbose>true</verbose> <!--允许自动覆盖文件--> <overwrite>true</overwrite> <configurationFile> src/main/resources/mybatis-generator.xml </configurationFile> </configuration> </plugin>
然后在resources包下创建mybatis-generator.xml配置文件(注意名字一定一模一样,我就是把-写成了_弄了大半天)
首先在JdbcConnection的标签内配置类,url,userId,pwd等,javaModelGenerator中配置java Bean对应的路径,
sqlMapgenerator的映射路径,mybatis的mapping映射;javaClientGenerator里填写dao层的路径,最后配置数据库的表table,最好加上enableCountByExample="false" ,enableUpdateByExample="false"enableDeleteByExample="false" enableSelectByExample="false"selectByExampleQueryId="false"。
<?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> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--数据库连接地址账号密码--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/mssc" userId="root" password="111111"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--生成Model(DataObject类)存放的位置--> <javaModelGenerator targetPackage="com.dspro.dataobject" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--生成映射文件存放位置--> <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!--生成Dao类文件存放--> <!--客户端代码,生成易于使用的针对Model对象和XML配置文件的代码--> <!--type="ANNOTATEDMAPPER" 生成javaModel和基于注解的Mapper对象--> <!--type="MIXEDMAPPER",生成基于注解的javaModel和相应的Mapper对象--> <!--type="XMLMAPPER",生成SQLMAP XML文件和独立的Mapper接口--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.dspro.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!--<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >--> <!--<property name="useActualColumnNames" value="true"/>--> <!--<generatedKey column="ID" sqlStatement="DB2" identity="true" />--> <!--<columnOverride column="DATE_FIELD" property="startDate" />--> <!--<ignoreColumn column="FRED" />--> <!--<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />--> <!--</table>--> <table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> <table tableName="user_password" domainObjectName="UserPasswordDo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"
这几个配置了之后就不会产生example类,不然新版本会产生一个example文件。
上面是我自己的配置,需要官方配置xml文件的官方配置
然后(IDEA)run-->edit configurations,点击"+"添加maven,在Command line处填写
mybatis-generator:generate
命令,
点击OK,然后run。
错误:
"POM for xxx is missing, no dependency information available" The POM for org.eclipse.m2e:lifecycle-mapping:jar:1.0.0 is missing, no dependency information available
在POM里添加:
<plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <versionRange>[1.2,)</versionRange> <goals> <goal>enforce</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin>