mybatis 遍历字段和字段对应的值循环插入
java代码:
/**
* 插入单条数据
* @param request
* @param response
* @param id
* @param str
* @return
* @throws Exception
*/
@RequestMapping(value="insertData")
@ResponseBody
public Map<String,Object> insertData(HttpServletRequest request,
HttpServletResponse response,Integer id,String str) throws Exception{
String key=null;
String value;
String tableName=null;
BusinessDesc businessDesc = businessDescService.queryById(id);
//取出所有字段
List<ColumnRule> columnnlist = columnRuleService.findByBusinessId(id);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
List<String> list2 = new ArrayList<String>();
Map<String,Object> maps=new HashMap<String,Object>();
JSONObject jsonObject = new JSONObject(str);
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()){
Map<String,Object> map=new HashMap<String,Object>();
key = (String) iterator.next();
value = jsonObject.getString(key);
for(ColumnRule rule:columnnlist){
if(rule.getName().equals(key)){
if(rule.getFieldType().contains("int")){
if(value!=null && !"".equals(value)){
map.put("value", Integer.parseInt(value));
}else{
map.put("value",0);
}
}else{
map.put("value", value);
}
}
}
map.put("key", key);
list.add(map);
}
if(businessDesc!=null){
tableName=businessDesc.getTablename();
}
maps.put("list", list);
maps.put("table", tableName);
businessDescService.insertData(maps);
return maps;
}
/**
* 修改单条数据
* @param request
* @param response
* @param dataId
* @param str
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value="updateData")
@ResponseBody
public Map<String,Object> updateData(HttpServletRequest request,
HttpServletResponse response,Integer dataId,String str,Integer id) throws Exception{
String tableName=null;
String key=null;
String value;
Map<String,Object> maps=new HashMap<String,Object>();
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//获取表名
BusinessDesc businessDesc = businessDescService.queryById(dataId);
//取出所有字段
List<ColumnRule> columnnlist = columnRuleService.findByBusinessId(dataId);
JSONObject jsonObject = new JSONObject(str);
Iterator iterator = jsonObject.keys();
while(iterator.hasNext()){
Map<String,Object> map=new HashMap<String,Object>();
key = (String) iterator.next();
value = jsonObject.getString(key);
for(ColumnRule rule:columnnlist){
if(rule.getName().equals(key)){
if(rule.getFieldType().contains("int")){
if(value!=null && !"".equals(value)){
map.put("value", Integer.parseInt(value));
}else{
map.put("value", 0);
}
}else{
if(value!=null &&!"".equals(value)){
if(!key.equals("id")){
map.put("value", value);
}
}
}
}
}
map.put("key", key);
if(key.equals("id")){
map.put("value", id);
}
list.add(map);
}
if(businessDesc!=null){
tableName=businessDesc.getTablename();
}
maps.put("list", list);
maps.put("table", tableName);
maps.put("id", id);
businessDescService.updateData(maps);
return maps;
}
隐射文件:
<insert id="insertData" parameterType="java.util.Map">
insert into ${table}
(<foreach collection="list" item="item" index="index" separator=",">${item.key}</foreach>)
values
(
<foreach collection="list" item="subitem" index="index" separator=",">
#{subitem.value}
</foreach>
)
</insert>
<update id="updateData" parameterType="java.util.Map">
update
${table}
set
<foreach collection="list" item="item" index="index" separator=",">${item.key}=#{item.value}</foreach>
where id=#{id}
</update>
遍历list
dao类:List<Garage> findRecommendGarageById(List<String> list);
xml隐射文件:
<select id="findRecommendGarageById" resultMap="BaseResultMap" resultType="java.util.List">
select <include refid="Base_Column_List" /> from s_garage
<where>
id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</where>
</select>
mybatis 遍历字段和字段对应的值循环插入的更多相关文章
- mysql 动态拼接表字段,值 mybatis 动态获取表字段
-- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...
- Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值
我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...
- SQL Server 按某一字段分组 取 最大 (小)值所在行的数据
SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23 ...
- Mysql JSON字段提取某一个属性值的函数
mysql从5.7开始才支持JSON_EXTRACT等 JSON相关的函数, 项目里用到的mysql是5.6的,需要提取JSON字段里某一个属性值进行统计, 自己写了一个笨的提取方法: CREATE ...
- Python3+SQLAlchemy不使用字段名获取主键值教程
一.说明 1.1 环境说明 user model如下,且其现有一个实例user_inst: class User(Base): __tablename__ = 'users' username = C ...
- powerdesigner 字段添加注释和默认值
powerdesigner 字段添加注释和默认值 2017年01月06日 10:59:02 qingzhuoran 阅读数:27161更多 个人分类: powerdesigner 1.选中表,右键 ...
- 【PowerDesigner】【3】字段添加注释和默认值
问题:最开始生成的Table,表头有些字段没有 解决方案: 1,打开表(双击左键),点击下图圈起来的图标 2,找到comment(注释),勾选 3,找到default value(默认值),勾选 4, ...
- ArcGIS自定义工具箱-自增字段(可以设置初始值和步长)
ArcGIS自定义工具箱-自增字段(可以设置初始值和步长) 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:字段值初始值为设定值(默认为0),每次增加一个步长( ...
随机推荐
- IOS AppStore上线前测试
IOS AppStore上线前测试 TestFlight Beta ios 8才开始支持. https://developer.apple.com/library/ios/documentation ...
- Linux 查看一个端口的连接数
netstat -antp|grep -i "80" |wc -l 譬如查看80端口的连接数
- day17 14.dao模式介绍
Web的三层架构,不是MVC,Web层,Service层,DAO层. 之前玩的JSP Servlet JavaBean那是MVC模式,那玩意只是表现层的东西. 转账汇款的例子. 说了这么多有啥用啊,一 ...
- linux 中更改权限命令chown,chmod,chgrp
写在前面,关于chown,chmod的区别 chown用法 用来更改某个目录或文件的用户名和用户组的 chown 用户名:组名 文件路径(可以是就对路径也可以是相对路径) 例1:chown root: ...
- [poj3159]Candies(差分约束+链式前向星dijkstra模板)
题意:n个人,m个信息,每行的信息是3个数字,A,B,C,表示B比A多出来的糖果不超过C个,问你,n号人最多比1号人多几个糖果 解题关键:差分约束系统转化为最短路,B-A>=C,建有向边即可,与 ...
- 25-Fibonacci(矩阵快速幂)
http://poj.org/problem?id=3070 Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- Excel VBA 若要在64位系统上使用,则必须更新此项目中的代码,请检查并更新Declare语句,然后用PtrSafe属性标记它们
在Office 2010 32位上开发的Excel VBA系统,迁移到Office 2010 64位下面,打开后使用,报下面错误: 解决办法: 在Declare 后面加PtrSafe 进行标记
- C++--VECTOR的简单应用
在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...
- 手把手教Android商业项目-即时通讯-i美聊
[课程概况] 手把手教你从无到有的完整实现一个Android商业项目,是目前整个市场上所没有的课程,废话不多说,请往下看. [项目概况] 项目名称:i美聊 所属领域:移动社交 即时通讯 代码行数: ...
- [学习笔记]fork写实复制
#include<stdio.h> #include<stdlib.h> #include<string.h> #include <unistd.h> ...