mybatis mybatis-generator 程式碼自動生成工具
阿新 • • 發佈:2019-01-03
一、簡介
mybatis generator是很好用的mybatis自動程式碼生成工具。最近公司使用maven和mybatis開發專案,手動寫入一個個實體類和mapper還有xml配置檔案感覺會很麻煩,使用mybatis generator只需要簡單的配置就能完成我們的工作,這裡簡述一下開發步驟。
二、開發流程
2.1 建立maven專案
我們選擇開發工具建立maven專案,我這裡使用myeclipse開發,建議使用eclipse或者idea開發。
2.2 在pom配置檔案中加入依賴包
<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.0</modelVersion>
<groupId>mybatis-generator-base</groupId>
<artifactId>mybatis-generator-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name/>
<description/>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId >javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.annotation</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.ejb</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-osgi-bundle</artifactId>
<version>1.0.1-SP3</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0.1</version>
</dependency>
<!--測試框架 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- Mysql -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<!-- Mysql 依賴 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--生成程式碼外掛-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.3 新建生成程式碼的配置檔案mybatis-generator-config.xml
將配置檔案配置在合適的位置就可以,但是要訪問的到,我這裡放置在resources檔案下。
<?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>
<context id="prod">
<!-- RowBounds pagination -->
<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>
<!-- jdbc連線 -->
<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">
<!-- 是否針對string型別的欄位在set的時候進行trim呼叫 -->
<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標籤下配置資料庫的表面和生成實體的表名
2.4 新建批處理類main方法
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等操作。
WxRankingFlowMapper介面
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<WxRankingFlow> selectByExampleWithRowbounds(WxRankingFlowExample example, RowBounds rowBounds);
List<WxRankingFlow> 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);
}
WxRankingFlowMapper.xml配置檔案(這個配置檔案就是我們對應實體的,命名是資料庫名)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mybatis.mapper.WxRankingFlowMapper" >
<resultMap id="BaseResultMap" type="com.mybatis.entity.WxRankingFlow" >
<id column="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" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="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
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.mybatis.entity.WxRankingFlowExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from wx_ranking_flow
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from wx_ranking_flow
where r_id = #{rId,jdbcType=VARCHAR}
</select>
<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 id="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}
)
</insert>
<insert id="insertSelective" parameterType="com.mybatis.entity.WxRankingFlow" >
insert into wx_ranking_flow
<trim prefix="(" 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>
</trim>
<trim prefix="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>
</trim>
</insert>
<select id="countByExample" parameterType="com.mybatis.entity.WxRankingFlowExample" resultType="java.lang.Integer" >
select count(*) from wx_ranking_flow
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="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>
</update>
<update id="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>
</update>
<update id="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}
</update>
<update id="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}
</update>
<select resultMap="BaseResultMap" parameterType="com.mybatis.entity.WxRankingFlowExample" id="selectByExampleWithRowbounds" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from wx_ranking_flow
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
</mapper>