Mybatis反向生成DAO、Mapper、Model
2017-12-08 21:07
316 查看
工程目录结构
现在mybatis generator 最高版本是1.3.5。我使用的是1.3.2版本。以下书生成代码目录结构:
上图是生成DAO、Mapper、Model需要的所有文件
generator : 生成出来的代码存放这个目录中
generator.xml : 用于生成表的一些核心配置文件、
mybatis-generator-core-1.3.2.jar 生成代码需要用的jar文件
mysql-connector-java-5.1.34.jar : mysql数据库驱动
下载地址:
https://github.com/mybatis/generator
官方参考:
http://www.mybatis.org/generator/
generator.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="mysql-connector-java-5.1.34.jar"/> <context id="MysqlTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否生成注释代时间戳--> <property name="suppressDate" value="true"/> <!-- 是否取消注释 --> <property name="suppressAllComments" value="true"/> <!-- 生成的Java文件的编码 --> <property name="javaFileEncoding" value="UTF-8"/> <!-- 格式化java代码 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML代码 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> </commentGenerator> <!-- jdbc连接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3308/restaurant" userId="root" password="password"></jdbcConnection> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成实体类地址 --> <javaModelGenerator targetPackage="cn.com.test.restaurant.model" targetProject="generator"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true"/> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="cn.com.test.restaurant.mapping" targetProject="generator"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.com.test.restaurant.dao" targetProject="generator"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 配置表信息 --> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <table tableName="s_log" domainObjectName="SLog" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!-- 忽略列,不生成bean 字段 <ignoreColumn column="FRED" /> --> <!-- 指定列的java数据类型 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> <table tableName="s_menu" domainObjectName="SMenu" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="s_menu_role" domainObjectName="SMenuRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="s_role" domainObjectName="SRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="s_user" domainObjectName="SUser" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="s_user_role" domainObjectName="SUserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="u_dining_table" domainObjectName="UDiningTable" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="u_integral" domainObjectName="UIntegral" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="u_order" domainObjectName="UOrder" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="u_restaurant" domainObjectName="URestaurant" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context> </generatorConfiguration>
执行命令
D:\tools\MyBatis Generator>java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite MyBatis Generator finished successfully.
如果成功会提示:MyBatis Generator finished successfully
在generator 目录中会看到生成的文件:
dao:
mapper:
model
注意
需要配置jdk启动数据库
相关文章推荐
- 整合maven+mybatis+generator生成java自定义model实体类,dao接口和mapper映射文件
- mybatis generator cmd 终端命令 生成dao model mapper
- IDEA——mybatis-generator自动生成dao/model/mapper
- MyBatis 使用Generator自动生成Model , Dao, mapper
- 使用Mybatis-Generator自动生成Dao、model和mapper.xml相关文件
- 使用Mybatis-Generator自动生成Dao、model和mapper.xml相关文件
- 使用mybatis-generator自动生成dao、model和mapper
- 使用mybatis-generator自动生成dao、model和mapper
- MyBatis自动生成mapper,dao,model文件
- 【mybatis】根据model自动生成 mapper service dao 层的工具类
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- 使用Mybatis-Generator自动生成entity实体、dao接口以及mapper映射文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
- MyBatis 根据表结构自动生成代码, domain, dao, mapper 文件
- eclipse中根据数据库自动生成dao、mapper、model
- 简单两步快速学会使用Mybatis-Generator自动生成entity实体、dao接口和简单mapper映射(用mysql和oracle举例)
- mybatis-generator 作为 gradle的一个task运行生成xml,dao,model
- Mybatis中通过generator生成mapper、Dao、mapper.xml
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)