当前位置: 首页 > news >正文

数据库和ORM如何优雅的添加字段?

RT,业务需要为某个实体添加字段,如何在生成了Mybatis XML(包含了手写的部分联合查询)的情况下优雅的添加字段呢?

作者:方小葱
链接:https://www.zhihu.com/question/284511416/answer/438123378
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 

我非常讨厌Mybatis的其中一个原因就是因为这个~

hibernate这种生成SQL是"运行时"的;而Mybatis是"编译时"的,改动一个字段需要手动"修改"或者重新生成XML(这种需求在实际开发当中是很常见的,尤其是系统设计时间比较紧或者一些小项目,开发期间需要变动字段是很普遍的~)

因为Mybatis这种机制,我觉得目前没有很好的办法处理这个问题;

我们之前一般是将"手写的"和"生成的"分开管理~

修改字段后手写(自己管理的)自己改SQL,自动生成的重新生成一遍(可使用一些自动化工具,脚本,插件之类的);

个人觉得这种方式根本谈不上优雅(希望有人能真正给一个优雅的方式)~

总之我一直觉得Mybatis多数情况下不是一个很好地选择;

比如一些简单的小项目(比如个人博客这种)可以看看nutz这种框架;企业级管理软件之类的用hibernate(hibernate用代码直接数据库建模,然后导出DDL和文档可以省去很多工作,出来的模型约束和完整性都还不错,该加的约束和索引都能帮你加上,如果你自己设计数据库你还得考虑一遍这部分内容);银行/支付或者你认为hibernate有问题的项目(一般有问题,是因为你的开发人员hold不住,经验不够,而不是hibernate本身的问题)就用DButils,JDBCTemplate 或者自己封装一个工具;

如果你们公司很大,有专门的人帮你写SQL,检查SQL你再考虑Mybatis~

个人意见~


 

我介绍一下我司是如何做的。

