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 遍历字段和字段对应的值循环插入的更多相关文章

  1. mysql 动态拼接表字段,值 mybatis 动态获取表字段

    -- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...

  2. Mysql数据表字段设置了默认值,插入数据后默认字段的值却为null,不是默认值

    我将mysql的数据表的某个字段设置了默认值为1,当向该表插入数据的时候该字段的值不是默认值,而是null. 我的错误原因: 对数据库的操作我使用了持久化工具mybatis,插入数据的时候插入的是整个 ...

  3. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  4. SQL Server 按某一字段分组 取 最大 (小)值所在行的数据

    SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23 ...

  5. Mysql JSON字段提取某一个属性值的函数

    mysql从5.7开始才支持JSON_EXTRACT等 JSON相关的函数, 项目里用到的mysql是5.6的,需要提取JSON字段里某一个属性值进行统计, 自己写了一个笨的提取方法: CREATE ...

  6. Python3+SQLAlchemy不使用字段名获取主键值教程

    一.说明 1.1 环境说明 user model如下,且其现有一个实例user_inst: class User(Base): __tablename__ = 'users' username = C ...

  7. powerdesigner 字段添加注释和默认值

    powerdesigner 字段添加注释和默认值 2017年01月06日 10:59:02 qingzhuoran 阅读数:27161更多 个人分类: powerdesigner   1.选中表,右键 ...

  8. 【PowerDesigner】【3】字段添加注释和默认值

    问题:最开始生成的Table,表头有些字段没有 解决方案: 1,打开表(双击左键),点击下图圈起来的图标 2,找到comment(注释),勾选 3,找到default value(默认值),勾选 4, ...

  9. ArcGIS自定义工具箱-自增字段(可以设置初始值和步长)

    ArcGIS自定义工具箱-自增字段(可以设置初始值和步长) 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:字段值初始值为设定值(默认为0),每次增加一个步长( ...

随机推荐

  1. Struts2 全局结果集-全局result节点设置,package设置abstract=true的实现

    转自:https://blog.csdn.net/u013161278/article/details/41855273 如果我们所有的action均有可能跳到相同的页面,则不妨使用全局result. ...

  2. jquery-messager-消息提示

    一.页面引入 jquery.js 下载地址问度娘 jquery-message.js 下载地址:jquery-message.js 二.页面使用 //ajax轮询检查新的订单 function che ...

  3. java之线程飞机大战制作

    import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...

  4. eclipse java 注释模板配置详解

    设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元 ...

  5. ES02 变量、数组、对象、方法

    1 变量 1.1 变量的声明 利用var关键字来声明变量,例如: var a = 100; <!DOCTYPE html> <html> <head> <me ...

  6. ZROI2018普转提day7t2

    传送门 分析 首先我们不难想到我们一定可以将每一个点分开算,然后看这个点被几个矩形包含 于是对于位置为$(i,j)$的点它被包含的次数为$i * (n-i+1) * j * (m-j+1)$ 这个式子 ...

  7. Linux-shell获取天气

    用Linux中的shell获取天气,本来觉的比较难,原来,真简单,个位数的代码就搞定. 获取对应城市天气 所有天气信息都从中国天气网获取.每一个城市多会对应一个id(比如,北京为101010100,因 ...

  8. 操作系统--UNIX代码段和数据段分开

    (1)代码段:代码段是用来存放可执行文件的操作指令,也就是说是它是可执行程序在内存中的镜像.代码段需要防止在运行时被非法修改,所以只准许读取操作,而不允许写入(修改)操作----它是不可写的. (2) ...

  9. Java50道经典习题-程序41 猴子分桃

    题目:海滩上有一堆桃子,五只猴子来分.第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份.第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿 ...

  10. sql去掉换行符

    select replace(replace(ServiceCall,CHAR(13),''),CHAR(10),'') from outbound_complaint  where  bz1='朱成 ...