由于maybatis手动编写各类sql语句非常麻烦,所有有generator这个工具半自动的生成各种基于主键的简单的增删改查,虽然复杂查询还是需要手工编写,但是总体来说,还是减少了人工重复的工作,关于generator的配置细节,大家可以参考http://mbg.cndocs.tk/index.html和http://blog.csdn.net/isea533/article/details/42102297,这几篇文章理由比较详细的讲解。
一、下面就来具体重现下我实现的步奏。首先是基于Java的实现。
首先在http://repo1.maven.org/maven2/org/mybatis/generator/mybatis-generator-core/下载你对应的版本的jar包,导入你项目中。
在你classpath下配置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="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <commentGenerator> <property name="suppressDate" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password="123"> </jdbcConnection> <javaModelGenerator targetPackage="test.model" targetProject="D:\tianWorkPlace\mybatis1\src"> <property name="trimStrings" value="true" /> </javaModelGenerator> <sqlMapGenerator targetPackage="test.xml" targetProject="D:\tianWorkPlace\mybatis1\src" /> <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="D:\tianWorkPlace\mybatis1\src" /> <table tableName="%"> <generatedKey column="id" sqlStatement="Mysql" /> </table> </context> </generatorConfiguration>
着重要说的是进行测试的main函数体的内容,因为官方文件不是很完整和清楚,所以我这里贴出通过编译了的代码,以供参考。
List<String> warnings = new ArrayList<String>(); boolean overwrite = true; ConfigurationParser cp = new ConfigurationParser(warnings); File configFile = new File( "D:\\tianWorkPlace\\mybatis1\\src\\generatorConfig.xml"); Configuration config = null; try { config = cp.parseConfiguration(configFile); } catch (IOException | XMLParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = null; try { myBatisGenerator = new MyBatisGenerator(config, callback, warnings); } catch (InvalidConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { myBatisGenerator.generate(null); } catch (SQLException | IOException | InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
二、在Maven下建立。
首先简单提下maven,eclipse在3.6之后的版本都集成了maven工具,安装步骤就是,Apache官网下载maven的包,到本地解压,在项目的windows--》preference--》maven选项安装路径选着解压文件中conf文件的setting.xml即可。
接着在eclipse下建立maven project,建立完成后,配置pom.xml
<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>tian</groupId> <artifactId>jun</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>jun</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </plugin> </plugins> </pluginManagement> </build> </project>
最后在maven项目上右键run as 选择maven bulid,如下图输入命令
最后build success下也可以得到如Java下同样的三个文件。