这个是2014年2015年在mybatis大规模应用的时候自己研究出来的一个mybatis代码生成器,好久没有用了,官方也没有怎么大规模更新,毕竟好用的民间工具太多,好用的是大家的思维和sql。mybatis的强大之处在于把任何复杂的sql,包括多表关联,包括多重计算,处理之后映射到任意一个实体上,以达到查询的目的。
CREATE TABLE `sys_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`username` varchar(255) DEFAULT NULL COMMENT '账号',
`password` varchar(255) DEFAULT NULL COMMENT '密码',
`user_type` int(11) DEFAULT NULL COMMENT '用户类型',
`createtime` datetime DEFAULT NULL COMMENT '创建时间',
`lastupdatetime` datetime DEFAULT NULL COMMENT '最后更新时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
自动生成entity/XML/mapper/DAO的配置,注释我写得很详细了
<generatorConfiguration>
<classPathEntry
location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="false" />
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8"
userId="root" password="root">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="true" />
javaTypeResolver>
<javaModelGenerator targetPackage="com.tcbj.checksys.user.domain"
targetProject="D:\workspace\temp">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
javaModelGenerator>
<sqlMapGenerator targetPackage="com.tcbj.checksys.user.persistence"
targetProject="D:\workspace\temp">
<property name="enableSubPackages" value="true" />
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.tcbj.checksys.user.persistence"
targetProject="D:\workspace\temp">
<property name="enableSubPackages" value="true" />
javaClientGenerator>
<table tableName="sys_user" domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"
/>
context>
generatorConfiguration>
接下来进入到目录手工运行@mybatisGenerator.bat
如果提示“The specified target project directory D:\Workspaces\temp does not exist”是因为指定的生成目录不存在,记得要创建
提示以下这个是因为已经生成过了,所以每次都会重新覆盖,如果是加了新字段,你可以直接copy实体,然后mapper的xml自己加就可以了。
Existing file D:\workspace\temp\com\tcbj\checksys\user\domain\User.java was overwritten
Existing file D:\workspace\temp\com\tcbj\checksys\user\persistence\UserMapper.java was overwritten
<commentGenerator>
<property name="suppressAllComments" value="true" />
commentGenerator>
package com.tcbj.checksys.user.domain;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String password;
private Integer userType;
private Date createtime;
private Date lastupdatetime;
//这里对应的set、get我先删了,生成是有的,我们看下精华部分就行
}
package com.tcbj.checksys.user.persistence;
import com.tcbj.checksys.user.domain.User;
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
<mapper namespace="com.tcbj.checksys.user.persistence.UserMapper">
<resultMap id="BaseResultMap" type="com.tcbj.checksys.user.domain.User">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="user_type" jdbcType="INTEGER" property="userType" />
<result column="createtime" jdbcType="TIMESTAMP" property="createtime" />
<result column="lastupdatetime" jdbcType="TIMESTAMP" property="lastupdatetime" />
resultMap>
<sql id="Base_Column_List">
id, username, password, user_type, createtime, lastupdatetime
sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from sys_user
where id = #{id,jdbcType=INTEGER}
select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from sys_user
where id = #{id,jdbcType=INTEGER}
delete>
<insert id="insert" parameterType="com.tcbj.checksys.user.domain.User">
insert into sys_user (id, username, password,
user_type, createtime, lastupdatetime
)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{userType,jdbcType=INTEGER}, #{createtime,jdbcType=TIMESTAMP}, #{lastupdatetime,jdbcType=TIMESTAMP}
)
insert>
<insert id="insertSelective" parameterType="com.tcbj.checksys.user.domain.User">
insert into sys_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="userType != null">
user_type,
if>
<if test="createtime != null">
createtime,
if>
<if test="lastupdatetime != null">
lastupdatetime,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
if>
<if test="userType != null">
#{userType,jdbcType=INTEGER},
if>
<if test="createtime != null">
#{createtime,jdbcType=TIMESTAMP},
if>
<if test="lastupdatetime != null">
#{lastupdatetime,jdbcType=TIMESTAMP},
if>
trim>
insert>
<update id="updateByPrimaryKeySelective" parameterType="com.tcbj.checksys.user.domain.User">
update sys_user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
if>
<if test="userType != null">
user_type = #{userType,jdbcType=INTEGER},
if>
<if test="createtime != null">
createtime = #{createtime,jdbcType=TIMESTAMP},
if>
<if test="lastupdatetime != null">
lastupdatetime = #{lastupdatetime,jdbcType=TIMESTAMP},
if>
set>
where id = #{id,jdbcType=INTEGER}
update>
<update id="updateByPrimaryKey" parameterType="com.tcbj.checksys.user.domain.User">
update sys_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
user_type = #{userType,jdbcType=INTEGER},
createtime = #{createtime,jdbcType=TIMESTAMP},
lastupdatetime = #{lastupdatetime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=INTEGER}
update>
mapper>