自己写各种 *Mapper.xml和 *Mapper.Java,注意各种sql语句中的 id 是否匹配,xml中的namespace是否正确,很麻烦有木有?今天博客内容就是高大上的自动构建~
需要的工具包、文件
下面来介绍一下generator.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="D:\software\lib\mysql-connector-java-5.1.21.jar" /> --> <classPathEntry location="E:\mysql-connector-java-5.1.29-bin.jar" /> <context id="DB2Tables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true" /> </commentGenerator> <!-- 数据库链接URL、用户名、密码 jdbc\:mysql\://localhost\:3306/cinema--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/cinema" userId="root" password="a"> <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa"> --> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置 --> <javaModelGenerator targetPackage="com.rindy.cinema.entity" targetProject="E:\src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成的映射文件包名和位置 --> <sqlMapGenerator targetPackage="mapper" targetProject="E:\src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.rindy.cinema.mapper" targetProject="E:\src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成那些表(更改tableName和domainObjectName就可以)这里是生成两张表的映射文件,多表可自行增加,确保你的数据库中已经建好表,并且表名没有写错~ --> <table tableName="filminfo" domainObjectName="FILMINFO" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> <table tableName="filmtype" domainObjectName="FILMTYPE" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" /> </context></generatorConfiguration>
3.上图中的 src是为生成的映射文件新建的空目录,构建成功,该目录下将会有你需要的文件。
4.自动生成Mapper映射文件命令:
java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite11
5.下面来看一下我的运行结果:
6.可能有的小伙伴会因为编码格式而引发一些错误,我们来看一下下面这种情况怎么解决
改一下编码格式就可以了