用過Mybatis的人確定知道Mybatis開發的三劍客:通用Mapper、分頁pageHelper、文件生成Generator。Generator能夠根據物理表生成對應實體、Dao層、Xml文件,極大提升開發效率。現將我的近日用到的配置文件貼出以記錄。java
<?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> <!-- 指定數據鏈接驅動jar地址 --> <!-- <classPathEntry location="/Users/xubin/.m2/repository/mysql/mysql-connector-java-5.1.41.jar" /> --> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <!-- 生成的Java文件的編碼 --> <property name="javaFileEncoding" value="UTF-8" /> <!-- 格式化java代碼 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" /> <!-- 格式化XML代碼 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" /> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="com.minivision.util.IMapper" /> <!-- caseSensitive默認false,當數據庫表名區分大小寫時,能夠將該屬性設置爲true --> <property name="caseSensitive" value="true" /> </plugin> <!-- 註釋 --> <commentGenerator> <!-- 是否取消註釋 --> <property name="suppressAllComments" value="false" /> <!-- 是否生成註釋代時間戳 --> <property name="suppressDate" value="true" /> </commentGenerator> <!-- jdbc鏈接 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useSSL=true" userId="root" password="123456"> </jdbcConnection> <!-- 生成實體類地址 --> <javaModelGenerator targetPackage="com.test.entity" targetProject="./src/main/java"> <!-- 繼承父類 --> <!-- <property name="rootClass" value="com.test.common.base.BaseEntity"/> --> <property name="enableSubPackages" value="false" /> <!-- 是否針對string類型的字段在set的時候進行trim調用 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成mapxml文件 --> <sqlMapGenerator targetPackage="mapper" targetProject="./src/main/resources"> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml對應client,也就是接口dao --> <javaClientGenerator targetPackage="com.test.mapper" targetProject="./src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> <!-- 實現接口 --> <!-- <property name="rootInterface" value="tk.mybatis.mapper.common.MySqlMapper"/> --> </javaClientGenerator> <!-- 配置表信息 --> <table tableName="T_USER" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- 忽略列,不生成bean 字段 <ignoreColumn column="FRED" /> --> <!-- 指定列的java數據類型 <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> --> </table> </context> </generatorConfiguration>
GenConfigMain.javamysql
package com.test; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback; public class GenConfigMain { public static void main(String[] args) { List<String> warnings = new ArrayList<String>(); boolean overwrite = true; String genCfg = "generatorConfig.xml"; File configFile = new File(GenConfigMain.class.getResource(genCfg).getFile()); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = null; try { config = cp.parseConfiguration(configFile); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = null; try { myBatisGenerator = new MyBatisGenerator(config, callback, warnings); } catch (InvalidConfigurationException e) { e.printStackTrace(); } try { myBatisGenerator.generate(null); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
須要注意的是:sql
一、在生成的Dao層文件中,若是要繼承某個接口,使用(<property name="mappers" value="com.test.util.IMapper" />),rootInterface也能夠,可是不能自動生成泛型。數據庫
二、生成的Dao層文件沒有註解,須要自行添加。api
三、在數據庫配置那段,須要加useSSL=true以去除運行時控制檯報警。mybatis