思路如下:统一xml模板,使sql无论增加什么字段,最终的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="cn.kanmars.sn.dao.TblDemoInfoMapper"><resultMap id="BaseResultMap" type="cn.kanmars.sn.entity.TblDemoInfo"><constructor><idArg column="demo_id" javaType="java.lang.Integer" jdbcType="DECIMAL" /><arg column="demo_nm" javaType="java.lang.String" jdbcType="VARCHAR" /><arg column="demo_money" javaType="java.math.BigDecimal" jdbcType="DECIMAL" /><arg column="create_date" javaType="java.lang.String" jdbcType="CHAR" /><arg column="create_time" javaType="java.lang.String" jdbcType="CHAR" /><arg column="select_static" javaType="java.lang.String" jdbcType="CHAR" /><arg column="select_dynamic" javaType="java.lang.String" jdbcType="CHAR" /><arg column="radio_static" javaType="java.lang.String" jdbcType="CHAR" /><arg column="radio_dynamic" javaType="java.lang.String" jdbcType="CHAR" /><arg column="checkbox_static" javaType="java.lang.String" jdbcType="VARCHAR" /><arg column="checkbox_dynamic" javaType="java.lang.String" jdbcType="VARCHAR" /><arg column="net_address" javaType="java.lang.String" jdbcType="VARCHAR" /><arg column="count_number" javaType="java.lang.Integer" jdbcType="DECIMAL" /></constructor></resultMap><sql id="Base_Column_List">demo_id,demo_nm,demo_money,create_date,create_time,select_static,select_dynamic,radio_static,radio_dynamic,checkbox_static,checkbox_dynamic,net_address,count_number</sql><select id="select" parameterType="cn.kanmars.sn.entity.TblDemoInfo" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from tbl_demo_infowhere 1=1<if test="demoId != null and demoId != '' ">and demo_id = #{demoId,jdbcType=DECIMAL}</if><if test="demoNm != null and demoNm != '' ">and demo_nm = #{demoNm,jdbcType=VARCHAR}</if><if test="demoMoney != null and demoMoney != '' ">and demo_money = #{demoMoney,jdbcType=DECIMAL}</if><if test="createDate != null and createDate != '' ">and create_date = #{createDate,jdbcType=CHAR}</if><if test="createTime != null and createTime != '' ">and create_time = #{createTime,jdbcType=CHAR}</if><if test="selectStatic != null and selectStatic != '' ">and select_static = #{selectStatic,jdbcType=CHAR}</if><if test="selectDynamic != null and selectDynamic != '' ">and select_dynamic = #{selectDynamic,jdbcType=CHAR}</if><if test="radioStatic != null and radioStatic != '' ">and radio_static = #{radioStatic,jdbcType=CHAR}</if><if test="radioDynamic != null and radioDynamic != '' ">and radio_dynamic = #{radioDynamic,jdbcType=CHAR}</if><if test="checkboxStatic != null and checkboxStatic != '' ">and checkbox_static = #{checkboxStatic,jdbcType=VARCHAR}</if><if test="checkboxDynamic != null and checkboxDynamic != '' ">and checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR}</if><if test="netAddress != null and netAddress != '' ">and net_address = #{netAddress,jdbcType=VARCHAR}</if><if test="countNumber != null and countNumber != '' ">and count_number = #{countNumber,jdbcType=DECIMAL}</if></select><select id="selectList" parameterType="cn.kanmars.sn.entity.TblDemoInfo" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from tbl_demo_infowhere 1=1<if test="demoId != null and demoId != '' ">and demo_id = #{demoId,jdbcType=DECIMAL}</if><if test="demoNm != null and demoNm != '' ">and demo_nm = #{demoNm,jdbcType=VARCHAR}</if><if test="demoMoney != null and demoMoney != '' ">and demo_money = #{demoMoney,jdbcType=DECIMAL}</if><if test="createDate != null and createDate != '' ">and create_date = #{createDate,jdbcType=CHAR}</if><if test="createTime != null and createTime != '' ">and create_time = #{createTime,jdbcType=CHAR}</if><if test="selectStatic != null and selectStatic != '' ">and select_static = #{selectStatic,jdbcType=CHAR}</if><if test="selectDynamic != null and selectDynamic != '' ">and select_dynamic = #{selectDynamic,jdbcType=CHAR}</if><if test="radioStatic != null and radioStatic != '' ">and radio_static = #{radioStatic,jdbcType=CHAR}</if><if test="radioDynamic != null and radioDynamic != '' ">and radio_dynamic = #{radioDynamic,jdbcType=CHAR}</if><if test="checkboxStatic != null and checkboxStatic != '' ">and checkbox_static = #{checkboxStatic,jdbcType=VARCHAR}</if><if test="checkboxDynamic != null and checkboxDynamic != '' ">and checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR}</if><if test="netAddress != null and netAddress != '' ">and net_address = #{netAddress,jdbcType=VARCHAR}</if><if test="countNumber != null and countNumber != '' ">and count_number = #{countNumber,jdbcType=DECIMAL}</if></select><insert id="insert" parameterType="cn.kanmars.sn.entity.TblDemoInfo" >insert into tbl_demo_info<trim prefix="(" suffix=")" suffixOverrides=","><if test="demoId != null ">demo_id,</if><if test="demoNm != null ">demo_nm,</if><if test="demoMoney != null ">demo_money,</if><if test="createDate != null ">create_date,</if><if test="createTime != null ">create_time,</if><if test="selectStatic != null ">select_static,</if><if test="selectDynamic != null ">select_dynamic,</if><if test="radioStatic != null ">radio_static,</if><if test="radioDynamic != null ">radio_dynamic,</if><if test="checkboxStatic != null ">checkbox_static,</if><if test="checkboxDynamic != null ">checkbox_dynamic,</if><if test="netAddress != null ">net_address,</if><if test="countNumber != null ">count_number,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="demoId != null ">#{demoId,jdbcType=DECIMAL},</if><if test="demoNm != null ">#{demoNm,jdbcType=VARCHAR},</if><if test="demoMoney != null ">#{demoMoney,jdbcType=DECIMAL},</if><if test="createDate != null ">#{createDate,jdbcType=CHAR},</if><if test="createTime != null ">#{createTime,jdbcType=CHAR},</if><if test="selectStatic != null ">#{selectStatic,jdbcType=CHAR},</if><if test="selectDynamic != null ">#{selectDynamic,jdbcType=CHAR},</if><if test="radioStatic != null ">#{radioStatic,jdbcType=CHAR},</if><if test="radioDynamic != null ">#{radioDynamic,jdbcType=CHAR},</if><if test="checkboxStatic != null ">#{checkboxStatic,jdbcType=VARCHAR},</if><if test="checkboxDynamic != null ">#{checkboxDynamic,jdbcType=VARCHAR},</if><if test="netAddress != null ">#{netAddress,jdbcType=VARCHAR},</if><if test="countNumber != null ">#{countNumber,jdbcType=DECIMAL},</if></trim></insert><update id="updateByPrimaryKey" parameterType="cn.kanmars.sn.entity.TblDemoInfo" >update tbl_demo_info<set><if test="demoNm != null">demo_nm = #{demoNm,jdbcType=VARCHAR} ,</if><if test="demoMoney != null">demo_money = #{demoMoney,jdbcType=DECIMAL} ,</if><if test="createDate != null">create_date = #{createDate,jdbcType=CHAR} ,</if><if test="createTime != null">create_time = #{createTime,jdbcType=CHAR} ,</if><if test="selectStatic != null">select_static = #{selectStatic,jdbcType=CHAR} ,</if><if test="selectDynamic != null">select_dynamic = #{selectDynamic,jdbcType=CHAR} ,</if><if test="radioStatic != null">radio_static = #{radioStatic,jdbcType=CHAR} ,</if><if test="radioDynamic != null">radio_dynamic = #{radioDynamic,jdbcType=CHAR} ,</if><if test="checkboxStatic != null">checkbox_static = #{checkboxStatic,jdbcType=VARCHAR} ,</if><if test="checkboxDynamic != null">checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR} ,</if><if test="netAddress != null">net_address = #{netAddress,jdbcType=VARCHAR} ,</if><if test="countNumber != null">count_number = #{countNumber,jdbcType=DECIMAL} ,</if></set>where 1=1and demo_id = #{demoId,jdbcType=DECIMAL}</update><delete id="delete" parameterType="cn.kanmars.sn.entity.TblDemoInfo" >delete from tbl_demo_infowhere 1=1<if test="demoId != null and demoId != '' ">and demo_id = #{demoId,jdbcType=DECIMAL}</if><if test="demoNm != null and demoNm != '' ">and demo_nm = #{demoNm,jdbcType=VARCHAR}</if><if test="demoMoney != null and demoMoney != '' ">and demo_money = #{demoMoney,jdbcType=DECIMAL}</if><if test="createDate != null and createDate != '' ">and create_date = #{createDate,jdbcType=CHAR}</if><if test="createTime != null and createTime != '' ">and create_time = #{createTime,jdbcType=CHAR}</if><if test="selectStatic != null and selectStatic != '' ">and select_static = #{selectStatic,jdbcType=CHAR}</if><if test="selectDynamic != null and selectDynamic != '' ">and select_dynamic = #{selectDynamic,jdbcType=CHAR}</if><if test="radioStatic != null and radioStatic != '' ">and radio_static = #{radioStatic,jdbcType=CHAR}</if><if test="radioDynamic != null and radioDynamic != '' ">and radio_dynamic = #{radioDynamic,jdbcType=CHAR}</if><if test="checkboxStatic != null and checkboxStatic != '' ">and checkbox_static = #{checkboxStatic,jdbcType=VARCHAR}</if><if test="checkboxDynamic != null and checkboxDynamic != '' ">and checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR}</if><if test="netAddress != null and netAddress != '' ">and net_address = #{netAddress,jdbcType=VARCHAR}</if><if test="countNumber != null and countNumber != '' ">and count_number = #{countNumber,jdbcType=DECIMAL}</if></delete><select id="queryOneMap" parameterType="java.util.HashMap" resultType="java.util.HashMap">select<include refid="Base_Column_List" />from tbl_demo_infowhere 1=1<if test="demoId != null and demoId != '' ">and demo_id = #{demoId,jdbcType=DECIMAL}</if><if test="demoNm != null and demoNm != '' ">and demo_nm = #{demoNm,jdbcType=VARCHAR}</if><if test="demoMoney != null and demoMoney != '' ">and demo_money = #{demoMoney,jdbcType=DECIMAL}</if><if test="createDate != null and createDate != '' ">and create_date = #{createDate,jdbcType=CHAR}</if><if test="createTime != null and createTime != '' ">and create_time = #{createTime,jdbcType=CHAR}</if><if test="selectStatic != null and selectStatic != '' ">and select_static = #{selectStatic,jdbcType=CHAR}</if><if test="selectDynamic != null and selectDynamic != '' ">and select_dynamic = #{selectDynamic,jdbcType=CHAR}</if><if test="radioStatic != null and radioStatic != '' ">and radio_static = #{radioStatic,jdbcType=CHAR}</if><if test="radioDynamic != null and radioDynamic != '' ">and radio_dynamic = #{radioDynamic,jdbcType=CHAR}</if><if test="checkboxStatic != null and checkboxStatic != '' ">and checkbox_static = #{checkboxStatic,jdbcType=VARCHAR}</if><if test="checkboxDynamic != null and checkboxDynamic != '' ">and checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR}</if><if test="netAddress != null and netAddress != '' ">and net_address = #{netAddress,jdbcType=VARCHAR}</if><if test="countNumber != null and countNumber != '' ">and count_number = #{countNumber,jdbcType=DECIMAL}</if></select><select id="queryListMap" parameterType="java.util.HashMap" resultType="java.util.HashMap">select<include refid="Base_Column_List" />from tbl_demo_infowhere 1=1<if test="demoId != null and demoId != '' ">and demo_id = #{demoId,jdbcType=DECIMAL}</if><if test="demoNm != null and demoNm != '' ">and demo_nm = #{demoNm,jdbcType=VARCHAR}</if><if test="demoMoney != null and demoMoney != '' ">and demo_money = #{demoMoney,jdbcType=DECIMAL}</if><if test="createDate != null and createDate != '' ">and create_date = #{createDate,jdbcType=CHAR}</if><if test="createTime != null and createTime != '' ">and create_time = #{createTime,jdbcType=CHAR}</if><if test="selectStatic != null and selectStatic != '' ">and select_static = #{selectStatic,jdbcType=CHAR}</if><if test="selectDynamic != null and selectDynamic != '' ">and select_dynamic = #{selectDynamic,jdbcType=CHAR}</if><if test="radioStatic != null and radioStatic != '' ">and radio_static = #{radioStatic,jdbcType=CHAR}</if><if test="radioDynamic != null and radioDynamic != '' ">and radio_dynamic = #{radioDynamic,jdbcType=CHAR}</if><if test="checkboxStatic != null and checkboxStatic != '' ">and checkbox_static = #{checkboxStatic,jdbcType=VARCHAR}</if><if test="checkboxDynamic != null and checkboxDynamic != '' ">and checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR}</if><if test="netAddress != null and netAddress != '' ">and net_address = #{netAddress,jdbcType=VARCHAR}</if><if test="countNumber != null and countNumber != '' ">and count_number = #{countNumber,jdbcType=DECIMAL}</if></select><update id="updateCAS" parameterType="java.util.HashMap" >update tbl_demo_info<set><if test="demoNm_new != null">demo_nm = #{demoNm_new,jdbcType=VARCHAR} ,</if><if test="demoMoney_new != null">demo_money = #{demoMoney_new,jdbcType=DECIMAL} ,</if><if test="createDate_new != null">create_date = #{createDate_new,jdbcType=CHAR} ,</if><if test="createTime_new != null">create_time = #{createTime_new,jdbcType=CHAR} ,</if><if test="selectStatic_new != null">select_static = #{selectStatic_new,jdbcType=CHAR} ,</if><if test="selectDynamic_new != null">select_dynamic = #{selectDynamic_new,jdbcType=CHAR} ,</if><if test="radioStatic_new != null">radio_static = #{radioStatic_new,jdbcType=CHAR} ,</if><if test="radioDynamic_new != null">radio_dynamic = #{radioDynamic_new,jdbcType=CHAR} ,</if><if test="checkboxStatic_new != null">checkbox_static = #{checkboxStatic_new,jdbcType=VARCHAR} ,</if><if test="checkboxDynamic_new != null">checkbox_dynamic = #{checkboxDynamic_new,jdbcType=VARCHAR} ,</if><if test="netAddress_new != null">net_address = #{netAddress_new,jdbcType=VARCHAR} ,</if><if test="countNumber_new != null">count_number = #{countNumber_new,jdbcType=DECIMAL} ,</if></set>where 1=1<if test="demoId != null ">and demo_id = #{demoId,jdbcType=DECIMAL}</if><if test="demoNm != null ">and demo_nm = #{demoNm,jdbcType=VARCHAR}</if><if test="demoMoney != null ">and demo_money = #{demoMoney,jdbcType=DECIMAL}</if><if test="createDate != null ">and create_date = #{createDate,jdbcType=CHAR}</if><if test="createTime != null ">and create_time = #{createTime,jdbcType=CHAR}</if><if test="selectStatic != null ">and select_static = #{selectStatic,jdbcType=CHAR}</if><if test="selectDynamic != null ">and select_dynamic = #{selectDynamic,jdbcType=CHAR}</if><if test="radioStatic != null ">and radio_static = #{radioStatic,jdbcType=CHAR}</if><if test="radioDynamic != null ">and radio_dynamic = #{radioDynamic,jdbcType=CHAR}</if><if test="checkboxStatic != null ">and checkbox_static = #{checkboxStatic,jdbcType=VARCHAR}</if><if test="checkboxDynamic != null ">and checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR}</if><if test="netAddress != null ">and net_address = #{netAddress,jdbcType=VARCHAR}</if><if test="countNumber != null ">and count_number = #{countNumber,jdbcType=DECIMAL}</if></update><select id="queryForPage" parameterType="java.util.HashMap" resultType="java.util.HashMap"><if test="countFlag != null and countFlag == 'Y'.toString() ">select count(1) as TOTALCOUNT from (</if>select<include refid="Base_Column_List" />from tbl_demo_infowhere 1=1<if test="demoId != null and demoId != '' ">and demo_id = #{demoId,jdbcType=DECIMAL}</if><if test="demoNm != null and demoNm != '' ">and demo_nm like CONCAT(#{demoNm,jdbcType=VARCHAR},'%')</if><if test="demoMoney != null and demoMoney != '' ">and demo_money = #{demoMoney,jdbcType=DECIMAL}</if><if test="createDate != null and createDate != '' ">and create_date = #{createDate,jdbcType=CHAR}</if><if test="createDate_start != null and createDate_start != '' ">and create_date &gt;= #{createDate_start,jdbcType=CHAR}</if><if test="createDate_end != null and createDate_end != '' ">and create_date &lt;= #{createDate_end,jdbcType=CHAR}</if><if test="createTime != null and createTime != '' ">and create_time = #{createTime,jdbcType=CHAR}</if><if test="selectStatic != null and selectStatic != '' ">and select_static = #{selectStatic,jdbcType=CHAR}</if><if test="selectDynamic != null and selectDynamic != '' ">and select_dynamic = #{selectDynamic,jdbcType=CHAR}</if><if test="radioStatic != null and radioStatic != '' ">and radio_static = #{radioStatic,jdbcType=CHAR}</if><if test="radioDynamic != null and radioDynamic != '' ">and radio_dynamic = #{radioDynamic,jdbcType=CHAR}</if><if test="checkboxStatic != null and checkboxStatic != '' ">and checkbox_static = #{checkboxStatic,jdbcType=VARCHAR}</if><if test="checkboxDynamic != null and checkboxDynamic != '' ">and checkbox_dynamic = #{checkboxDynamic,jdbcType=VARCHAR}</if><if test="netAddress != null and netAddress != '' ">and net_address like CONCAT(#{netAddress,jdbcType=VARCHAR},'%')</if><if test="countNumber != null and countNumber != '' ">and count_number = #{countNumber,jdbcType=DECIMAL}</if><if test="countFlag != null and countFlag == 'N'.toString() ">limit #{limitStart,jdbcType=DECIMAL},#{limitSize,jdbcType=DECIMAL}</if><if test="countFlag != null and countFlag == 'Y'.toString() ">) as TMP_COUNT_TABLE</if></select><!-- 如果是mysql数据库,需要在jdbcUrl中设置allowMultiQueries=true参数才可以使用 --><!-- 返回值为第一条更新语句的执行结果,并非所有批量更新的语句总和 --><!--ORACLE的写法<insert id="insertBatch"  parameterType="java.util.List"><foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";">insert into test (a,b,c) values (#{item.a},#{item.b},#{item.c})</foreach></insert>MYSQL的写法<insert id="insertBatch"  parameterType="java.util.List">insert into test (a,b,c) values <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">#{item.a},#{item.b},#{item.c}</foreach></insert>--><insert id="insertBatch"  parameterType="java.util.List">insert into tbl_demo_info (demo_id,demo_nm,demo_money,create_date,create_time,select_static,select_dynamic,radio_static,radio_dynamic,checkbox_static,checkbox_dynamic,net_address,count_number) values <foreach collection="list" item="item" index="index" open="(" close=")" separator="),(">#{item.demoId},#{item.demoNm},#{item.demoMoney},#{item.createDate},#{item.createTime},#{item.selectStatic},#{item.selectDynamic},#{item.radioStatic},#{item.radioDynamic},#{item.checkboxStatic},#{item.checkboxDynamic},#{item.netAddress},#{item.countNumber}</foreach></insert><!-- 如果是mysql数据库,需要在jdbcUrl中设置allowMultiQueries=true参数才可以使用 --><!-- 返回值为第一条更新语句的执行结果,并非所有批量更新的语句总和 --><!--ORACLE的写法<update id="updateBatch"  parameterType="java.util.List"><foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";">update test<set>test=${item.test}+1</set>where id = ${item.id}</foreach></update>MYSQL的写法<update id="updateBatch"  parameterType="java.util.List"><foreach collection="list" item="item" index="index" open="" close="" separator=";">update test<set>test=${item.test}+1</set>where id = ${item.id}</foreach></update>--><update id="updateBatch" parameterType="java.util.List" ><foreach collection="list" item="item" index="index" open="" close="" separator=";">update tbl_demo_info<set><if test="item.demoNm_new != null">demo_nm = #{item.demoNm_new,jdbcType=VARCHAR} ,</if><if test="item.demoMoney_new != null">demo_money = #{item.demoMoney_new,jdbcType=DECIMAL} ,</if><if test="item.createDate_new != null">create_date = #{item.createDate_new,jdbcType=CHAR} ,</if><if test="item.createTime_new != null">create_time = #{item.createTime_new,jdbcType=CHAR} ,</if><if test="item.selectStatic_new != null">select_static = #{item.selectStatic_new,jdbcType=CHAR} ,</if><if test="item.selectDynamic_new != null">select_dynamic = #{item.selectDynamic_new,jdbcType=CHAR} ,</if><if test="item.radioStatic_new != null">radio_static = #{item.radioStatic_new,jdbcType=CHAR} ,</if><if test="item.radioDynamic_new != null">radio_dynamic = #{item.radioDynamic_new,jdbcType=CHAR} ,</if><if test="item.checkboxStatic_new != null">checkbox_static = #{item.checkboxStatic_new,jdbcType=VARCHAR} ,</if><if test="item.checkboxDynamic_new != null">checkbox_dynamic = #{item.checkboxDynamic_new,jdbcType=VARCHAR} ,</if><if test="item.netAddress_new != null">net_address = #{item.netAddress_new,jdbcType=VARCHAR} ,</if><if test="item.countNumber_new != null">count_number = #{item.countNumber_new,jdbcType=DECIMAL} ,</if></set>where 1=1<if test="item.demoId != null ">and demo_id = #{item.demoId,jdbcType=DECIMAL}</if><if test="item.demoNm != null ">and demo_nm = #{item.demoNm,jdbcType=VARCHAR}</if><if test="item.demoMoney != null ">and demo_money = #{item.demoMoney,jdbcType=DECIMAL}</if><if test="item.createDate != null ">and create_date = #{item.createDate,jdbcType=CHAR}</if><if test="item.createTime != null ">and create_time = #{item.createTime,jdbcType=CHAR}</if><if test="item.selectStatic != null ">and select_static = #{item.selectStatic,jdbcType=CHAR}</if><if test="item.selectDynamic != null ">and select_dynamic = #{item.selectDynamic,jdbcType=CHAR}</if><if test="item.radioStatic != null ">and radio_static = #{item.radioStatic,jdbcType=CHAR}</if><if test="item.radioDynamic != null ">and radio_dynamic = #{item.radioDynamic,jdbcType=CHAR}</if><if test="item.checkboxStatic != null ">and checkbox_static = #{item.checkboxStatic,jdbcType=VARCHAR}</if><if test="item.checkboxDynamic != null ">and checkbox_dynamic = #{item.checkboxDynamic,jdbcType=VARCHAR}</if><if test="item.netAddress != null ">and net_address = #{item.netAddress,jdbcType=VARCHAR}</if><if test="item.countNumber != null ">and count_number = #{item.countNumber,jdbcType=DECIMAL}</if></foreach></update><select id="selectByPrimaryKey" resultMap="BaseResultMap">select<include refid="Base_Column_List" />from tbl_demo_infowhere 1=1and demo_id = #{0}</select>
</mapper>

