使用Mybatis generator帮助Mybatis生成配置和映射
下载安装Mybatis Generator后,在Eclipse中,File>>New>>Other...,输入MyBatis,出现如下选项,说明安装成功。
点击Next>
点击Finish ,得到如下页面:
-
<?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=
"context1" >
-
<jdbcConnection driverClass=
"???" connectionURL=
"???" userId=
"???" password=
"???" />
-
<javaModelGenerator targetPackage=
"???" targetProject=
"???" />
-
<sqlMapGenerator targetPackage=
"???" targetProject=
"???" />
-
<javaClientGenerator targetPackage=
"???" targetProject=
"???" type=
"XMLMAPPER" />
-
<table schema=
"???" tableName=
"???" >
-
<columnOverride column=
"???" property=
"???" />
-
</table>
-
</context>
-
</generatorConfiguration>
以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=
"mysql-connector-java-5.1.45-bin.jar包的存放路径"/>
-
<context id=
"context1" >
-
<jdbcConnection driverClass=
"com.mysql.jdbc.Driver" connectionURL=
"jdbc:mysql://localhost:端口号/数据库名" userId=
"root" password=
"数据库密码" />
-
<javaModelGenerator targetPackage=
"包名1" targetProject=
"项目名" />
-
<sqlMapGenerator targetPackage=
"包名2" targetProject=
"项目名" />
-
<javaClientGenerator targetPackage=
"包名2" targetProject=
"项目名" type=
"XMLMAPPER" />
-
<table schema=
"数据库名" tableName=
"表名" >
-
<columnOverride column=
"列名" property=
"属性名" />
-
<columnOverride column=
"列名" property=
"属性名" />
-
<columnOverride column=
"列名" property=
"属性名" />
-
</table>
-
</context>
-
</generatorConfiguration>
编辑好配置后,在GeneratorConfig.xml上右键:选择Generate MyBatis/iBATIS Artifacts。
即可得到相应的包、类和映射文件。