插件准备:
链接:http://pan.baidu.com/s/1bpHwgt5 密码:u7in
数据库(mysql)表准备:
CREATE TABLE `role` (
`id` int(11) NOT NULL auto_increment,
`rolename` varchar(20) default NULL,
`note` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
创建项目MyBatis
创建相关的包
- com.yan.po
- com.yan.dao
- com.yan.mapper
创建资源包
- src/main/resources
生成generatorConfig.xml
- 右击项目
- new
- Other
- MyBatis Generator Configuration File
- Next
- Location中填写存放的位置:/MyBatis/src/main/resources
- Finish
配置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 >
<!-- 数据库驱动包位置 -->
<classPathEntry location="F:\Maven\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar"/>
<context id="context1" >
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mysql" userId="root" password="mysql" />
<!-- 生成模型的包名和位置 -->
<javaModelGenerator targetPackage="com.yan.po" targetProject="MyBatis/src/main/java" />
<!-- 生成的映射文件报名和位置 -->
<sqlMapGenerator targetPackage="com.yan.mapper" targetProject="MyBatis/src/main/java" />
<!-- 生成DAO的包名和位置 -->
<javaClientGenerator targetPackage="com.yan.dao" targetProject="MyBatis/src/main/java" type="XMLMAPPER" />
<!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) -->
<table schema="mysql" tableName="role" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<columnOverride column="id" property="id" />
<columnOverride column="rolename" property="roleName" />
<columnOverride column="note" property="note"/>
</table>
</context>
</generatorConfiguration>
生成文件
右击generatorConfig.xml,选择Generator MyBatis/IBATIS Artifacts