在使用mybatis对接数据的时候,手动写mapper,model,client会比较费时间,可以试用mybatis_gererator来生成难度比较大的mybatis配置文件.
先要用myeclipse创建java工程.
将mysql-connector-java-5.1.18.jar放到指定的目录项目,并且配置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="/opt/app-w/gitresp/testcore/src/main/webapp/WEB-INF/lib/mysql-connector-java-5.1.18.jar" /> <context id="context1"> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://ip:3306/xx?useUnicode=true&characterEncoding=UTF-8" userId="root" password="123456" /> <javaModelGenerator targetPackage="model" targetProject="testcore" /> <sqlMapGenerator targetPackage="mapper" targetProject="testcore" /> <!-- 数据库 表名 生成名 --> <javaClientGenerator targetPackage="client" targetProject="testcore" type="XMLMAPPER" /> <table schema="xx" tableName="xx" domainObjectName="xx"> </table> </context></generatorConfiguration>
然后到github上下载mybatis/generator插件,git clone https://github.com/mybatis/generator.git.并且把目录generator/eclipse/UpdateSite下面的features和plugins拷贝到myeclipse相应的目录下面.
这样就可以右击在myeclipse中的generatorConfig.xml,找到生成mybatis artifacts选项,就可以生成mapper,model,client,如下图:
如果出现错误:Unexpected error while running MyBatis Generator. Exception getting JDBC Driver
这个就说明是<classPathEntry
location="/opt/app-w/gitresp/testcore/src/main/webapp/WEB-INF/lib/mysql-connector-java-5.1.18.jar" />
这里路径有问题,配置好就可以解决!