1.问题描述:使用mybatis生成mapper.xml文件的时候经常遇到数据库表字段很多,写mapper.xml文件时转化成java实体很麻烦,写增删改查的sql时也要做
很多重复的工作。程序员就想一劳永逸,我拼接了几个sql可以简化,方便生成数据库映射和java实体。(待解决问题,没有JDBCTYPE,需要再建立一个映射java类型和数据库字段类型的关系表)
2.解决:
先来一个表结构(字段很多,要是一个一个写,早烦啦烦啦,死啦死啦,怎么破?)
CREATE TABLE `t_ltl_order` (
`id` varchar(100) NOT NULL COMMENT 'id',
`shipper_id` varchar(30) DEFAULT NULL COMMENT '发货客户id',
`shipper_number` varchar(50) DEFAULT NULL COMMENT '发货客户编码',
`shipper_name` varchar(100) DEFAULT NULL COMMENT '发货客户名称',
`contact_name` varchar(500) DEFAULT NULL COMMENT '发货联系人名称',
`contact_mobile` varchar(200) DEFAULT NULL COMMENT '手机',
`contact_phone` varchar(500) DEFAULT NULL COMMENT '电话',
`contact_province` varchar(200) DEFAULT NULL COMMENT '省份',
`contact_city` varchar(500) DEFAULT NULL COMMENT '城市',
`contact_area` varchar(100) DEFAULT NULL COMMENT '区县',
`contact_address` varchar(255) DEFAULT NULL COMMENT '详细地址',
`isreceivegood` varchar(1) DEFAULT NULL COMMENT '是否接货',
`beginaccep_time` datetime DEFAULT NULL COMMENT '接货起始时间',
`endaccp_time` datetime DEFAULT NULL COMMENT '接货结束时间',
`receive_id` varchar(50) DEFAULT NULL COMMENT '收货客户id',
`receive_number` varchar(50) DEFAULT NULL COMMENT '收货客户编码',
`receive_custname` varchar(100) DEFAULT NULL,
`issendms` varchar(1) DEFAULT NULL COMMENT '是否短信通知',
`receive_name` varchar(255) DEFAULT NULL COMMENT '收货联系人姓名',
`receive_mobile` varchar(200) DEFAULT NULL COMMENT '收货联系人手机',
`receive_phone` varchar(500) DEFAULT NULL COMMENT '收货联系人电话',
`receive_province` varchar(200) DEFAULT NULL COMMENT '收货联系人省份',
`receive_city` varchar(500) DEFAULT NULL COMMENT '收货联系人城市',
`receive_area` varchar(50) DEFAULT NULL COMMENT '收货联系人区县',
`receive_address` varchar(255) DEFAULT NULL COMMENT '收货联系人详细地址',
`transport_mode` varchar(30) DEFAULT NULL COMMENT '运输方式',
`goods_name` varchar(255) DEFAULT NULL COMMENT '货物名称',
`packing` varchar(500) DEFAULT NULL COMMENT '包装材料',
`goods_type` varchar(50) DEFAULT NULL COMMENT '货物类型',
`total_piece` int(11) DEFAULT NULL COMMENT '托运货物总件数',
`total_weight` double(28,10) DEFAULT NULL COMMENT '托运货物总重量',
`total_volume` double(28,10) DEFAULT NULL COMMENT '托运货物总体积',
`payment_type` varchar(30) DEFAULT NULL COMMENT '付款方式',
`delivery_mode` varchar(50) DEFAULT NULL COMMENT '提货方式',
`dshk_type` varchar(30) DEFAULT NULL COMMENT '代收货款类型',
`dshk` double(28,10) DEFAULT NULL COMMENT '代收货款',
`bjsmjz` double(28,10) DEFAULT NULL COMMENT '保价声明价值',
`order_time` datetime DEFAULT NULL COMMENT '下单时间',
`departure_id` varchar(30) DEFAULT NULL COMMENT '始发网点id',
`accept_deptid` varchar(30) DEFAULT NULL COMMENT '受理部门id',
`order_status` varchar(50) DEFAULT NULL COMMENT '订单状态',
`accepter` varchar(500) DEFAULT NULL COMMENT '受理人',
`ordercreate_time` datetime DEFAULT NULL COMMENT '订单创建时间',
`accept_time` datetime DEFAULT NULL COMMENT '订单受理时间',
`waybill_number` varchar(500) DEFAULT NULL COMMENT '运单号',
`resource` varchar(50) DEFAULT NULL COMMENT '订单来源',
`hasten_count` int(11) DEFAULT '0' COMMENT '催单次数',
`order_number` varchar(50) NOT NULL COMMENT '订单号',
`channel_number` varchar(500) DEFAULT NULL COMMENT '渠道的单号',
`returnbill_type` varchar(30) DEFAULT NULL COMMENT '签收单方式',
`trans_note` varchar(1000) DEFAULT NULL COMMENT '储运事项',
`receivingto_pointid` varchar(30) DEFAULT NULL COMMENT '到达网点id',
`order_person` varchar(100) DEFAULT NULL COMMENT '下单人',
`depart_linkmannum` varchar(50) DEFAULT NULL COMMENT '发货人联系人编码',
`order_type` varchar(50) DEFAULT NULL COMMENT '订单类型:数据字典数据',
`arrive_linkmannum` varchar(50) DEFAULT NULL COMMENT '收货人联系人编码',
`remark` varchar(1000) DEFAULT NULL COMMENT '备注(网点名称、电话,快递员手机、电话)',
`refund_mode` varchar(30) DEFAULT NULL COMMENT '退款方式',
`create_usernum` varchar(65) DEFAULT NULL COMMENT '制单员工工号',
`accepter_name` varchar(100) DEFAULT NULL COMMENT '接货员姓名',
`accepter_mobile` varchar(30) DEFAULT NULL COMMENT '接货员联系方式',
`feedback_info` varchar(2000) DEFAULT NULL COMMENT '反馈信息',
`member_type` varchar(255) DEFAULT NULL COMMENT '阿里巴巴会员类型',
`towait_accept_time` datetime DEFAULT NULL COMMENT '最后一次待受理时间',
`delayorder_time` datetime DEFAULT NULL COMMENT '延迟时间',
`contact_comments` varchar(1000) DEFAULT NULL COMMENT '发货方地址备注',
`receive_comments` varchar(1000) DEFAULT NULL COMMENT '收货方地址备注',
`procurement_number` varchar(50) DEFAULT NULL COMMENT '采购单号',
`cust_group` varchar(100) DEFAULT NULL COMMENT '客户分群',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='快递订单表';
2.1 mysql版本(后面有oracle版本)
先建立一个拼sql的函数(做了两件事,把下划线后面的字母大写,去除下划线)
CREATE
FUNCTION fun_yxl_rep(str varchar(128))
RETURNS varchar(128) CHARSET utf8
BEGIN
DECLARE len,
i int;
DECLARE ch char;
DECLARE s1,s2 varchar(128);
SET i = 1;
SELECT
LENGTH(str) INTO len;
WHILE (i <= len) DO
SET ch = SUBSTRING(str, i, 1);
IF (ch = '_' and (i+1) <= len) THEN
set s1 = SUBSTRING(str, 1, i);
set s2 = SUBSTRING(str, i+2, len - i);
SET str = CONCAT(s1,UPPER(SUBSTRING(str, i+1, 1)),s2);
END IF;
SET i = i + 1;
END WHILE;
RETURN REPLACE(str,'_','');
END
2.2使用
2.2.1.生成表的列升序排列(注意修改表名和schema就可以用了)
select CONCAT('t.',COLUMN_NAME,',') from information_schema.COLUMNS where
table_name = 't_ltl_order' and table_schema = 'oms'order by COLUMN_NAME;
结果:
t.accepter,
t.accepter_mobile,
t.accepter_name,
t.accept_deptid,
t.accept_time,
t.arrive_linkmannum,
t.beginaccep_time,
t.bjsmjz,
t.channel_number,
t.contact_address,
t.contact_area,
t.contact_city,
t.contact_comments,
t.contact_mobile,
t.contact_name,
t.contact_phone,
t.contact_province,
t.create_usernum,
t.cust_group,
t.delayorder_time,
t.delivery_mode,
t.departure_id,
t.depart_linkmannum,
t.dshk,
t.dshk_type,
t.endaccp_time,
t.feedback_info,
t.goods_name,
t.goods_type,
t.hasten_count,
t.id,
t.isreceivegood,
t.issendms,
t.member_type,
t.ordercreate_time,
t.order_number,
t.order_person,
t.order_status,
t.order_time,
t.order_type,
t.packing,
t.payment_type,
t.procurement_number,
t.receive_address,
t.receive_area,
t.receive_city,
t.receive_comments,
t.receive_custname,
t.receive_id,
t.receive_mobile,
t.receive_name,
t.receive_number,
t.receive_phone,
t.receive_province,
t.receivingto_pointid,
t.refund_mode,
t.remark,
t.resource,
t.returnbill_type,
t.shipper_id,
t.shipper_name,
t.shipper_number,
t.total_piece,
t.total_volume,
t.total_weight,
t.towait_accept_time,
t.transport_mode,
t.trans_note,
t.waybill_number,
2.2.2生成java实体 及其注释
select CONCAT('// ',COLUMN_COMMENT,CHAR(13),' private String ',
fun_yxl_rep(COLUMN_NAME),';') from information_schema.COLUMNS where
table_name = 't_ltl_order' and table_schema = 'oms'order by COLUMN_NAME;
结果:
// 受理人
private String accepter;
// 接货员联系方式
private String accepterMobile;
// 接货员姓名
private String accepterName;
// 受理部门id
private String acceptDeptid;
// 订单受理时间
private String acceptTime;
// 收货人联系人编码
private String arriveLinkmannum;
// 接货起始时间
private String beginaccepTime;
// 保价声明价值
private String bjsmjz;
// 渠道的单号
private String channelNumber;
// 详细地址
private String contactAddress;
// 区县
private String contactArea;
// 城市
private String contactCity;
// 发货方地址备注
private String contactComments;
// 手机
private String contactMobile;
// 发货联系人名称
private String contactName;
// 电话
private String contactPhone;
// 省份
private String contactProvince;
// 制单员工工号
private String createUsernum;
// 客户分群
private String custGroup;
// 延迟时间
private String delayorderTime;
// 提货方式
private String deliveryMode;
// 始发网点id
private String departureId;
// 发货人联系人编码
private String departLinkmannum;
// 代收货款
private String dshk;
// 代收货款类型
private String dshkType;
// 接货结束时间
private String endaccpTime;
// 反馈信息
private String feedbackInfo;
// 货物名称
private String goodsName;
// 货物类型
private String goodsType;
// 催单次数
private String hastenCount;
// id
private String id;
// 是否接货
private String isreceivegood;
// 是否短信通知
private String issendms;
// 阿里巴巴会员类型
private String memberType;
// 订单创建时间
private String ordercreateTime;
// 订单号
private String orderNumber;
// 下单人
private String orderPerson;
// 订单状态
private String orderStatus;
// 下单时间
private String orderTime;
// 订单类型:数据字典数据
private String orderType;
// 包装材料
private String packing;
// 付款方式
private String paymentType;
// 采购单号
private String procurementNumber;
// 收货联系人详细地址
private String receiveAddress;
// 收货联系人区县
private String receiveArea;
// 收货联系人城市
private String receiveCity;
// 收货方地址备注
private String receiveComments;
//
private String receiveCustname;
// 收货客户id
private String receiveId;
// 收货联系人手机
private String receiveMobile;
// 收货联系人姓名
private String receiveName;
// 收货客户编码
private String receiveNumber;
// 收货联系人电话
private String receivePhone;
// 收货联系人省份
private String receiveProvince;
// 到达网点id
private String receivingtoPointid;
// 退款方式
private String refundMode;
// 备注(网点名称、电话,快递员手机、电话)
private String remark;
// 订单来源
private String resource;
// 签收单方式
private String returnbillType;
// 发货客户id
private String shipperId;
// 发货客户名称
private String shipperName;
// 发货客户编码
private String shipperNumber;
// 托运货物总件数
private String totalPiece;
// 托运货物总体积
private String totalVolume;
// 托运货物总重量
private String totalWeight;
// 最后一次待受理时间
private String towaitAcceptTime;
// 运输方式
private String transportMode;
// 储运事项
private String transNote;
// 运单号
private String waybillNumber;
2.2.3生成resultMap
select CONCAT('
table_name = 't_ltl_order' and table_schema = 'oms'order by COLUMN_NAME;
结果:
2.2.4生成--where if 判空
select CONCAT('
from information_schema.COLUMNS where
table_name = 't_ltl_order' and table_schema = 'oms'order by COLUMN_NAME;
结果:
and t.accepter = #{accepter}
and t.accepter_mobile = #{accepterMobile}
and t.accepter_name = #{accepterName}
and t.accept_deptid = #{acceptDeptid}
and t.accept_time = #{acceptTime}
and t.arrive_linkmannum = #{arriveLinkmannum}
and t.beginaccep_time = #{beginaccepTime}
and t.bjsmjz = #{bjsmjz}
and t.channel_number = #{channelNumber}
and t.contact_address = #{contactAddress}
and t.contact_area = #{contactArea}
and t.contact_city = #{contactCity}
and t.contact_comments = #{contactComments}
and t.contact_mobile = #{contactMobile}
and t.contact_name = #{contactName}
and t.contact_phone = #{contactPhone}
and t.contact_province = #{contactProvince}
and t.create_usernum = #{createUsernum}
and t.cust_group = #{custGroup}
and t.delayorder_time = #{delayorderTime}
and t.delivery_mode = #{deliveryMode}
and t.departure_id = #{departureId}
and t.depart_linkmannum = #{departLinkmannum}
and t.dshk = #{dshk}
and t.dshk_type = #{dshkType}
and t.endaccp_time = #{endaccpTime}
and t.feedback_info = #{feedbackInfo}
and t.goods_name = #{goodsName}
and t.goods_type = #{goodsType}
and t.hasten_count = #{hastenCount}
and t.id = #{id}
and t.isreceivegood = #{isreceivegood}
and t.issendms = #{issendms}
and t.member_type = #{memberType}
and t.ordercreate_time = #{ordercreateTime}
and t.order_number = #{orderNumber}
and t.order_person = #{orderPerson}
and t.order_status = #{orderStatus}
and t.order_time = #{orderTime}
and t.order_type = #{orderType}
and t.packing = #{packing}
and t.payment_type = #{paymentType}
and t.procurement_number = #{procurementNumber}
and t.receive_address = #{receiveAddress}
and t.receive_area = #{receiveArea}
and t.receive_city = #{receiveCity}
and t.receive_comments = #{receiveComments}
and t.receive_custname = #{receiveCustname}
and t.receive_id = #{receiveId}
and t.receive_mobile = #{receiveMobile}
and t.receive_name = #{receiveName}
and t.receive_number = #{receiveNumber}
and t.receive_phone = #{receivePhone}
and t.receive_province = #{receiveProvince}
and t.receivingto_pointid = #{receivingtoPointid}
and t.refund_mode = #{refundMode}
and t.remark = #{remark}
and t.resource = #{resource}
and t.returnbill_type = #{returnbillType}
and t.shipper_id = #{shipperId}
and t.shipper_name = #{shipperName}
and t.shipper_number = #{shipperNumber}
and t.total_piece = #{totalPiece}
and t.total_volume = #{totalVolume}
and t.total_weight = #{totalWeight}
and t.towait_accept_time = #{towaitAcceptTime}
and t.transport_mode = #{transportMode}
and t.trans_note = #{transNote}
and t.waybill_number = #{waybillNumber}
2.2.5生成extjs model
select CONCAT('{name : \'',fun_yxl_rep(COLUMN_NAME),'\' // ',
COLUMN_COMMENT,CHAR(13),'},') from information_schema.COLUMNS where
table_name = 't_ltl_order' and table_schema = 'oms' order by COLUMN_NAME;
结果:
{name : 'accepter' // 受理人
},
{name : 'accepterMobile' // 接货员联系方式
},
{name : 'accepterName' // 接货员姓名
},
{name : 'acceptDeptid' // 受理部门id
},
{name : 'acceptTime' // 订单受理时间
},
{name : 'arriveLinkmannum' // 收货人联系人编码
},
{name : 'beginaccepTime' // 接货起始时间
},
{name : 'bjsmjz' // 保价声明价值
},
{name : 'channelNumber' // 渠道的单号
},
{name : 'contactAddress' // 详细地址
},
{name : 'contactArea' // 区县
},
{name : 'contactCity' // 城市
},
{name : 'contactComments' // 发货方地址备注
},
{name : 'contactMobile' // 手机
},
{name : 'contactName' // 发货联系人名称
},
{name : 'contactPhone' // 电话
},
{name : 'contactProvince' // 省份
},
{name : 'createUsernum' // 制单员工工号
},
{name : 'custGroup' // 客户分群
},
{name : 'delayorderTime' // 延迟时间
},
{name : 'deliveryMode' // 提货方式
},
{name : 'departureId' // 始发网点id
},
{name : 'departLinkmannum' // 发货人联系人编码
},
{name : 'dshk' // 代收货款
},
{name : 'dshkType' // 代收货款类型
},
{name : 'endaccpTime' // 接货结束时间
},
{name : 'feedbackInfo' // 反馈信息
},
{name : 'goodsName' // 货物名称
},
{name : 'goodsType' // 货物类型
},
{name : 'hastenCount' // 催单次数
},
{name : 'id' // id
},
{name : 'isreceivegood' // 是否接货
},
{name : 'issendms' // 是否短信通知
},
{name : 'memberType' // 阿里巴巴会员类型
},
{name : 'ordercreateTime' // 订单创建时间
},
{name : 'orderNumber' // 订单号
},
{name : 'orderPerson' // 下单人
},
{name : 'orderStatus' // 订单状态
},
{name : 'orderTime' // 下单时间
},
{name : 'orderType' // 订单类型:数据字典数据
},
{name : 'packing' // 包装材料
},
{name : 'paymentType' // 付款方式
},
{name : 'procurementNumber' // 采购单号
},
{name : 'receiveAddress' // 收货联系人详细地址
},
{name : 'receiveArea' // 收货联系人区县
},
{name : 'receiveCity' // 收货联系人城市
},
{name : 'receiveComments' // 收货方地址备注
},
{name : 'receiveCustname' //
},
{name : 'receiveId' // 收货客户id
},
{name : 'receiveMobile' // 收货联系人手机
},
{name : 'receiveName' // 收货联系人姓名
},
{name : 'receiveNumber' // 收货客户编码
},
{name : 'receivePhone' // 收货联系人电话
},
{name : 'receiveProvince' // 收货联系人省份
},
{name : 'receivingtoPointid' // 到达网点id
},
{name : 'refundMode' // 退款方式
},
{name : 'remark' // 备注(网点名称、电话,快递员手机、电话)
},
{name : 'resource' // 订单来源
},
{name : 'returnbillType' // 签收单方式
},
{name : 'shipperId' // 发货客户id
},
{name : 'shipperName' // 发货客户名称
},
{name : 'shipperNumber' // 发货客户编码
},
{name : 'totalPiece' // 托运货物总件数
},
{name : 'totalVolume' // 托运货物总体积
},
{name : 'totalWeight' // 托运货物总重量
},
{name : 'towaitAcceptTime' // 最后一次待受理时间
},
{name : 'transportMode' // 运输方式
},
{name : 'transNote' // 储运事项
},
{name : 'waybillNumber' // 运单号
},
2.2.6 update 修改
select CONCAT('
from information_schema.COLUMNS where
table_name = 't_ltl_order' and table_schema = 'oms'order by COLUMN_NAME;
结果:
2.2.7insert插入 判空
插入语句模板
insert into t_ltl_order
select CONCAT('
COLUMN_NAME,',','
from information_schema.COLUMNS where
table_name = 't_ltl_order' and table_schema = 'oms'order by COLUMN_NAME;
结果:
insert插入 判空values
select CONCAT('
'#{',fun_yxl_rep(COLUMN_NAME),'},','')
from information_schema.COLUMNS where
table_name = 't_ltl_order' and table_schema = 'oms'order by COLUMN_NAME;
结果:
2.3oracle版本
2.3.1函数:create or replace function fun_yxl_rep(str varchar2) return varchar2 is
lng int;
i int;
ch char;
s1 varchar2(128);
s2 varchar2(128);
rst varchar2(128);
begin
i := 1;
rst := str;
select length(rst) into lng from dual;
while (i <= lng) loop
ch := substr(rst, i, 1);
if (ch = '_' and (i + 1) <= lng) then
s1 := substr(rst, 1, i);
s2 := substr(rst, i + 2, lng - i);
rst := s1 || upper(substr(rst, i + 1, 1)) || s2;
end if;
i := i + 1;
end loop;
return replace(rst,'_','');
end fun_yxl_rep;
2.3.2用到的基本表
select c.table_name as 表名,
c.column_name as 列名,
c.data_type as 数据类型,
c.data_length as 长度,
c.nullable as 是否为空,
c.column_id as 列序号,
m.comments as 备注
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
2.3.3列columns
select 'T.' || column_name ||','
from user_tab_cols c
where c.table_name = 'T_AUTH_FUNCTION'
order by column_name;
2.3.4java实体 属性
select '// ' || m.comments || chr(13) || ' private String ' ||
fun_yxl_rep(lower(c.column_name)) || ';'
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
order by c.column_name;
2.3.5mapper resultMap
select '
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
order by c.column_name;
2.3.6if 判空
select '
lower(c.column_name) || ' = #{' || fun_yxl_rep(lower(c.column_name)) || '}' ||
chr(13) || '
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
order by c.column_name;
2.3.7extjsjs model
select '{name : ''' || fun_yxl_rep(lower(c.column_name)) || ''' // ' ||
m.comments || chr(13) || '},'
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
order by c.column_name;
2.3.8update 修改
select '
lower(c.column_name) || ' = #{' || fun_yxl_rep(lower(c.column_name)) || '}' ||
',
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
order by c.column_name;
2.3.9insert插入 判空
select '
lower(c.column_name) ||','|| '
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
order by c.column_name;
-- insert插入 判空values
select '
fun_yxl_rep(lower(c.column_name)) || '},' || '
from user_tab_cols c
inner join user_col_comments m
on m.table_name = c.table_name
and m.column_name = c.column_name
where c.table_name = 'T_AUTH_FUNCTION'
order by c.column_name;