一,MyEclipse中安装Generator
可以在MyEclipse中,选择install from site:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
二,生成各层类
在src目录下,添加生成的配置文件:
<?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="H:\mysql-connector-java-5.1.7-bin.jar" /><!--配置绝对路径 --> <!-- 配置数据源连接 --> <context id="aisSnsTables" targetRuntime="Ibatis2Java5"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/mybatis" userId="root" password=""> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- targetPackage:生成的pojo放入的包, targetProject:生成的pojo放入的项目(对应工作空间的项目) --> <javaModelGenerator targetPackage="ShuiTian.NaiLuo.po" targetProject="Shopping/src"> <property name="enableSubPackages" value="false" /> <property name="trimStrings" value="false" /> </javaModelGenerator> <!--targetPackage=生成的sql对应的包 targetProject:生成的sql接口对应的项目 --> <sqlMapGenerator targetPackage="ShuiTian.NaiLuo.mapper" targetProject="Shopping/src"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- targetPackage:生成的dao接口对应的包 targetProject:生成的dao接口对应的项目 type:映射文件类型,一般为XMLMAPPER --> <javaClientGenerator type="SPRING" targetPackage="ShuiTian.NaiLuo.service.impl" implementationPackage="ShuiTian.NaiLuo.service.impl" targetProject="Shopping/src"> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 要生成哪些表 --> <table tableName="items" domainObjectName="Items" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> </table> <table tableName="orderdetail" domainObjectName="Orderdetail" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> </table> <table tableName="orders" domainObjectName="Orders" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> </table> <table tableName="user" domainObjectName="User" enableInsert="true" enableSelectByPrimaryKey="true" enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true" enableSelectByExample="false" enableDeleteByExample="false" enableCountByExample="false" enableUpdateByExample="false"> </table> </context> </generatorConfiguration>
之后在配置文件上右击:
ok~~~~~
三,配置文件的一些注意
1,数据库的驱动包请配置成绝对路径,最好不要中文。
2,困了=_=!