按照以上模板,

增加字段,只需要在指定的几个位置修改即可:共计15个位置。且这几个位置都是可以通过正则表达式匹配出来的,可以“工具化”的在某个字段后新增字段。

再说一下我司的ORM层设计,mybatis有官方的mybatisgenerator,但是实际工作中不太好用,因此我们自己开发了一套mybatismapper生成工具,按照类似上面的xml的格式:

1、生成xml文件。

2、xml文件中固定有:select查询对象、selectList查询对象列表、insert插入对象、updateByPrimaryKey根据主键修改对象、delete删除对象、queryOneMap查询Map、queryListMap查询Map列表、updateCAS原子性更新、queryForPage分页查询、insertBatch批量插入、updateBatch批量更新、selectByPrimaryKey主键查询 共十二种标准SQL语句,覆盖了绝大多数场景。

3、xml中的和字段相关的行全部独立为一行,方便新增字段时全局替换,或者beyoundcompare对比,或者svnmerge,或者gitmerge

4、开发了工具包,针对xml文件,输入字段和自动类型,自动找到指定位置新增字段

5、开发了工具包,针对xml文件,实现了mysql->oracle->db2->sqlite->sqlserver五种数据库的相互转换

6、开发了工具包,针对整体的数据库层,设计了“页面元素描述语言”,根据DB中的comment,或者参数型的设计,全套自动生成:mapper.java,mapper.xml,entity,logic,controller,ftl,js,页面校验,页面排版,一整套都是自动生成的。

