前言
接着上篇文章,一步步自定义需要生成的自定义方法以及在mapper.xml中生成自己想要的sql文件
开始改造
更改mapper方法命名风格
mybatis-generator生成的默认方法不是我想要的,我想要的风格如下:
selectByPrimaryKey --> selectOne
updateByPrimaryKeySelective --> update
deleteByPrimaryKey -->?delete
mybatis-generator生成的mapper方法名来自于org.mybatis.generator.api.IntrospectedTable.java,找到该类中的calculateXmlAttributes方法,修改如下
protected void calculateXmlAttributes() {
---部分省略----
// 将deleteByPrimaryKey修改成 delete
// setDeleteByPrimaryKeyStatementId("deleteByPrimaryKey"); //$NON-NLS-1$
setDeleteByPrimaryKeyStatementId("delete");
// 将insertSelective修改成 insert
// setInsertSelectiveStatementId("insertSelective"); //$NON-NLS-1$
setInsertSelectiveStatementId("insert"); //$NON-NLS-1$
// 将selectByPrimaryKey修改成 selectOne
// setSelectByPrimaryKeyStatementId("selectByPrimaryKey"); //$NON-NLS-1$
setSelectByPrimaryKeyStatementId("selectOne");
// 将updateByPrimaryKeySelective修改成 update
// setUpdateByPrimaryKeySelectiveStatementId("updateByPrimaryKeySelective"); //$NON-NLS-1$
setUpdateByPrimaryKeySelectiveStatementId("update");
//$NON-NLS-1$
}
在mybaits-example工程中运行myTest类,成功生成了自己想要的命名风格方法,同时mapper.xml文件中insert标签中的id属性值也跟着变了,但是有个问题,生成的insert方法冲突了,接着改造。
public interface SysUserMapper {
int delete(String id);
int insert(SysUser row);
int insert(SysUser row);
SysUser selectOne(String id);
int update(SysUser row);
int updateByPrimaryKey(SysUser row);
}
<select id="selectOne" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_sys_user
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="delete" parameterType="java.lang.String">
delete from t_sys_user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.cloud.dao.po.SysUser">
insert into t_sys_user (id, username, password,
nickname, dep_id, pos_id
)
values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{nickname,jdbcType=VARCHAR}, #{depId,jdbcType=INTEGER}, #{posId,jdbcType=VARCHAR}
)
</insert>
<insert id="insert" parameterType="com.cloud.dao.po.SysUser">
insert into t_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="nickname != null">
nickname,
</if>
<if test="depId != null">
dep_id,
</if>
<if test="posId != null">
pos_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="nickname != null">
#{nickname,jdbcType=VARCHAR},
</if>
<if test="depId != null">
#{depId,jdbcType=INTEGER},
</if>
<if test="posId != null">
#{posId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="update" parameterType="com.cloud.dao.po.SysUser">
update t_sys_user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="nickname != null">
nickname = #{nickname,jdbcType=VARCHAR},
</if>
<if test="depId != null">
dep_id = #{depId,jdbcType=INTEGER},
</if>
<if test="posId != null">
pos_id = #{posId,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.cloud.dao.po.SysUser">
update t_sys_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
nickname = #{nickname,jdbcType=VARCHAR},
dep_id = #{depId,jdbcType=INTEGER},
pos_id = #{posId,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
?找到org.mybatis.generator.codegen.mybatis3.javamapper.JavaMapperGenerator.java中的getCompilationUnits方法,只保留我们想要生成的mapper方法
@Override
public List<CompilationUnit> getCompilationUnits() {
---省略部分代码---
// addCountByExampleMethod(interfaze);
// addDeleteByExampleMethod(interfaze);
// addDeleteByPrimaryKeyMethod(interfaze);
// addInsertMethod(interfaze);
// addInsertSelectiveMethod(interfaze);
// addSelectByExampleWithBLOBsMethod(interfaze);
// addSelectByExampleWithoutBLOBsMethod(interfaze);
// addSelectByPrimaryKeyMethod(interfaze);
// addUpdateByExampleSelectiveMethod(interfaze);
// addUpdateByExampleWithBLOBsMethod(interfaze);
// addUpdateByExampleWithoutBLOBsMethod(interfaze);
// addUpdateByPrimaryKeySelectiveMethod(interfaze);
// addUpdateByPrimaryKeyWithBLOBsMethod(interfaze);
// addUpdateByPrimaryKeyWithoutBLOBsMethod(interfaze);
addDeleteByPrimaryKeyMethod(interfaze);
addInsertSelectiveMethod(interfaze);
addUpdateByPrimaryKeySelectiveMethod(interfaze);
addSelectByPrimaryKeyMethod(interfaze);
----省略部分代码----
}
运行myTest类,生成的结果只保留了我们想要生成的方法,但是生成的sql还是有多余的,并没有随着JavaMapperGenerator.java这个类的修改而减少,接着往下改造
public interface SysUserMapper {
int delete(String id);
int insert(SysUser row);
int update(SysUser row);
SysUser selectOne(String id);
}
<select id="selectOne" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_sys_user
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="delete" parameterType="java.lang.String">
delete from t_sys_user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.cloud.dao.po.SysUser">
insert into t_sys_user (id, username, password,
nickname, dep_id, pos_id
)
values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{nickname,jdbcType=VARCHAR}, #{depId,jdbcType=INTEGER}, #{posId,jdbcType=VARCHAR}
)
</insert>
<insert id="insert" parameterType="com.cloud.dao.po.SysUser">
insert into t_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="nickname != null">
nickname,
</if>
<if test="depId != null">
dep_id,
</if>
<if test="posId != null">
pos_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="nickname != null">
#{nickname,jdbcType=VARCHAR},
</if>
<if test="depId != null">
#{depId,jdbcType=INTEGER},
</if>
<if test="posId != null">
#{posId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="update" parameterType="com.cloud.dao.po.SysUser">
update t_sys_user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="nickname != null">
nickname = #{nickname,jdbcType=VARCHAR},
</if>
<if test="depId != null">
dep_id = #{depId,jdbcType=INTEGER},
</if>
<if test="posId != null">
pos_id = #{posId,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.cloud.dao.po.SysUser">
update t_sys_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
nickname = #{nickname,jdbcType=VARCHAR},
dep_id = #{depId,jdbcType=INTEGER},
pos_id = #{posId,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
找到org.mybatis.generator.codegen.mybatis3.xmlmapper.XMLMapperGenerator.java中的getSqlMapElement方法,也只需要保留我们自己想要生成的sql(与JavaMapperGenerator.java类中保留的方法保持一致)
protected XmlElement getSqlMapElement() {
---省略部分代码---
// addResultMapWithoutBLOBsElement(answer);
// addResultMapWithBLOBsElement(answer);
// addExampleWhereClauseElement(answer);
// addMyBatis3UpdateByExampleWhereClauseElement(answer);
// addBaseColumnListElement(answer);
// addBlobColumnListElement(answer);
// addSelectByExampleWithBLOBsElement(answer);
// addSelectByExampleWithoutBLOBsElement(answer);
// addSelectByPrimaryKeyElement(answer);
// addDeleteByPrimaryKeyElement(answer);
// addDeleteByExampleElement(answer);
// addInsertElement(answer);
// addInsertSelectiveElement(answer);
// addCountByExampleElement(answer);
// addUpdateByExampleSelectiveElement(answer);
// addUpdateByExampleWithBLOBsElement(answer);
// addUpdateByExampleWithoutBLOBsElement(answer);
// addUpdateByPrimaryKeySelectiveElement(answer);
// addUpdateByPrimaryKeyWithBLOBsElement(answer);
// addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
// mapper.xml中 -> 生成resultMap 标签内容
addResultMapWithoutBLOBsElement(answer);
// mapper.xml中 -> 生成sql 标签内容
addBaseColumnListElement(answer);
// mapper.xml中 -> 生成 select标签 id = selectOne
addSelectByPrimaryKeyElement(answer);
// mapper.xml中 -> 生成 insert标签 id = insert
addInsertSelectiveElement(answer);
// mapper.xml中 -> 生成 update标签 id = update
addUpdateByPrimaryKeySelectiveElement(answer);
// mapper.xml中 -> 生成 delete标签 id = delete
addDeleteByPrimaryKeyElement(answer);
return answer;
}
重新生成mapper.xml文件后,生成的sql和mapper方法一一对应着
<select id="selectOne" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_sys_user
where id = #{id,jdbcType=VARCHAR}
</select>
<insert id="insert" parameterType="com.cloud.dao.po.SysUser">
insert into t_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="nickname != null">
nickname,
</if>
<if test="depId != null">
dep_id,
</if>
<if test="posId != null">
pos_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="nickname != null">
#{nickname,jdbcType=VARCHAR},
</if>
<if test="depId != null">
#{depId,jdbcType=INTEGER},
</if>
<if test="posId != null">
#{posId,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="update" parameterType="com.cloud.dao.po.SysUser">
update t_sys_user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="nickname != null">
nickname = #{nickname,jdbcType=VARCHAR},
</if>
<if test="depId != null">
dep_id = #{depId,jdbcType=INTEGER},
</if>
<if test="posId != null">
pos_id = #{posId,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<delete id="delete" parameterType="java.lang.String">
delete from t_sys_user
where id = #{id,jdbcType=VARCHAR}
</delete>
?结语
到这里基本的改造就完成了,我们只保留了我们需要的sql以及重新定义了方法的命名风格,下一篇扩展自定义sql,扩展流程和上面改造流程基本上是一样的
|