数据库和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 >= #{createDate_start,jdbcType=CHAR}</if><if test="createDate_end != null and createDate_end != '' ">and create_date <= #{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,业务需要为某个实体添加字段,如何在生成了Mybatis XML(包含了手写的部分联合查询)的情况下优雅的添加字段呢? 作者:方小葱 链接: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)load 和 unload 事件监听web页面的进入和离开,一般用于页面的首次加载、刷新和关闭等操作的监听; 2)pageshow 和 pagehide 事件多用于监听浏览器的前进和后退等。 二、pagesh…...
【面试问题12】
1.吞吐量和并发区别? 并发请求树/单位时间一般是s 吞吐量 并发/平均响应时间 常用的吞吐量指标有qps,tps 2.mysql数据是怎么存储的? 存储在data目录下,数据库作为文件夹,文件夹里面有.ibd文件,一个表一个ib…...
高性能网络框架笔记
目录 TCP粘包、分包惊群断开连接,TCP怎么检测的?大量的close wait,如何解 ?双方同时调用close水平触发和边沿触发的区别 TCP粘包、分包 解决:1.应用层协议头前面pktlen;2.为每一个包加上分隔符;(\r\n&…...
leetcode 738. 单调递增的数字
2023.8.4 这题用暴力法会超时,我就没试了,采用了个挺巧的方法,为了方便需要先将整数n转换为字符串的形式,然后从后向前遍历,当两个数字非递增时,将前一个数字--,后一个数字的位置记录在index中&…...
FPGA项目设计:数字时钟
项目要求: 设计一个数字时钟,数码管前两位显示小时,数码管中间两位显示分钟,数码管后面两位显示秒。 项目设计: 系统框架图: 计数模块时序图: 代码实现: 计数模块: /…...
科技云报道:向量数据库:AI时代的下一个热点
科技云报道原创。 最近,又一个概念火了——向量数据库。 随着大模型带来的应用需求提升,4月以来多家海外知名向量数据库创业企业传出融资喜讯。 4月28日,向量数据库平台Pinecone宣布获得1亿美元(约7亿元)B轮融资&am…...
【更新】119所院校考研重点勾画更新预告!
截至目前,我已经发布了47篇不同院校的择校分析。发布了87套名校信号考研真题以及119所不同院校的考研知识点重点勾画。 另外为了更好服务已经报名的同学,24梦马全程班也到了收尾的阶段。即将封班!需要报名的同学抓紧啦! 去年开始…...
【Leetcode】(自食用)LRU算法(哈希链表法)
step by step. 题目: 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类: LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存int get(int key) 如果关键字 key 存在于缓存中,则返回关键…...
robots.txt 如何禁止蜘蛛(百度,360,搜狗,谷歌)搜索引擎获取页面内容
什么是蜘蛛抓取 搜索引擎使用spider程序自动访问互联网上的网页并获取网页信息。spider在访问一个网站时,会首先会检查该网站的根域下是否有一个叫做robots.txt的纯文本文件。您可以在您的网站中创建一个纯文本文件robots.txt,在文件中声明该网站中不想…...
JVM 学习—— 类加载机制
前言 在上一篇文章中,荔枝梳理了有关Java中JVM体系架构的相关知识,其中涉及到的有关Java类加载机制的相关知识并没有过多描述。那么在这篇文章中,荔枝会详细梳理一下有关JVM的类加载机制和双亲委派模型的知识,希望能够帮助到有需要…...
C#实现int类型和字节流的相互在转化
通过TCP协议进行数据传输时,需要将所有传输的内容转为字节流,这里就用到了将int型的数据转为字节流的。代码如下: 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代码实现)
💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&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。关键字是索引,值表示与…...
TypeScript技能总结(二)
typescript是js的超集,目前很多前端框架都开始使用它来作为项目的维护管理的工具,还在不断地更新,添加新功能中,我们学习它,才能更好的在的项目中运用它,发挥它的最大功效 //readonly 只能修饰属性&#x…...
整理一些Postgresql工作中常用面试中会问的问题---Postgresql面试题001
1.什么是Postgresql TOAST? TOAST (The Oversized-Attribute Storage Technique,超大尺寸字段存储技术)主要用于存储大字段的值。 PostgreSQL 页面(page)大小是固定的(通常为8KB),且不允许tuples跨多个页面存储。因此不能存储非常大的字段值。为了克服这个限制,大字段…...
JavaSec-RCE
简介 RCE(Remote Code Execution),可以分为:命令注入(Command Injection)、代码注入(Code Injection) 代码注入 1.漏洞场景:Groovy代码注入 Groovy是一种基于JVM的动态语言,语法简洁,支持闭包、动态类型和Java互操作性,…...
转转集团旗下首家二手多品类循环仓店“超级转转”开业
6月9日,国内领先的循环经济企业转转集团旗下首家二手多品类循环仓店“超级转转”正式开业。 转转集团创始人兼CEO黄炜、转转循环时尚发起人朱珠、转转集团COO兼红布林CEO胡伟琨、王府井集团副总裁祝捷等出席了开业剪彩仪式。 据「TMT星球」了解,“超级…...
1.3 VSCode安装与环境配置
进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件,然后打开终端,进入下载文件夹,键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...
数据链路层的主要功能是什么
数据链路层(OSI模型第2层)的核心功能是在相邻网络节点(如交换机、主机)间提供可靠的数据帧传输服务,主要职责包括: 🔑 核心功能详解: 帧封装与解封装 封装: 将网络层下发…...
Psychopy音频的使用
Psychopy音频的使用 本文主要解决以下问题: 指定音频引擎与设备;播放音频文件 本文所使用的环境: Python3.10 numpy2.2.6 psychopy2025.1.1 psychtoolbox3.0.19.14 一、音频配置 Psychopy文档链接为Sound - for audio playback — Psy…...
MySQL中【正则表达式】用法
MySQL 中正则表达式通过 REGEXP 或 RLIKE 操作符实现(两者等价),用于在 WHERE 子句中进行复杂的字符串模式匹配。以下是核心用法和示例: 一、基础语法 SELECT column_name FROM table_name WHERE column_name REGEXP pattern; …...
OpenLayers 分屏对比(地图联动)
注:当前使用的是 ol 5.3.0 版本,天地图使用的key请到天地图官网申请,并替换为自己的key 地图分屏对比在WebGIS开发中是很常见的功能,和卷帘图层不一样的是,分屏对比是在各个地图中添加相同或者不同的图层进行对比查看。…...
华硕a豆14 Air香氛版,美学与科技的馨香融合
在快节奏的现代生活中,我们渴望一个能激发创想、愉悦感官的工作与生活伙伴,它不仅是冰冷的科技工具,更能触动我们内心深处的细腻情感。正是在这样的期许下,华硕a豆14 Air香氛版翩然而至,它以一种前所未有的方式&#x…...
Python Ovito统计金刚石结构数量
大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...
小木的算法日记-多叉树的递归/层序遍历
🌲 从二叉树到森林:一文彻底搞懂多叉树遍历的艺术 🚀 引言 你好,未来的算法大神! 在数据结构的世界里,“树”无疑是最核心、最迷人的概念之一。我们中的大多数人都是从 二叉树 开始入门的,它…...