可以说,我司的后台开发人员,仅需安装powerdesign软件,然后用各种自动化工具,就能把整套系统完成到可上线状态。

而题主所说的“包含了手写的部分联合查询”,在这套方案里,不过是queryForPage分页查询的SQL语句不同罢了,我一般喜欢beyoundcompare一下,然后把旧的queryForPage放到新的xml上,提交SVN即可。

对于实体类,只能手动加。

xml可以用sql标签将字段写到一处。

看看是否可以添加到新表,写新的实体对象。不用在原表上添加。

相关文章:

数据库和ORM如何优雅的添加字段?

RT&#xff0c;业务需要为某个实体添加字段&#xff0c;如何在生成了Mybatis XML&#xff08;包含了手写的部分联合查询&#xff09;的情况下优雅的添加字段呢&#xff1f; 作者&#xff1a;方小葱 链接&#xff1a;https://www.zhihu.com/question/284511416/answer/43812337…...

QT ubuntu下开发视频播放 FFmpeg

ubuntu 安装FFmpeg T113VideoDemo.pro #------------------------------------------------- # # Project created by QtCreator 2023-07-28T11:45:22 # #-------------------------------------------------QT core guigreaterThan(QT_MAJOR_VERSION, 4): QT widget…...

8.3一日总结

1.远程仓库的使用 a.克隆远程仓库 1>.在桌面克隆远程仓库 git clone 仓库名 2>.修改仓库内容 3>添加目录 git add. 4>提交: git commit -m 完成登录功能 5>推送提交远程仓库 : git push origin master -u 6>更改推送:git push(简写形式) 需要先添加,再提交,最…...

