1、更全内容
本文聚焦 mybatis 自动生成代码的部分,SpringBoot 配置的等更多细节可以参考
SringBoot 结合 Mybatis
2、细节
Spring + Maven 环境下 Mybatis 自动生成代码配置
-
需要对应数据库的数据库驱动jar包
本文为mysql-connector-java-5.1.7-bin.jar,放置路径为C:/Users/Administrator/Desktop/mybatis-generator/mysql-connector-java-5.1.7-bin.jar -
添加 generatorConfig.xml 文件到项目中,注意路径,本文为
src/main/resources/generator/generatorConfig.xml- generatorConfig.xml 内容,涉及数据库驱动,数据库连接,Mapper,实体,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> <classPathEntry location="C:/Users/Administrator/Desktop/mybatis-generator/mysql-connector-java-5.1.7-bin.jar" /> <context id="context"> <!-- 注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/><!-- 是否取消注释 --> <property name="suppressDate" value="true" /> <!-- 是否生成注释代时间戳--> <property name="addRemarkComments" value="true"/> </commentGenerator> <!-- JDBC连接 --> <jdbcConnection connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" driverClass="com.mysql.jdbc.Driver" password="root" userId="root"></jdbcConnection> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="com.bestcxx.stu.springmybatis.model" targetProject="src/main/java"> <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] --> <property name="enableSubPackages" value="false" /> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources"> <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] --> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.bestcxx.stu.springmybatis.dao" targetProject="src/main/java" type="XMLMAPPER"> <!-- 是否在当前路径下新加一层schema,eg:fase路径cn.ffcs.test.domain", true:cn.ffcs.test.domain".[schemaName] --> <property name="enableSubPackages" value="false" /> <!--<property name="rootInterface" value="cn.acsm.farmeasy.order.core.dao.BaseDAO"/> --> </javaClientGenerator> <!-- 配置表信息,这里没生成一张表,这里需要改变一次对应表名 --> <!-- 已经生成实体类的表:--> <!-- <table tableName="表名" domainObjectName="驼峰格式实体名" module="order" enableCountByExample="false" --> <table tableName="test2" domainObjectName="Test2" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <!-- 忽略列,不生成bean 字段 <ignoreColumn column="FRED" />--> <!-- 指定列的java数据类型 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />--> </table> </context> </generatorConfiguration> -
在
pom.xml中添加 Maven 插件 配置<build> ... <plugins> ... <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> -
运行
mybatis-generator:generate,生成代码
允许一次生成多个表的对应代码,如果使用 Spring Boot 可以直接在项目上运行 Mybatis 自动生成代码的程序
完整的命令行是 mvn:mybatis-generator:generate,这是一个 Maven 命令,如果直接在 Eclipse 中运行,使用mybatis-generator:generate即可。
- 数据库 tinyint(4) 类型转 Java 中 Integer
<table tableName="demo_table" domainObjectName="DemoTabelDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="true">
<generatedKey column="id" sqlStatement="MySql" identity="true"/>
<columnOverride column="activate_status" property="activateStatus" javaType="java.lang.Integer" />
</table>
本文详细介绍如何在Spring+Maven环境下配置MyBatis自动生成代码,并提供了generatorConfig.xml的具体配置示例,包括数据库驱动、连接设置、实体类生成路径等。
1207

被折叠的 条评论
为什么被折叠?



