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>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/pinyougoudb" userId="root"
password="123456">
</jdbcConnection>
<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg"
userId="yycg"
password="yycg">
</jdbcConnection> -->
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO类的位置 -->
<javaModelGenerator targetPackage="com.pinyougou.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.pinyougou.mapper"
targetProject=".\resource">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.pinyougou.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定数据库表 -->
<table schema="" tableName="tb_address"></table>
<table schema="" tableName="tb_areas"></table>
<table schema="" tableName="tb_brand"></table>
<table schema="" tableName="tb_cities"></table>
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_freight_template"></table>
<table schema="" tableName="tb_goods"></table>
<table schema="" tableName="tb_goods_desc"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_item_spec_option"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_pay_log"></table>
<table schema="" tableName="tb_provinces"></table>
<table schema="" tableName="tb_seller"></table>
<table schema="" tableName="tb_specification"></table>
<table schema="" tableName="tb_specification_option"></table>
<table schema="" tableName="tb_type_template"></table>
<table schema="" tableName="tb_user"></table>
<!-- 有些表的字段需要指定java类型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
生成工具
import java.io.File;
import java.io.IOException;
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.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
public class GeneratorSqlmap {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
生成后的代码结构
将生成的实体类实现序列化接口
,用于HTTP
传输。然后将生成的包拷贝到对应的模块(工程):
将com.pinyougou.pojo包拷贝到pojo工程
将com.pinyougou.mapper包和resouce下的com.pinyougou.mapper文件夹拷贝到dao工程
生成后的代码示例(以t_user表为例)
TbUserMapper.java
package com.pinyougou.mapper;
import com.pinyougou.pojo.TbUser;
import com.pinyougou.pojo.TbUserExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface TbUserMapper {
int countByExample(TbUserExample example);
int deleteByExample(TbUserExample example);
int deleteByPrimaryKey(Long id);
int insert(TbUser record);
int insertSelective(TbUser record);
List<TbUser> selectByExample(TbUserExample example);
TbUser selectByPrimaryKey(Long id);
int updateByExampleSelective(@Param("record") TbUser record, @Param("example") TbUserExample example);
int updateByExample(@Param("record") TbUser record, @Param("example") TbUserExample example);
int updateByPrimaryKeySelective(TbUser record);
int updateByPrimaryKey(TbUser record);
}
TbUserMapper.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.pinyougou.mapper.TbUserMapper" >
<resultMap id="BaseResultMap" type="com.pinyougou.pojo.TbUser" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="created" property="created" jdbcType="TIMESTAMP" />
<result column="updated" property="updated" jdbcType="TIMESTAMP" />
<result column="source_type" property="sourceType" jdbcType="VARCHAR" />
<result column="nick_name" property="nickName" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="VARCHAR" />
<result column="head_pic" property="headPic" jdbcType="VARCHAR" />
<result column="qq" property="qq" jdbcType="VARCHAR" />
<result column="account_balance" property="accountBalance" jdbcType="DECIMAL" />
<result column="is_mobile_check" property="isMobileCheck" jdbcType="VARCHAR" />
<result column="is_email_check" property="isEmailCheck" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<result column="user_level" property="userLevel" jdbcType="INTEGER" />
<result column="points" property="points" jdbcType="INTEGER" />
<result column="experience_value" property="experienceValue" jdbcType="INTEGER" />
<result column="birthday" property="birthday" jdbcType="TIMESTAMP" />
<result column="last_login_time" property="lastLoginTime" 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" >
id, username, password, phone, email, created, updated, source_type, nick_name, name,
status, head_pic, qq, account_balance, is_mobile_check, is_email_check, sex, user_level,
points, experience_value, birthday, last_login_time
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="com.pinyougou.pojo.TbUserExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from tb_user
<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.Long" >
select
<include refid="Base_Column_List" />
from tb_user
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from tb_user
where id = #{id,jdbcType=BIGINT}
</delete>
<delete id="deleteByExample" parameterType="com.pinyougou.pojo.TbUserExample" >
delete from tb_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.pinyougou.pojo.TbUser" >
insert into tb_user (id, username, password,
phone, email, created,
updated, source_type, nick_name,
name, status, head_pic,
qq, account_balance, is_mobile_check,
is_email_check, sex, user_level,
points, experience_value, birthday,
last_login_time)
values (#{id,jdbcType=BIGINT}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{created,jdbcType=TIMESTAMP},
#{updated,jdbcType=TIMESTAMP}, #{sourceType,jdbcType=VARCHAR}, #{nickName,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, #{headPic,jdbcType=VARCHAR},
#{qq,jdbcType=VARCHAR}, #{accountBalance,jdbcType=DECIMAL}, #{isMobileCheck,jdbcType=VARCHAR},
#{isEmailCheck,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{userLevel,jdbcType=INTEGER},
#{points,jdbcType=INTEGER}, #{experienceValue,jdbcType=INTEGER}, #{birthday,jdbcType=TIMESTAMP},
#{lastLoginTime,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.pinyougou.pojo.TbUser" >
insert into tb_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="username != null" >
username,
</if>
<if test="password != null" >
password,
</if>
<if test="phone != null" >
phone,
</if>
<if test="email != null" >
email,
</if>
<if test="created != null" >
created,
</if>
<if test="updated != null" >
updated,
</if>
<if test="sourceType != null" >
source_type,
</if>
<if test="nickName != null" >
nick_name,
</if>
<if test="name != null" >
name,
</if>
<if test="status != null" >
status,
</if>
<if test="headPic != null" >
head_pic,
</if>
<if test="qq != null" >
qq,
</if>
<if test="accountBalance != null" >
account_balance,
</if>
<if test="isMobileCheck != null" >
is_mobile_check,
</if>
<if test="isEmailCheck != null" >
is_email_check,
</if>
<if test="sex != null" >
sex,
</if>
<if test="userLevel != null" >
user_level,
</if>
<if test="points != null" >
points,
</if>
<if test="experienceValue != null" >
experience_value,
</if>
<if test="birthday != null" >
birthday,
</if>
<if test="lastLoginTime != null" >
last_login_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
#{phone,jdbcType=VARCHAR},
</if>
<if test="email != null" >
#{email,jdbcType=VARCHAR},
</if>
<if test="created != null" >
#{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null" >
#{updated,jdbcType=TIMESTAMP},
</if>
<if test="sourceType != null" >
#{sourceType,jdbcType=VARCHAR},
</if>
<if test="nickName != null" >
#{nickName,jdbcType=VARCHAR},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="status != null" >
#{status,jdbcType=VARCHAR},
</if>
<if test="headPic != null" >
#{headPic,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
#{qq,jdbcType=VARCHAR},
</if>
<if test="accountBalance != null" >
#{accountBalance,jdbcType=DECIMAL},
</if>
<if test="isMobileCheck != null" >
#{isMobileCheck,jdbcType=VARCHAR},
</if>
<if test="isEmailCheck != null" >
#{isEmailCheck,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
<if test="userLevel != null" >
#{userLevel,jdbcType=INTEGER},
</if>
<if test="points != null" >
#{points,jdbcType=INTEGER},
</if>
<if test="experienceValue != null" >
#{experienceValue,jdbcType=INTEGER},
</if>
<if test="birthday != null" >
#{birthday,jdbcType=TIMESTAMP},
</if>
<if test="lastLoginTime != null" >
#{lastLoginTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.pinyougou.pojo.TbUserExample" resultType="java.lang.Integer" >
select count(*) from tb_user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update tb_user
<set >
<if test="record.id != null" >
id = #{record.id,jdbcType=BIGINT},
</if>
<if test="record.username != null" >
username = #{record.username,jdbcType=VARCHAR},
</if>
<if test="record.password != null" >
password = #{record.password,jdbcType=VARCHAR},
</if>
<if test="record.phone != null" >
phone = #{record.phone,jdbcType=VARCHAR},
</if>
<if test="record.email != null" >
email = #{record.email,jdbcType=VARCHAR},
</if>
<if test="record.created != null" >
created = #{record.created,jdbcType=TIMESTAMP},
</if>
<if test="record.updated != null" >
updated = #{record.updated,jdbcType=TIMESTAMP},
</if>
<if test="record.sourceType != null" >
source_type = #{record.sourceType,jdbcType=VARCHAR},
</if>
<if test="record.nickName != null" >
nick_name = #{record.nickName,jdbcType=VARCHAR},
</if>
<if test="record.name != null" >
name = #{record.name,jdbcType=VARCHAR},
</if>
<if test="record.status != null" >
status = #{record.status,jdbcType=VARCHAR},
</if>
<if test="record.headPic != null" >
head_pic = #{record.headPic,jdbcType=VARCHAR},
</if>
<if test="record.qq != null" >
qq = #{record.qq,jdbcType=VARCHAR},
</if>
<if test="record.accountBalance != null" >
account_balance = #{record.accountBalance,jdbcType=DECIMAL},
</if>
<if test="record.isMobileCheck != null" >
is_mobile_check = #{record.isMobileCheck,jdbcType=VARCHAR},
</if>
<if test="record.isEmailCheck != null" >
is_email_check = #{record.isEmailCheck,jdbcType=VARCHAR},
</if>
<if test="record.sex != null" >
sex = #{record.sex,jdbcType=VARCHAR},
</if>
<if test="record.userLevel != null" >
user_level = #{record.userLevel,jdbcType=INTEGER},
</if>
<if test="record.points != null" >
points = #{record.points,jdbcType=INTEGER},
</if>
<if test="record.experienceValue != null" >
experience_value = #{record.experienceValue,jdbcType=INTEGER},
</if>
<if test="record.birthday != null" >
birthday = #{record.birthday,jdbcType=TIMESTAMP},
</if>
<if test="record.lastLoginTime != null" >
last_login_time = #{record.lastLoginTime,jdbcType=TIMESTAMP},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update tb_user
set id = #{record.id,jdbcType=BIGINT},
username = #{record.username,jdbcType=VARCHAR},
password = #{record.password,jdbcType=VARCHAR},
phone = #{record.phone,jdbcType=VARCHAR},
email = #{record.email,jdbcType=VARCHAR},
created = #{record.created,jdbcType=TIMESTAMP},
updated = #{record.updated,jdbcType=TIMESTAMP},
source_type = #{record.sourceType,jdbcType=VARCHAR},
nick_name = #{record.nickName,jdbcType=VARCHAR},
name = #{record.name,jdbcType=VARCHAR},
status = #{record.status,jdbcType=VARCHAR},
head_pic = #{record.headPic,jdbcType=VARCHAR},
qq = #{record.qq,jdbcType=VARCHAR},
account_balance = #{record.accountBalance,jdbcType=DECIMAL},
is_mobile_check = #{record.isMobileCheck,jdbcType=VARCHAR},
is_email_check = #{record.isEmailCheck,jdbcType=VARCHAR},
sex = #{record.sex,jdbcType=VARCHAR},
user_level = #{record.userLevel,jdbcType=INTEGER},
points = #{record.points,jdbcType=INTEGER},
experience_value = #{record.experienceValue,jdbcType=INTEGER},
birthday = #{record.birthday,jdbcType=TIMESTAMP},
last_login_time = #{record.lastLoginTime,jdbcType=TIMESTAMP}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.pinyougou.pojo.TbUser" >
update tb_user
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="email != null" >
email = #{email,jdbcType=VARCHAR},
</if>
<if test="created != null" >
created = #{created,jdbcType=TIMESTAMP},
</if>
<if test="updated != null" >
updated = #{updated,jdbcType=TIMESTAMP},
</if>
<if test="sourceType != null" >
source_type = #{sourceType,jdbcType=VARCHAR},
</if>
<if test="nickName != null" >
nick_name = #{nickName,jdbcType=VARCHAR},
</if>
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="status != null" >
status = #{status,jdbcType=VARCHAR},
</if>
<if test="headPic != null" >
head_pic = #{headPic,jdbcType=VARCHAR},
</if>
<if test="qq != null" >
qq = #{qq,jdbcType=VARCHAR},
</if>
<if test="accountBalance != null" >
account_balance = #{accountBalance,jdbcType=DECIMAL},
</if>
<if test="isMobileCheck != null" >
is_mobile_check = #{isMobileCheck,jdbcType=VARCHAR},
</if>
<if test="isEmailCheck != null" >
is_email_check = #{isEmailCheck,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="userLevel != null" >
user_level = #{userLevel,jdbcType=INTEGER},
</if>
<if test="points != null" >
points = #{points,jdbcType=INTEGER},
</if>
<if test="experienceValue != null" >
experience_value = #{experienceValue,jdbcType=INTEGER},
</if>
<if test="birthday != null" >
birthday = #{birthday,jdbcType=TIMESTAMP},
</if>
<if test="lastLoginTime != null" >
last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.pinyougou.pojo.TbUser" >
update tb_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
created = #{created,jdbcType=TIMESTAMP},
updated = #{updated,jdbcType=TIMESTAMP},
source_type = #{sourceType,jdbcType=VARCHAR},
nick_name = #{nickName,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
status = #{status,jdbcType=VARCHAR},
head_pic = #{headPic,jdbcType=VARCHAR},
qq = #{qq,jdbcType=VARCHAR},
account_balance = #{accountBalance,jdbcType=DECIMAL},
is_mobile_check = #{isMobileCheck,jdbcType=VARCHAR},
is_email_check = #{isEmailCheck,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
user_level = #{userLevel,jdbcType=INTEGER},
points = #{points,jdbcType=INTEGER},
experience_value = #{experienceValue,jdbcType=INTEGER},
birthday = #{birthday,jdbcType=TIMESTAMP},
last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>