Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,
由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成实体类、DAO接口和Mapping映射文件
1.
MyBatis Generator 插件安装好后,即可配置generatorConfig.xml文件
插件安装参见 http://write.blog.csdn.net/postedit/72850645
2.
拷贝generatorConfig.xml文件到config下
3.
<?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="D:/MyCommonApps/mysql-connector-java-5.1.18-bin.jar"/> <context id="mybatis" > <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> <!-- 使用criteria代替Examples自动生成结尾 如这里会在dao下生成MyInfoCriteria 否则默认的为MyInfoExample--> <plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin"> <property name="searchString" value="Example$" /> <property name="replaceString" value="Criteria" /> </plugin> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <commentGenerator><property name="suppressAllComments" value="true" /></commentGenerator> <!-- 数据库链接URL,用户名、密码 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/version?characterEncoding=utf8" userId="root" password="xiazhang" /> <!-- 转BigDecimal --> <javaTypeResolver > <property name="forceBigDecimals" value="true" /> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="com.biu.wifi.weekend.dao.model" targetProject="DemoProject"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="com.biu.wifi.weekend.dao.mappers" targetProject="DemoProject" > <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成dao的包名和位置 --> <javaClientGenerator targetPackage="com.biu.wifi.weekend.dao" targetProject="DemoProject" type="XMLMAPPER" > <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 表配置 --> <table schema="version" tableName="my_test" domainObjectName="MyInfo" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成example类 --> <!-- 忽略列,不生成bean 字段 --> <ignoreColumn column="name" /> <!-- 指定列的java数据类型 --> <columnOverride column="sex" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
补充:
<javaModelGenerator targetPackage="com.biu.wifi.weekend.dao.model" targetProject="DemoProject"> <property name="enableSubPackages" value="false" /> <!-- 表示自动生成类之后自动继承CoreEntity --> <property name="rootClass" value="com.biu.wifi.core.base.CoreEntity" /> <property name="trimStrings" value="true" /> </javaModelGenerator <javaClientGenerator type="XMLMAPPER" targetPackage="com.biu.wifi.weekend.dao" targetProject="DemoProject"> <property name="enableSubPackages" value="false" /> <!-- 表示自动生成类之后自动实现接口CoreDao --> <property name="rootInterface" value="com.biu.wifi.core.base.CoreDao" /> </javaClientGenerator>
4.注意
(1) 使用时哟去除xml注释,否则报错
(2) 数据库驱动要引入正确 <classPathEntry location="D:/MyCommonApps/mysql-connector-java-5.1.18-bin.jar"/>
否则报错:Project jdbc does not exist
(3) targetProject="你的项目名称" 使用相对路径
否则报错:project does not exist
5.自动生成
(1) 在项目中的该配置文件上右击,选择下图选项即可
生成的文件
(2) 使用命令在目录中生成(需要使用到的相关文件及jar包)
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可