用Mybatis自动生成代码
2016-11-07 17:33
330 查看
所需jar包
配置generatorxml文件
生成方法
在校时的项目一般持久层框架都是用的Mybatis,最近做到一个数据属性稍多的一个项目,故查找了一下Mybatis自动生成代码的方法,现总结如下。
2.数据库连接的jar包
3.生成器的jar包
1.进入上述截图中的目录,在该目录下运行命令行窗口,执行下面语句,则可以自动生成。
2.如果是用Maven构建的项目,则在POM.xml中加入插件依赖:
然后,将generatorConfig.xml放在resources目录中,目录结构如下:
右键项目——Run As——Maven Build在goals中输入mybatis-generator:generate,然后apply——run,刷新项目,即可看到生成的代码。
注意:生成的mapper文件一般都会以“***Dao”命名,习惯上改为“***mapper”。
配置generatorxml文件
生成方法
在校时的项目一般持久层框架都是用的Mybatis,最近做到一个数据属性稍多的一个项目,故查找了一下Mybatis自动生成代码的方法,现总结如下。
1.所需jar包
1.Mybatis的jar包2.数据库连接的jar包
3.生成器的jar包
2.配置generator.xml文件
generator.xml是我们对生成规则进行配置的文件,在MySql数据库的各个表中,如果词之间用”_“或者”-“分隔,在自动生成时,后面第一个字母会自动转化成大写(驼峰)。其余的有关说明在注释中。其配置如下:<?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="D:\Code\maven3.2.5\repository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/> <context id="MySQL2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="false"/> </commentGenerator> <!--数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springmvc" userId="root" password=""></jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.cie.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.cie.model.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <!-- tableName="%" 表示对所有的表都生成对应的文件 --> <!-- <table tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> --> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="test" domainObjectName="Test" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context> </generatorConfiguration>
3.生成方法
完成配置文件之后,则根据配置文件生成相应的代码,生成的方法有两种。1.进入上述截图中的目录,在该目录下运行命令行窗口,执行下面语句,则可以自动生成。
Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml –overwrite
2.如果是用Maven构建的项目,则在POM.xml中加入插件依赖:
<!-- Mybatis自动生成实体、dao和mapper的插件 --> <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>
然后,将generatorConfig.xml放在resources目录中,目录结构如下:
右键项目——Run As——Maven Build在goals中输入mybatis-generator:generate,然后apply——run,刷新项目,即可看到生成的代码。
注意:生成的mapper文件一般都会以“***Dao”命名,习惯上改为“***mapper”。
相关文章推荐
- 使用Mybatis Generator自动生成Mybatis相关代码
- MyBatis代码自动生成
- MyBatis代码自动生成
- Mybatis 自动生成代码工具
- maven+mybatis+mybatis-generator+sql server 2005自动生成代码,加上自定义分页插件和批量插入更新插件
- MyBatis使用Generator自动生成代码
- 使用Mybatis Generator自动生成Mybatis相关代码
- Mybatis代码自动生成插件使用
- 基于eclipse的mybatis映射代码自动生成的插件http://blog.csdn.net/fu9958/article/details/7521681
- maven + MyBatis Generator 自动生成代码配置
- MyBatis使用Generator自动生成代码
- MyBatis使用Generator自动生成代码
- MyBatisGenerator 自动生成java代码(反向工具)
- Mybatis代码自动生成插件使用
- 使用Mybatis Generator自动生成Mybatis相关代码
- 使用Mybatis Generator自动生成Mybatis相关代码
- MyBatis使用Generator自动生成代码
- 使用Mybatis Generator自动生成Mybatis相关代码
- Mybatis代码自动生成工具和插件
- MyBatis 自动生成单表的Model DAO SqlMap 底层操作代码