Maven项目创建mybatis generator需要注意事项:
1.参考资料中说的
<dependency> <groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.3.2</version>
</dependency> |
这个没必要使用,使用了会一直下载不到jar 报错
2.
mysql的mysql-connector-java的jar如果是使用6.0.5版本会报错,建议降低版本到5.1.39(本地mysql server是5.7.18)
3.
generatorConfig.xml这个文件需要配对targetProject路径
4.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.mybatis.test</groupId> <artifactId>test-mybatis-generator</artifactId> <version>1.0-SNAPSHOT</version>
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency>
</dependencies>
<build> <finalName>xxx</finalName> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> </project> |
5.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> <!-- 指定数据连接驱动jar地址 --> <classPathEntry location="/Users/mysql-connector-java/5.1.39/mysql-connector-java-5.1.39.jar" /> <!-- 一个数据库一个context --> <context id="context" defaultModelType="flat" targetRuntime="MyBatis3"> <!-- 注释 --> <commentGenerator> <property name="suppressAllComments" value="false" /><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳 --> </commentGenerator> <!-- jdbc连接 --> <!--jdbc:mysql://xxxxxxx:8406/CL_DEMO?useUnicode=true&characterEncoding=UTF-8--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/cms?useUnicode=true&characterEncoding=UTF-8" userId="root" password="newpass" />
<!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver>
<!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.cd.dao.pojo" targetProject="src/main/java"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true" /> </javaModelGenerator>
<!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="com.cd.dao.mapper" targetProject="src/main/java"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator>
<!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.cd.dao.mapper" targetProject="src/main/java" type="XMLMAPPER"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </javaClientGenerator>
<table schema="cms" tableName="application_permission" domainObjectName="ApplicationPermission" selectByExampleQueryId="false" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false" enableDeleteByPrimaryKey="false" />
</context>
</generatorConfiguration> |
6.参考资料:
http://www.tuicool.com/articles/NBnUr2E
http://www.cnblogs.com/smileberry/p/4145872.html