load、unload和pagehide、pageshow

一、load、unload和pagehide、pageshow的主要应用 1&#xff09;load 和 unload 事件监听web页面的进入和离开&#xff0c;一般用于页面的首次加载、刷新和关闭等操作的监听&#xff1b; 2&#xff09;pageshow 和 pagehide 事件多用于监听浏览器的前进和后退等。 二、pagesh…...

【面试问题12】

1.吞吐量和并发区别&#xff1f; 并发请求树/单位时间一般是s 吞吐量 并发/平均响应时间 常用的吞吐量指标有qps&#xff0c;tps 2.mysql数据是怎么存储的&#xff1f; 存储在data目录下&#xff0c;数据库作为文件夹&#xff0c;文件夹里面有.ibd文件&#xff0c;一个表一个ib…...

高性能网络框架笔记

目录 TCP粘包、分包惊群断开连接&#xff0c;TCP怎么检测的&#xff1f;大量的close wait&#xff0c;如何解 ?双方同时调用close水平触发和边沿触发的区别 TCP粘包、分包 解决&#xff1a;1.应用层协议头前面pktlen&#xff1b;2.为每一个包加上分隔符&#xff1b;(\r\n&…...

leetcode 738. 单调递增的数字

2023.8.4 这题用暴力法会超时&#xff0c;我就没试了&#xff0c;采用了个挺巧的方法&#xff0c;为了方便需要先将整数n转换为字符串的形式&#xff0c;然后从后向前遍历&#xff0c;当两个数字非递增时&#xff0c;将前一个数字--&#xff0c;后一个数字的位置记录在index中&…...

