mybatis generator是很好用的mybatis自动代码生成工具。最近公司使用maven和mybatis开发项目,手动写入一个个实体类和mapper还有xml配置文件感觉会很麻烦,使用mybatis generator只需要简单的配置就能完成我们的工作,这里简述一下开发步骤。
我们选择开发工具创建maven项目,我这里使用myeclipse开发,建议使用eclipse或者idea开发。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>mybatis-generator-basegroupId>
<artifactId>mybatis-generator-baseartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<name/>
<description/>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.glassfishgroupId>
<artifactId>javax.annotationartifactId>
<version>3.0.1version>
dependency>
<dependency>
<groupId>org.glassfishgroupId>
<artifactId>javax.ejbartifactId>
<version>3.0.1version>
dependency>
<dependency>
<groupId>org.jboss.weldgroupId>
<artifactId>weld-osgi-bundleartifactId>
<version>1.0.1-SP3version>
dependency>
<dependency>
<groupId>org.glassfishgroupId>
<artifactId>javax.servletartifactId>
<version>3.0.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.2.7version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-coreartifactId>
<version>1.3.2version>
<type>jartype>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-pluginartifactId>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.6source>
<target>1.6target>
configuration>
plugin>
plugins>
build>
project>
将配置文件配置在合适的位置就可以,但是要访问的到,我这里放置在resources文件下。
<generatorConfiguration>
<context id="prod">
<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />
<plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin" />
<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
<commentGenerator>
<property name="suppressDate" value="true" />
<property name="suppressAllComments" value="true" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/generator" userId="root"
password="123456" />
<javaModelGenerator targetPackage="com.mybatis.entity"
targetProject="src/main/java">
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="mappers" targetProject="src/main/java" />
<javaClientGenerator targetPackage="com.mybatis.mapper"
targetProject="src/main/java" type="XMLMAPPER" />
<table tableName="wx_ranking_flow" domainObjectName="WxRankingFlow">
table>
context>
generatorConfiguration>
(1)在javaModelGenerator标签下配置你需要生成的数据库实体的地址
(2)在sqlMapGenerator标签下配置mysql的xml配置文件
(3)在javaClientGenerator标签下配置mapper方法
(4)在table标签下配置数据库的表面和生成实体的表名
package com.mybatis.test;
import org.mybatis.generator.api.ShellRunner;
public class App {
public static void main(String[] args) {
args = new String[] { "-configfile", "src\\main\\resources\\mybatis-generator-config.xml", "-overwrite" };
ShellRunner.main(args);
}
}
这里要处理的就是我们的mybatis-generator-config.xml配置文件,路径一定要对应我们的存放路径。
执行main方法就可以生成对应的实体和xml配置文件了。
我们会发现我们生成了两个实体对象,一个是数据库映射对象,一个是Example对象。Example对象就是为了方便我们执行sql操作的类,可以使用Example类进行数据库的条件查询。同时mybatis-generator还帮助我们生成了sql的CRUD等操作。
package com.mybatis.mapper;
import java.util.List;
import com.mybatis.entity.WxRankingFlow;
import com.mybatis.entity.WxRankingFlowExample;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
public interface WxRankingFlowMapper {
int countByExample(WxRankingFlowExample example);
int deleteByExample(WxRankingFlowExample example);
int deleteByPrimaryKey(String rId);
int insert(WxRankingFlow record);
int insertSelective(WxRankingFlow record);
List selectByExampleWithRowbounds(WxRankingFlowExample example, RowBounds rowBounds);
List selectByExample(WxRankingFlowExample example);
WxRankingFlow selectByPrimaryKey(String rId);
int updateByExampleSelective(@Param("record") WxRankingFlow record, @Param("example") WxRankingFlowExample example);
int updateByExample(@Param("record") WxRankingFlow record, @Param("example") WxRankingFlowExample example);
int updateByPrimaryKeySelective(WxRankingFlow record);
int updateByPrimaryKey(WxRankingFlow record);
}
xml version="1.0" encoding="UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
"com.mybatis.mapper.WxRankingFlowMapper" >
"BaseResultMap" type="com.mybatis.entity.WxRankingFlow" >
"r_id" property="rId" jdbcType="VARCHAR" />
<result column="r_gift_date" property="rGiftDate" jdbcType="VARCHAR" />
<result column="r_sid" property="rSid" jdbcType="VARCHAR" />
<result column="r_card_no" property="rCardNo" jdbcType="VARCHAR" />
<result column="r_card_name" property="rCardName" jdbcType="VARCHAR" />
<result column="r_re_openid" property="rReOpenid" jdbcType="VARCHAR" />
<result column="r_number" property="rNumber" jdbcType="INTEGER" />
<result column="r_chain_code" property="rChainCode" jdbcType="VARCHAR" />
<result column="r_chain_id" property="rChainId" jdbcType="VARCHAR" />
<result column="r_chain_name" property="rChainName" jdbcType="VARCHAR" />
<result column="r_total_amount" property="rTotalAmount" jdbcType="INTEGER" />
<result column="r_total_count" property="rTotalCount" jdbcType="INTEGER" />
<result column="r_praise_count" property="rPraiseCount" jdbcType="INTEGER" />
<result column="r_create_time" property="rCreateTime" jdbcType="TIMESTAMP" />
<result column="r_update_time" property="rUpdateTime" jdbcType="TIMESTAMP" />
"Example_Where_Clause" >
"oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
"(" suffix=")" prefixOverrides="and" >
"criteria.criteria" item="criterion" >
"criterion.noValue" >
and ${criterion.condition}
"criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
"criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
"criterion.listValue" >
and ${criterion.condition}
"criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
if>
"Update_By_Example_Where_Clause" >
"example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
"(" suffix=")" prefixOverrides="and" >
"criteria.criteria" item="criterion" >
"criterion.noValue" >
and ${criterion.condition}
"criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
"criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
"criterion.listValue" >
and ${criterion.condition}
"criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
if>
"Base_Column_List" >
r_id, r_gift_date, r_sid, r_card_no, r_card_name, r_re_openid, r_number, r_chain_code,
r_chain_id, r_chain_name, r_total_amount, r_total_count, r_praise_count, r_create_time,
r_update_time
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from wx_ranking_flow
where r_id = #{rId,jdbcType=VARCHAR}
delete>
<delete id="deleteByExample" parameterType="com.mybatis.entity.WxRankingFlowExample" >
delete from wx_ranking_flow
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
if>
delete>
"insert" parameterType="com.mybatis.entity.WxRankingFlow" >
insert into wx_ranking_flow (r_id, r_gift_date, r_sid,
r_card_no, r_card_name, r_re_openid,
r_number, r_chain_code, r_chain_id,
r_chain_name, r_total_amount, r_total_count,
r_praise_count, r_create_time, r_update_time
)
values (#{rId,jdbcType=VARCHAR}, #{rGiftDate,jdbcType=VARCHAR}, #{rSid,jdbcType=VARCHAR},
#{rCardNo,jdbcType=VARCHAR}, #{rCardName,jdbcType=VARCHAR}, #{rReOpenid,jdbcType=VARCHAR},
#{rNumber,jdbcType=INTEGER}, #{rChainCode,jdbcType=VARCHAR}, #{rChainId,jdbcType=VARCHAR},
#{rChainName,jdbcType=VARCHAR}, #{rTotalAmount,jdbcType=INTEGER}, #{rTotalCount,jdbcType=INTEGER},
#{rPraiseCount,jdbcType=INTEGER}, #{rCreateTime,jdbcType=TIMESTAMP}, #{rUpdateTime,jdbcType=TIMESTAMP}
)
"insertSelective" parameterType="com.mybatis.entity.WxRankingFlow" >
insert into wx_ranking_flow
"(" suffix=")" suffixOverrides="," >
<if test="rId != null" >
r_id,
if>
<if test="rGiftDate != null" >
r_gift_date,
if>
<if test="rSid != null" >
r_sid,
if>
<if test="rCardNo != null" >
r_card_no,
if>
<if test="rCardName != null" >
r_card_name,
if>
<if test="rReOpenid != null" >
r_re_openid,
if>
<if test="rNumber != null" >
r_number,
if>
<if test="rChainCode != null" >
r_chain_code,
if>
<if test="rChainId != null" >
r_chain_id,
if>
<if test="rChainName != null" >
r_chain_name,
if>
<if test="rTotalAmount != null" >
r_total_amount,
if>
<if test="rTotalCount != null" >
r_total_count,
if>
<if test="rPraiseCount != null" >
r_praise_count,
if>
<if test="rCreateTime != null" >
r_create_time,
if>
<if test="rUpdateTime != null" >
r_update_time,
if>
"values (" suffix=")" suffixOverrides="," >
<if test="rId != null" >
#{rId,jdbcType=VARCHAR},
if>
<if test="rGiftDate != null" >
#{rGiftDate,jdbcType=VARCHAR},
if>
<if test="rSid != null" >
#{rSid,jdbcType=VARCHAR},
if>
<if test="rCardNo != null" >
#{rCardNo,jdbcType=VARCHAR},
if>
<if test="rCardName != null" >
#{rCardName,jdbcType=VARCHAR},
if>
<if test="rReOpenid != null" >
#{rReOpenid,jdbcType=VARCHAR},
if>
<if test="rNumber != null" >
#{rNumber,jdbcType=INTEGER},
if>
<if test="rChainCode != null" >
#{rChainCode,jdbcType=VARCHAR},
if>
<if test="rChainId != null" >
#{rChainId,jdbcType=VARCHAR},
if>
<if test="rChainName != null" >
#{rChainName,jdbcType=VARCHAR},
if>
<if test="rTotalAmount != null" >
#{rTotalAmount,jdbcType=INTEGER},
if>
<if test="rTotalCount != null" >
#{rTotalCount,jdbcType=INTEGER},
if>
<if test="rPraiseCount != null" >
#{rPraiseCount,jdbcType=INTEGER},
if>
<if test="rCreateTime != null" >
#{rCreateTime,jdbcType=TIMESTAMP},
if>
<if test="rUpdateTime != null" >
#{rUpdateTime,jdbcType=TIMESTAMP},
if>
"updateByExampleSelective" parameterType="map" >
update wx_ranking_flow
<set >
<if test="record.rId != null" >
r_id = #{record.rId,jdbcType=VARCHAR},
if>
<if test="record.rGiftDate != null" >
r_gift_date = #{record.rGiftDate,jdbcType=VARCHAR},
if>
<if test="record.rSid != null" >
r_sid = #{record.rSid,jdbcType=VARCHAR},
if>
<if test="record.rCardNo != null" >
r_card_no = #{record.rCardNo,jdbcType=VARCHAR},
if>
<if test="record.rCardName != null" >
r_card_name = #{record.rCardName,jdbcType=VARCHAR},
if>
<if test="record.rReOpenid != null" >
r_re_openid = #{record.rReOpenid,jdbcType=VARCHAR},
if>
<if test="record.rNumber != null" >
r_number = #{record.rNumber,jdbcType=INTEGER},
if>
<if test="record.rChainCode != null" >
r_chain_code = #{record.rChainCode,jdbcType=VARCHAR},
if>
<if test="record.rChainId != null" >
r_chain_id = #{record.rChainId,jdbcType=VARCHAR},
if>
<if test="record.rChainName != null" >
r_chain_name = #{record.rChainName,jdbcType=VARCHAR},
if>
<if test="record.rTotalAmount != null" >
r_total_amount = #{record.rTotalAmount,jdbcType=INTEGER},
if>
<if test="record.rTotalCount != null" >
r_total_count = #{record.rTotalCount,jdbcType=INTEGER},
if>
<if test="record.rPraiseCount != null" >
r_praise_count = #{record.rPraiseCount,jdbcType=INTEGER},
if>
<if test="record.rCreateTime != null" >
r_create_time = #{record.rCreateTime,jdbcType=TIMESTAMP},
if>
<if test="record.rUpdateTime != null" >
r_update_time = #{record.rUpdateTime,jdbcType=TIMESTAMP},
if>
set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
if>
"updateByExample" parameterType="map" >
update wx_ranking_flow
set r_id = #{record.rId,jdbcType=VARCHAR},
r_gift_date = #{record.rGiftDate,jdbcType=VARCHAR},
r_sid = #{record.rSid,jdbcType=VARCHAR},
r_card_no = #{record.rCardNo,jdbcType=VARCHAR},
r_card_name = #{record.rCardName,jdbcType=VARCHAR},
r_re_openid = #{record.rReOpenid,jdbcType=VARCHAR},
r_number = #{record.rNumber,jdbcType=INTEGER},
r_chain_code = #{record.rChainCode,jdbcType=VARCHAR},
r_chain_id = #{record.rChainId,jdbcType=VARCHAR},
r_chain_name = #{record.rChainName,jdbcType=VARCHAR},
r_total_amount = #{record.rTotalAmount,jdbcType=INTEGER},
r_total_count = #{record.rTotalCount,jdbcType=INTEGER},
r_praise_count = #{record.rPraiseCount,jdbcType=INTEGER},
r_create_time = #{record.rCreateTime,jdbcType=TIMESTAMP},
r_update_time = #{record.rUpdateTime,jdbcType=TIMESTAMP}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
if>
"updateByPrimaryKeySelective" parameterType="com.mybatis.entity.WxRankingFlow" >
update wx_ranking_flow
<set >
<if test="rGiftDate != null" >
r_gift_date = #{rGiftDate,jdbcType=VARCHAR},
if>
<if test="rSid != null" >
r_sid = #{rSid,jdbcType=VARCHAR},
if>
<if test="rCardNo != null" >
r_card_no = #{rCardNo,jdbcType=VARCHAR},
if>
<if test="rCardName != null" >
r_card_name = #{rCardName,jdbcType=VARCHAR},
if>
<if test="rReOpenid != null" >
r_re_openid = #{rReOpenid,jdbcType=VARCHAR},
if>
<if test="rNumber != null" >
r_number = #{rNumber,jdbcType=INTEGER},
if>
<if test="rChainCode != null" >
r_chain_code = #{rChainCode,jdbcType=VARCHAR},
if>
<if test="rChainId != null" >
r_chain_id = #{rChainId,jdbcType=VARCHAR},
if>
<if test="rChainName != null" >
r_chain_name = #{rChainName,jdbcType=VARCHAR},
if>
<if test="rTotalAmount != null" >
r_total_amount = #{rTotalAmount,jdbcType=INTEGER},
if>
<if test="rTotalCount != null" >
r_total_count = #{rTotalCount,jdbcType=INTEGER},
if>
<if test="rPraiseCount != null" >
r_praise_count = #{rPraiseCount,jdbcType=INTEGER},
if>
<if test="rCreateTime != null" >
r_create_time = #{rCreateTime,jdbcType=TIMESTAMP},
if>
<if test="rUpdateTime != null" >
r_update_time = #{rUpdateTime,jdbcType=TIMESTAMP},
if>
set>
where r_id = #{rId,jdbcType=VARCHAR}
"updateByPrimaryKey" parameterType="com.mybatis.entity.WxRankingFlow" >
update wx_ranking_flow
set r_gift_date = #{rGiftDate,jdbcType=VARCHAR},
r_sid = #{rSid,jdbcType=VARCHAR},
r_card_no = #{rCardNo,jdbcType=VARCHAR},
r_card_name = #{rCardName,jdbcType=VARCHAR},
r_re_openid = #{rReOpenid,jdbcType=VARCHAR},
r_number = #{rNumber,jdbcType=INTEGER},
r_chain_code = #{rChainCode,jdbcType=VARCHAR},
r_chain_id = #{rChainId,jdbcType=VARCHAR},
r_chain_name = #{rChainName,jdbcType=VARCHAR},
r_total_amount = #{rTotalAmount,jdbcType=INTEGER},
r_total_count = #{rTotalCount,jdbcType=INTEGER},
r_praise_count = #{rPraiseCount,jdbcType=INTEGER},
r_create_time = #{rCreateTime,jdbcType=TIMESTAMP},
r_update_time = #{rUpdateTime,jdbcType=TIMESTAMP}
where r_id = #{rId,jdbcType=VARCHAR}
mybatis/generator github
mybatis-generator 代码自动生成工具
菜鸟在MyBatis路上前行-MyBatis Generator快速生成实体类代码
Mybatis Generator配置文件介绍