使用MyBatis Generator生成基础代码。
##添加配置
###maven配置
在pom.xml
的plugins
中添加
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>com.github.abel533</groupId>
<artifactId>mapper</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</plugin>
此处使用了网友开源的组件生成定制的Mapper
,并且为实体类添加了中文注释
###MyBatis Generator配置
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>
<!-- 如果pom的plugin中没有配置使用的驱动,这里要使用绝对路径指定驱动 -->
<!--<classPathEntry location="mysql-connector-java-5.1.34.jar"/>-->
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<property name="javaFileEncoding" value="UTF-8"/>
<plugin type="com.github.abel533.generator.MapperPlugin">
<property name="mappers" value="com.github.abel533.mapper.Mapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=UTF-8"
userId=""
password="">
</jdbcConnection>
<javaModelGenerator targetPackage="com.aa.bb.cc.model" targetProject="/src/main/java/"/>
<sqlMapGenerator targetPackage="xml" targetProject="/mybatis"/>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.aa.bb.cc.dao" targetProject="/src/main/java/"/>
<!--匹配所有表-->
<!--<table tableName="%">-->
<!--<generatedKey column="id" sqlStatement="Mysql"/>-->
<!--</table>-->
<!-- 表对应实体类重命名 -->
<table tableName="t_bill_info" domainObjectName="BillInfo"/>
</context>
</generatorConfiguration>
如果数据库主键已设置了自增长属性,可以不使用<generatedKey column="id" sqlStatement="Mysql"/>
##Plugin 二级缓存设置、序列化、Like支持、Modal层ToString重写
<plugin type="org.mybatis.generator.plugins.CachePlugin">
<property name="cache_flushInterval" value="6000000"/>
<property name="cache_eviction" value="LRU"/>
<property name="cache_size" value="1024"/>
<property name="cache_readOnly" value="false"/>
<!--<property name="cache_type" value="org.mybatis.caches.ehcache.LoggingEhcache"/>-->
</plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"/>
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
##生成文件
在项目的根目录下,使用命令mvn mybatis-generator:generate
生成文件
注:如果需要自定义生成的类注释,需要继承MyBatis Generator的org.mybatis.generator.api.CommentGenerator
,默认使用的是org.mybatis.generator.internal.DefaultCommentGenerator
,并使用maven发布后才能使用。