FPGA项目设计:数字时钟

项目要求&#xff1a; 设计一个数字时钟&#xff0c;数码管前两位显示小时&#xff0c;数码管中间两位显示分钟&#xff0c;数码管后面两位显示秒。 项目设计&#xff1a; 系统框架图&#xff1a; 计数模块时序图&#xff1a; 代码实现&#xff1a; 计数模块&#xff1a; /…...

科技云报道:向量数据库:AI时代的下一个热点

科技云报道原创。 最近&#xff0c;又一个概念火了——向量数据库。 随着大模型带来的应用需求提升&#xff0c;4月以来多家海外知名向量数据库创业企业传出融资喜讯。 4月28日&#xff0c;向量数据库平台Pinecone宣布获得1亿美元&#xff08;约7亿元&#xff09;B轮融资&am…...

【更新】119所院校考研重点勾画更新预告!

截至目前&#xff0c;我已经发布了47篇不同院校的择校分析。发布了87套名校信号考研真题以及119所不同院校的考研知识点重点勾画。 另外为了更好服务已经报名的同学&#xff0c;24梦马全程班也到了收尾的阶段。即将封班&#xff01;需要报名的同学抓紧啦&#xff01; 去年开始…...

【Leetcode】(自食用)LRU算法(哈希链表法)

