使用Mybatis generator生成代码
2017-12-28 11:30
309 查看
环境信息
构建工具为Maven数据库为MySQL
准备pom.xml
<?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>my.test</groupId> <artifactId>mybatis</artifactId> <version>1.0.0</version> <build> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> </dependencies> <configuration> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> </configuration> </plugin> </plugins> </build> </project>
configurationFile可以不指定,默认就是${basedir}/src/main/resources/generatorConfig.xml
官方网址:http://www.mybatis.org/generator/running/runningWithMaven.html
准备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> <context id="servicesupport" targetRuntime="MyBatis3"> <property name="javaFileEncoding" value="UTF-8" /> <jdbcConnection connectionURL="jdbc:mysql://10.23.0.79:3306/servicesupport" driverClass="com.mysql.jdbc.Driver" password="location" userId="location" /> <javaModelGenerator targetPackage="my.test.mybatis.model" targetProject="MAVEN" /> <sqlMapGenerator targetPackage="mybatis" targetProject="MAVEN" /> <javaClientGenerator targetPackage="my.test.mybatis.persist" targetProject="MAVEN" type="XMLMAPPER" /> <table tableName="t_safe_apkrightlist_bak" domainObjectName="ApkRightlistBak"> <generatedKey column="id" sqlStatement="JDBC" identity="true" /> </table> </context> </generatorConfiguration>
官方网址:http://www.mybatis.org/generator/configreference/xmlconfig.html
在Maven工程中将targetProject设置为MAVEN有特殊含义,表示生成的代码会放在MAVEN的target目录中,且会自动生成targetPackage指定的目录结构
执行mvn mybatis-generator:generate
│ pom.xml │ ├─src │ └─main │ └─resources │ generatorConfig.xml │ └─target └─generated-sources └─mybatis-generator ├─my │ └─test │ └─mybatis │ ├─model │ │ ApkRightlistBak.java │ │ ApkRightlistBakExample.java │ │ ApkRightlistBakWithBLOBs.java │ │ │ └─persist │ ApkRightlistBakMapper.java │ └─mybatis ApkRightlistBakMapper.xml
业务代码
ApkRightlistBakMapper apkRightlistBakMapper = sqlTemplate.getMapper(ApkRightlistBakMapper.class); ApkRightlistBakExample apkRightlistBakExample = new ApkRightlistBakExample(); apkRightlistBakExample.or().andApknameEqualTo("xxx").andTypeEqualTo(1); apkRightlistBakExample.or().andApknameEqualTo("yyy").andTypeEqualTo(2); apkRightlistBakExample.setOrderByClause("id asc"); List<ApkRightlistBakWithBLOBs> apkRightlistBakWithBLOBsList = apkRightlistBakMapper.selectByExampleWithBLOBs(apkRightlistBakExample);
相关文章推荐
- 一个使用自定义命名空间的Schema文件,xml文件和castor生成的java代码的例子
- 如何高效地使用XCodeFactory自动生成的数据层代码(二)
- [How To]如何使用Wildfish进行ISeries开发---代码生成篇
- 在Eclipse中使用Hibernate插件 *******生成代码
- 使用Nhibernate.Tool.Hbm2Net来根据HBML文件生成POJO类的代码
- 使用Dali生成的代码
- 使用CODEDOM动态实现代码的生成,编译
- MyGeneration学习笔记(1) : 使用MyGeneration生成存储过程和数据访问层代码
- MySQL数据库使用Innodb数据库类型(工具生成Hibernate+Spring代码)
- 使用xsd生成代码
- 使用“代码文档对象模型”(CodeDOM) 生成源代码和可执行文件。
- C#分析数据库结构,使用XSL模板自动生成代码
- C#分析数据库结构,使用XSL模板自动生成代码
- ibatis - 自动代码生成工具abator 使用介绍(s00n原作)
- C#分析数据库结构,使用XSL模板自动生成代码
- 使用GhostDoc为代码生成注释文档
- 如何高效地使用XCodeFactory自动生成的数据层代码(一)
- 使用PowerDesigner进行代码生成(转载)
- [代码生成] 使用Java与XSLT的10条技巧
- 想完全使用代码动态生成GridView真难