转载自:http://ljhzzyx.blog.163.com/blog/static/38380312201311133927394/
mybatis代码生成工具mybatis-generator调用有好几种方式:
1. 使用eclipse插件,在下面地址安装
http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
然后编写生成配置文件generatorConfig.xml,eclipse中在这个文件上右键,就有Generate Mybatis/iBatis Artifacts,由此就可以生成代码了。generatorConfig.xml文件详细内容之后有示例,这里先不贴出来,使用eclipse插件的方式有个不同就是targetProject的配置是项目名开头的,比如其他配置是targetProject="src/main/java",在这里就要是targetProject="项目名/src/main/java"。这里为什么要加上项目名,可能是因为eclipse插件是针对ide的,需要指定ide内是哪个项目。
2. maven
使用mybatis-generator的maven插件,在这里http://mybatis.org/generator/running/runningWithMaven.html有说明。先添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.3</version>
</dependency>
在plugins里面添加
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
</dependencies>
<configuration>
<configurationFile>${basedir}/src/main/resources/config/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
<!--<configuration>
<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
<jdbcURL>jdbc:mysql://localhost:3306/test</jdbcURL>
<jdbcUserId>test</jdbcUserId>
<jdbcPassword>test</jdbcPassword>
</configuration>-->
</execution>
</executions>
</plugin>
里面注意在execution里面的configuration配置是注释掉的,网上好多例子都是这样写的,但我测试时写在这里是不生效的,而直接放在plugin内是可以的。
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="mysqlTables" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" />
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test" userId="test" password="test"/>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<javaModelGenerator targetPackage="com.xxx.mybatis.entity" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper.com.xxx.mybatis" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.xxx.mybatis.dao" targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="users">
<columnOverride column="userid" javaType="java.lang.Long"/>
</table>
</context>
</generatorConfiguration>
然后使用mvn mybatis-generator:generate运行成功执行。或者可以指定参数运行mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate。
详细参数见官方文档Parameter Reference那段。
3. 直接运行jar
为了简单起见,我把jar文件跟生成配置文件拷贝到一起mybatis-generator-core-1.3.2.jar和generatorConfig.xml,这里的xml文件要加上jdbs驱动的路径,在<generatorConfiguration>内部加上
<classPathEntry location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.24\mysql-connector-java-5.1.24.jar"/>
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
这样也可正常生成。
这里有段使用生成代码的小例子,需要mybatis的配置文件MapperConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/test" />
<property name="username" value="test" />
<property name="password" value="test" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/com/xxx/mybatis/UsersMapper.xml"/>
</mappers>
</configuration>
这里的mapper内的属性,netbeans会自动提示resources,应该是resource。坑爹的是,这里写s结尾的,报错却说mapper应该配置属性resources,然后删除s配置resource才不会报错。在下载的mybatis-generator-config_1_0.dtd文件可以看到正确的定义。然后java调用:
Reader reader = Resources.getResourceAsReader("config/MapperConfig.xml");
SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlMapper.openSession();
UsersExample example = new UsersExample();
example.createCriteria().andPasswordIsNotNull();
try {
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
List<Users> users = mapper.selectByExample(example);
for (Users u : users)
System.out.println(u.getUsername() + " -- " + u.getPassword());
} finally {
sqlSession.close();
}
4. 自行编写代码
官方有说明http://mybatis.org/generator/running/runningWithJava.html
直接上代码
...
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
...
List<String> warnings = new ArrayList<String>();
final boolean overwrite = true;
File configFile = new File("src/main/resources/config/generatorConfig.xml");
//System.out.println("config fiel is in : " + configFile.getAbsoluteFile());
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration configuration = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator mybatisGenerator = new MyBatisGenerator(configuration, callback, warnings);
mybatisGenerator.generate(null);
spring集成mybatis等,以后再补充吧:
http://www.cnblogs.com/huanmieuroshui/archive/2012/12/18/2822754.html
http://www.cnblogs.com/zemliu/archive/2013/08/07/3242966.html
附:
mybatis-generator使用Maven Plugin管理和生成代码