step by step. 题目&#xff1a; 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类&#xff1a; LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存int get(int key) 如果关键字 key 存在于缓存中&#xff0c;则返回关键…...

robots.txt 如何禁止蜘蛛(百度,360,搜狗,谷歌)搜索引擎获取页面内容

什么是蜘蛛抓取 搜索引擎使用spider程序自动访问互联网上的网页并获取网页信息。spider在访问一个网站时&#xff0c;会首先会检查该网站的根域下是否有一个叫做robots.txt的纯文本文件。您可以在您的网站中创建一个纯文本文件robots.txt&#xff0c;在文件中声明该网站中不想…...

JVM 学习—— 类加载机制

前言 在上一篇文章中&#xff0c;荔枝梳理了有关Java中JVM体系架构的相关知识&#xff0c;其中涉及到的有关Java类加载机制的相关知识并没有过多描述。那么在这篇文章中&#xff0c;荔枝会详细梳理一下有关JVM的类加载机制和双亲委派模型的知识&#xff0c;希望能够帮助到有需要…...

C#实现int类型和字节流的相互在转化

通过TCP协议进行数据传输时&#xff0c;需要将所有传输的内容转为字节流&#xff0c;这里就用到了将int型的数据转为字节流的。代码如下&#xff1a; public static byte[] BytesConvertToInt(int vel) {byte[] hex new byte[4];hex[3] (byte)(vel >> 24) & 0xff)…...

Centos设置固定IP地址,外网访问

查看网络信息 一般会看到enp0s3的网络配置 ip address切换至网络配置路径 cd /etc/sysconfig/network-scripts/编辑配置 vi ifcfg-enp0s3 编辑配置 主要修改 静态ip:BOOTPROTOdhcp --> OOTPROTOstaticDNS(訪問外網):DNS1114.114.114.114本机ip: 192.168.70.121子网掩码…...

