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),每次增加一个步长( ...
随机推荐
- javascript——对象的概念——函数 3 (使用技巧)
1.回调函数:将函数A传给函数B,由函数B来执行A,则称A为回调函数. 例1: 例2 function addone(a){;}; //定义一个回调函数 function mulitiply(a,b, ...
- python的文件锁使用
python的文件锁目前使用的是fcntl这个库,它实际上为 Unix上的ioctl,flock和fcntl 函数提供了一个接口. 1.fcntl库的简单使用 import fcntl import ...
- 理解和正确使用Java中的断言(assert)
一.语法形式: Java2在1.4中新增了一个关键字:assert.在程序开发过程中使用它创建一个断言(assertion),它的语法形式有如下所示的两种形式:1.assert conditio ...
- ascii#ascii,对象类中找__repr__,获取其返回值
#!/usr/bin/env python #ascii,对象类中找__repr__,获取其返回值 class Foo : def __repr__(self): return "hello ...
- vue-resource基础介绍
1.vue-resource 的请求api是按照rest风格设计的,它提供了7种请求api get(url, [data], [options]); head(url,[data],[options] ...
- cocos2dx中坐标系
CCNode类的setPosition,getPosition函数如果是一个Node的Child则获取的坐标就是该Node的本地坐标 另一个关键问题就是在cocos2d-x里就是各种对象的大小问题.因 ...
- SqlServer中把结果集放到到临时表的方法(转)
一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #temp from sysobjects 01. 把存储过程结 ...
- gcc 升级方法
Want GCC 4.8 with c++11 complete feature? Well here’s how to install it in Ubuntu 12.04, Ubuntu 13.0 ...
- 第一篇:Django基础
Django框架第一篇基础 一个小问题: 什么是根目录:就是没有路径,只有域名..url(r'^$') 补充一张关于wsgiref模块的图片 一.MTV模型 Django的MTV分别代表: Model ...
- vue 之 介绍及简单使用
浏览目录 vue的介绍 vue的使用 vue的介绍 简介 vue官网说:Vue.js(读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底 ...