非线性弹簧摆的仿真(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

css实现文字颜色渐变+阴影

效果 代码 <div class"top"><div class"top-text" text"总经理驾驶舱">总经理驾驶舱</div> </div><style lang"scss" scoped>.top{width: 100%;text-align: center;height: 80px;line-height: 80px;fo…...

C++学习笔记总结练习:关联容器

关联容器 0 关联容器概述 关联容器与顺序容器的区别 关联容器和顺序容器有着根本不同。关联容器中的元素是按关键字来把偶才能和访问的。书序容器中的元素是按他们在容器中的位置来顺序保存和访问的。 两个基础类型 map:键值对key-value。关键字是索引&#xff0c;值表示与…...

TypeScript技能总结(二)

typescript是js的超集&#xff0c;目前很多前端框架都开始使用它来作为项目的维护管理的工具&#xff0c;还在不断地更新&#xff0c;添加新功能中&#xff0c;我们学习它&#xff0c;才能更好的在的项目中运用它&#xff0c;发挥它的最大功效 //readonly 只能修饰属性&#x…...

整理一些Postgresql工作中常用面试中会问的问题---Postgresql面试题001

1.什么是Postgresql TOAST? TOAST (The Oversized-Attribute Storage Technique,超大尺寸字段存储技术)主要用于存储大字段的值。 PostgreSQL 页面(page)大小是固定的(通常为8KB),且不允许tuples跨多个页面存储。因此不能存储非常大的字段值。为了克服这个限制,大字段…...

MVC 数据库

MVC 数据库 引言 在软件开发领域,Model-View-Controller(MVC)是一种流行的软件架构模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于提高代码的可维护性和可扩展性。本文将深入探讨MVC架构与数据库之间的关系,以…...

在Ubuntu中设置开机自动运行(sudo)指令的指南

在Ubuntu系统中&#xff0c;有时需要在系统启动时自动执行某些命令&#xff0c;特别是需要 sudo权限的指令。为了实现这一功能&#xff0c;可以使用多种方法&#xff0c;包括编写Systemd服务、配置 rc.local文件或使用 cron任务计划。本文将详细介绍这些方法&#xff0c;并提供…...

OPenCV CUDA模块图像处理-----对图像执行 均值漂移滤波(Mean Shift Filtering)函数meanShiftFiltering()

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 在 GPU 上对图像执行 均值漂移滤波&#xff08;Mean Shift Filtering&#xff09;&#xff0c;用于图像分割或平滑处理。 该函数将输入图像中的…...

视频行为标注工具BehaviLabel(源码+使用介绍+Windows.Exe版本)

前言&#xff1a; 最近在做行为检测相关的模型&#xff0c;用的是时空图卷积网络&#xff08;STGCN&#xff09;&#xff0c;但原有kinetic-400数据集数据质量较低&#xff0c;需要进行细粒度的标注&#xff0c;同时粗略搜了下已有开源工具基本都集中于图像分割这块&#xff0c…...

逻辑回归暴力训练预测金融欺诈

简述 「使用逻辑回归暴力预测金融欺诈&#xff0c;并不断增加特征维度持续测试」的做法&#xff0c;体现了一种逐步建模与迭代验证的实验思路&#xff0c;在金融欺诈检测中非常有价值&#xff0c;本文作为一篇回顾性记录了早年间公司给某行做反欺诈预测用到的技术和思路。百度…...

Unity UGUI Button事件流程

场景结构 测试代码 public class TestBtn : MonoBehaviour {void Start(){var btn GetComponent<Button>();btn.onClick.AddListener(OnClick);}private void OnClick(){Debug.Log("666");}}当添加事件时 // 实例化一个ButtonClickedEvent的事件 [Formerl…...

【免费数据】2005-2019年我国272个地级市的旅游竞争力多指标数据(33个指标)

旅游业是一个城市的重要产业构成。旅游竞争力是一个城市竞争力的重要构成部分。一个城市的旅游竞争力反映了其在旅游市场竞争中的比较优势。 今日我们分享的是2005-2019年我国272个地级市的旅游竞争力多指标数据&#xff01;该数据集源自2025年4月发表于《地理学报》的论文成果…...

Linux基础开发工具——vim工具

文章目录 vim工具什么是vimvim的多模式和使用vim的基础模式vim的三种基础模式三种模式的初步了解 常用模式的详细讲解插入模式命令模式模式转化光标的移动文本的编辑 底行模式替换模式视图模式总结 使用vim的小技巧vim的配置(了解) vim工具 本文章仍然是继续讲解Linux系统下的…...

基于Uniapp的HarmonyOS 5.0体育应用开发攻略

一、技术架构设计 1.混合开发框架选型 &#xff08;1&#xff09;使用Uniapp 3.8版本支持ArkTS编译 &#xff08;2&#xff09;通过uni-harmony插件调用原生能力 &#xff08;3&#xff09;分层架构设计&#xff1a; graph TDA[UI层] -->|Vue语法| B(Uniapp框架)B --&g…...

Copilot for Xcode (iOS的 AI辅助编程)

Copilot for Xcode 简介Copilot下载与安装 体验环境要求下载最新的安装包安装登录系统权限设置 AI辅助编程生成注释代码补全简单需求代码生成辅助编程行间代码生成注释联想 代码生成 总结 简介 尝试使用了Copilot&#xff0c;它能根据上下文补全代码&#xff0c;快速生成常用…...